STEM Course Prep
MATLAB Exercises
Chapter 1
Slides 1-5
1. Open MATLAB. Change font/colors if needed.
Slides 6-8
2. Enter the following at the command prompt:
a = 2
Notice it repeats the value of ‘a’. Look in the workspace – you will find the value for ‘a’, as well as the
min/max of ‘a’.
3. Enter the command:
a = 2;
Notice the value of ‘a’ is suppressed.
4. Enter the command:
a=a+7
followed on a new line by:
a=a*10
The final value should be 90 ( (2+7)*10 = 90).
Slides 9-11
5. Press the up arrow key. All recent commands will be displayed one at a time.
6. Clear the variable ‘a’ with the following command:
clear a
Look in the workspace – ‘a’ is gone. The command ‘clear’ will clear all variables.
7. Enter the commands:
Copyright © 2017 STEM Course Prep
Radius = 2.37
Radius = radius+2
MATLAB is case-sensitive! Radius is defined; radius is not.
8. Enter the command:
who
MATLAB shows all defined variables.
9. Enter the command:
whos
MATLAB shows info about the variables – you can see if it’s an integer/real number.
Change the variable ‘Radius’ into an integer. Enter the command:
Radius_integer = int32(Radius)
MATLAB rounds the value to 4.
10. Enter the command:
whos
Slides 12-15
11. Calculate the density of air at standard temperature using the ideal gas law
𝑃𝑃
𝜌𝜌 =
𝑅𝑅𝑅𝑅
if pressure (P) is 101300 Pa, gas constant (R) is 286.9, and temperature (T) is 293 K. Enter the command:
P =101300;
R=286.9;
T=293;
Density= P/(R*T)
Result: 1.2051
Change to long format. Enter:
format long
Density
Result: 1.205067230379590
Copyright © 2017 STEM Course Prep
To see other format types:
help format
12. Determine the final displacement after 30 seconds of a vehicle that has an initial velocity of 30 m/s and a
deceleration of 1 m/s2. Initial displacement is 27900 m. The kinematic equation is:
1
𝑠𝑠 = 𝑠𝑠𝑜𝑜 + 𝑣𝑣𝑜𝑜 𝑡𝑡 + 𝑎𝑎𝑡𝑡 2
2
initial_disp = 27900;
initial_velocity = 30;
time=30;
accel = -1;
final_disp = initial_disp+initial_velocity*time+(accel*time^2)/2
Result: 28350
Slides 16-21
13. Enter:
help elfun
Enter:
help sin
14. Calculate the cosine of 75°. We are calling the function cosd and 75 is the input argument.
cosd(75) used cosd because the input argument was in degrees
Result: 0.258819045102521
cos(75*pi/180)) to use cos the input argument must be in radians
Result: 0.258819045102521
Calculate the cosecant of 0.3 rad. Enter:
csc(0.3)
Result: 3.383863361824123
Copyright © 2017 STEM Course Prep
Solve 𝑐𝑐𝑐𝑐𝑐𝑐 −1 (2.51). Find answer in degrees and radians. Enter:
acot(2.51)
acotd(2.51)
Result: 0.379131807531353 (rad), 21.722652450712772 (deg)
15. Test out the various rounding functions: Use -10.456 as the test value.
a=-10.456
round(a)
ceil(a)
floor(a)
fix(a)
sign(a)
Result: -10 (standard rounding)
-10 (rounds towards positive infinity)
-11 (rounds towards negative infinity)
-10 (rounds towards zero)
-1 (-1 indicates value less than zero. 1 would be given for positive number)
16. The displacement for a mass in a second-order underdamped system can be found using the equation:
𝜉𝜉 1
𝑥𝑥(𝑡𝑡) = 𝑒𝑒 −𝜉𝜉𝜔𝜔𝑛𝑛𝑡𝑡 �� 𝑥𝑥(0) + 𝑥𝑥̇ (0)� sin(𝜔𝜔𝑑𝑑 𝑡𝑡) + 𝑥𝑥(0)cos(𝜔𝜔𝑑𝑑 𝑡𝑡)�
�1−𝜉𝜉 2 𝜔𝜔𝑑𝑑
𝑤𝑤ℎ𝑒𝑒𝑒𝑒𝑒𝑒 𝜔𝜔𝑑𝑑 = 𝜔𝜔𝑛𝑛 �1 − 𝜉𝜉 2
Using the following values, calculate displacement.
𝜔𝜔𝑛𝑛 = 6 𝑟𝑟𝑟𝑟𝑟𝑟 (𝑛𝑛𝑛𝑛𝑛𝑛𝑛𝑛𝑛𝑛𝑛𝑛𝑛𝑛 𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓)
𝜉𝜉 = 0.5 (𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑 𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟)
𝑥𝑥(0) = 10 𝑚𝑚 (𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖 𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑)
𝑥𝑥̇ (0) = 0 𝑚𝑚/𝑠𝑠 (𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖 𝑣𝑣𝑣𝑣𝑣𝑣𝑣𝑣𝑣𝑣𝑣𝑣𝑣𝑣𝑣𝑣 − 𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟 𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓 𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟)
𝑡𝑡 = 0.605 𝑠𝑠 (𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡)
clear
damp_ratio = 0.5;
w_n = 6;
init_disp=10;
init_vel = 0;
t=0.605;
w_d= w_n*sqrt(1-damp_ratio^2);
a=init_disp*cos(w_d*t);
b=exp(-damp_ratio*w_n*t);
c=(damp_ratio/sqrt(1-damp_ratio^2)*init_disp);
d=(1/w_d)*init_vel;
displacement=b*((c+d)*sin(w_d*t)+a)
Copyright © 2017 STEM Course Prep
Slide 22
17. Enter the commands:
3<5
Result: 1 (3 is less than 5 so the statement is true. True statements always result in 1.)
Result:
10>=10 1 (10 is equal to 10 – true statement – true is represented by a value of 1)
9>=10 0 (statement is false. False statements result in a value of zero)
1 (statement is true)
11>=10
Copyright © 2017 STEM Course Prep