Week 9 Matlab - Part 1
Week 9 Matlab - Part 1
an Engineer
An Active Learning
Approach
Stephan, Bowman, Park, Sill, Ohland
Third Edition
Copyright © 2015
Pearson Prentice-Hall, Inc.
MATLAB is a programming and numeric computing
platform used by millions of engineers and scientists to
analyze data, develop algorithms, and create models.
This is the
current file.
Names in MATLAB are case sensitive: Frog and frog are different.
A. N30a
B. 20a
C. Hello_
D. i
E. K
A. N30a B
B. 20a
C. Hello_
D. i
E. K
X = 5
Place the number "5"
into the variable "X"
Thinking Like an Engineer An Active Learning Approach
Stephan, Bowman, Park, Sill, Ohland 3rd Edition Copyright © 2015 Pearson Prentice-Hall, Inc.
The symbol EQUAL TO (=) does not mean EQUAL TO as in
algebraic expression; in MATLAB
= means "complete the tasks on the right-hand side,
then store in the left-hand side variable"
Y = 2 * XX
Multiply the current value of "X"
(5) by 2, and store the answer in
the variable "Y"
Thinking Like an Engineer An Active Learning Approach
Stephan, Bowman, Park, Sill, Ohland 3rd Edition Copyright © 2015 Pearson Prentice-Hall, Inc.
The symbol EQUAL TO (=) does not mean EQUAL TO as in
algebraic expression; in MATLAB
= means "complete the tasks on the right-hand side,
then store in the left-hand side variable"
Y = 8 + YY
Add 8 to the current value of "Y" (10), and store
the answer back into the variable "Y"
1 X 1 MATRIX
RTP = 3^2 + 1
Q = sqrt(-9)
Thinking Like an Engineer An Active Learning Approach
Stephan, Bowman, Park, Sill, Ohland 3rd Edition Copyright © 2015 Pearson Prentice-Hall, Inc.
What is stored in the variable when each line of MATLAB
code below is executed?
Q = sqrt(-9) Q contains 0 + 3i
Thinking Like an Engineer An Active Learning Approach
Stephan, Bowman, Park, Sill, Ohland 3rd Edition Copyright © 2015 Pearson Prentice-Hall, Inc.
Default numeric display: four decimal places unless the
value has no fractional part.
X = 0: 0.25: 100
This generates a vector from 0 to 100, in increments of 0.25
Vector = Start value: Increment: End value
Y = 0: 100
This generates a vector from 0 to 100, in increments of 1
Vector = Start value: End value
**Default increment is 1 if it is not specified
Countdown = 5: -1: 0
This generates a vector with decreasing value, dependent on the increment.
Vector = Maximum value (Start value): decrease increment: minimum value
LSeq = 1:5;
Miss = 1:5:20;
LSeq contains [1 2 3 4 5]
LSeq = 1:5;
You could also enter LSeq = [1: 5];
If you attempt to add two vectors that are not the same
size, you will get angry red letters indicating an error!
D1 = V1 – 4;
S1 = V1 + V3;
E1 = V1 + V2;
D1 = V1 – 4; D1 contains [-2 1 5]
S1 contains [6 -2 9]
S1 = V1 + V3;
P1 = V2 * 300;
Q2 = V3 ./ V1;
R3 = 5 / V1;
MATrix LABoratory
M1 = [0 -4 3; -2 6 0.2];
M1 = [0 -4 3; -2 6 0.2];
T1 = M1’;
D1 = 7 – ExM1;
E1 = ExM1 + ExM2;
D1 = 7 – ExM1; D1 =
𝟒 𝟔. 𝟓 𝟕
𝟐 𝟏𝟒 𝟓
E1 = ExM1 + ExM2;
Error! Matrix dimensions
must agree.
D1 = 7 – ExM1;
E1 = ExM1 + ExM2;
D1 = 7 – ExM1; D1 =
𝟒 𝟔. 𝟓 𝟕
𝟐 𝟏𝟒 𝟓
E1 = ExM1 + ExM2;
Error! Matrix dimensions
must agree.
Calculate
% Calculate density of acetone
𝛒 = 𝐒𝐆 𝛒𝐰𝐚𝐭𝐞𝐫 rho_a = SG * rho_w; % kg/m^3