Showing 3-4 out of 19
using matlab.PDF-Using MATLAB Introduction to MATL...
using_matlab.PDF-Using MATLAB Introduction to MATLAB In
using matlab.PDF-Using MATLAB Intro...
using_matlab.PDF-Using MATLAB Introduction to MATLAB In
Page 3
Saving this script in a ϐile named
parallel.m
in the current working directory allows you to call it with
the command window, and other ϐiles.
fprintf
Printing out data in MATLAB using commands like
fprintf
is a good habit to get into, as they allow you
to describe your results with more information. Let's say we wrote a script that determined a maximum
allowed voltage
vans
for a certain value of
R
3. The easiest way to report the value of
vans
is to simply
put the variable name on a line of its own, with no semicolon:
>> vans
vans =
31.2100
Compare that to the use of the
fprintf
command below:
>> fprintf('\nMaximum allowed voltage if R3 is %5.2f Ohms, is %5.2f V\n\n', R3, vans)
Maximum allowed voltage if R3 is 66.33 Ohms, is 31.21 V
Type
doc fprintf
into the command window to learn how to use the
fprintf
, there is too much detail
about this command to include here.
Entering Matrices
MATLAB is popular for its ability to work with matrices (MATLAB stands for Matrix Laboratory). Matri-
ces (or arrays) are entered using square brackets like so:
A = [4, -1; -7, 13];
b = [24 -84];
Commas are used to separate columns, and semi-colons are used to begin a new row. Ending a line
with a semi-colon is optional in MATLAB, doing so suppresses the output of that line from appearing in
the console window. Omitting the semi-colons will give you a formatted print out of A and
b
when the
program is run, seen in Figure 3.
>> A = [4, -1; -7, 13]
A=
4
-1
-7
13
>> b = [24 -84]
b=
24
-84
Figure 3: Entering a matrix and row vector in the command window.
We can multiply
A
and
b
together like so:
3


Page 4
>> A*b'
ans =
180
-1260
The dash (') is shorthand for matrix transposition, hence it will change a row vector into a column vector.
Remember to ensure that the dimensions of the matrices you are multiplying agree, otherwise MATLAB
will give you a warning.
Working with Arrays and Matrices
MATLAB can perform entry-wise operations on matrices and arrays by replacing
*
,
/
and
^
with
.*
,
./
and
.^
. Take a look at the following MATLAB session:
>> x = 1:5
x=
1
2
3
4
5
>> x.^2
ans =
1
4
9
16
25
Firstly we deϐine an array
x
using the shorthand notation
start:increment:end
, which creates an array
from the start value to the end value, with entries separated by the speciϐied increment. Typing
x.^2
computes the corresponding array with each entry squared individually. Take a look at another example:
>>a=1:2:10
a=
1
3
5
7
9
>>b=1:3:13
b=
1
4
7
10
13
>> a.*b
ans =
1
12
35
70
117
After
a
and
b
are entered, and the expression
a.*b
is calculated, corresponding to the entry-wise multi-
plication of
a
and
b
.
4


Ace your assessments! Get Better Grades
Browse thousands of Study Materials & Solutions from your Favorite Schools
Pepperdine University
Pepperdine_University
School:
Introduction_to_Engineering_Design_and_Innov
Course:
Great resource for chem class. Had all the past labs and assignments
Leland P.
Santa Clara University
Introducing Study Plan
Using AI Tools to Help you understand and remember your course concepts better and faster than any other resource.
Find the best videos to learn every concept in that course from Youtube and Tiktok without searching.
Save All Relavent Videos & Materials and access anytime and anywhere
Prepare Smart and Guarantee better grades

Students also viewed documents