


using matlab.PDF
using_matlab.PDF
Showing 34 out of 19
using matlab.PDFUsing MATLAB Introduction to MATL...
using_matlab.PDFUsing MATLAB Introduction to MATLAB In
using matlab.PDFUsing MATLAB Intro...
using_matlab.PDFUsing 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 semicolons are used to begin a new row. Ending a line
with a semicolon is optional in MATLAB, doing so suppresses the output of that line from appearing in
the console window. Omitting the semicolons 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 entrywise 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 entrywise 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:
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
lab 18.docx
lab_18.docx
Course
Course
3
Module5QuizSTA2023.d...
Module5QuizSTA2023.docx.docx
Course
Course
10
Week 7 Test Math302....
Week_7_Test_Math302.docx.docx
Course
Course
30
Chapter 1 Assigment ...
Chapter_1_Assigment_Questions.docx.docx
Course
Course
5
Week 4 tests.docx.do...
Week_4_tests.docx.docx
Course
Course
23
Week 6 tests.docx.do...
Week_6_tests.docx.docx
Course
Course
106