Northeastern University
ML-1: Basics of MATLAB
Trajectories without Air Friction
BACKGROUND INFO
This exercise will use MATLAB to calculate and plot the frictionless trajectories of a projectile such as you have studied
in physics or will see soon. You will learn to use MATLAB to define variable names, use trig functions, solve a quadratic
equation, define an a
ay, use a
ay multiplication and exponentiation (“ .* ” and “ .^ ” in MATLAB) to calculate a
ays
of x- and y-points, and then plot these points to see the trajectory.
The problem is illustrated in Figure 1. A ball is projected at an angle of 60° with an initial velocity of 100 m/sec and
traces out a parabolic trajectory as shown in Figure 1. Similar to the warm-up exercise, we want to find the distance x2 - x1
for several different heights, y. The initial velocity can be
oken into a horizontal and vertical component, Vox and Voy,
and then the usual kinematic equations can be used for the x- and y-displacement. Since there is no horizontal force in the
x-direction, the x-component (distance) after an elapsed time t is given by:
x = Vox t = Vo cos(60)t
In the y-direction (height) there is a constant downward acceleration vector, g = -9.8 m/s2, as well as the initial upward
component of velocity, Voy = Vosin(60), and the height of the ball at a time t is given by:
y = Voyt + .5gt2 (same as: y = y0+Voyt + ½gt2)
Assign g to be a negative number because it is a downward vector. The instantaneous y-velocity is given by:
Vy = Voy + gt
At the top of the trajectory Vy = 0, so the time to reach the top can be found from:
ttop = -Voy /g.
So, the first step is to solve the warmup exercise and review trajectory physics, and the next step is to become acquainted
with how MATLAB functions so that it may be used to solve this and other real-world problems.
H
ei
gh
t (
m
)
Vo = 100 m/s
60 °
x1
x2
y
Figure 1. Height vs. Distance for projectile motion problem
Distance (m)
y
x
vo = 200 m/s
y
x
50o
y
x
ymax = ?
WARM-UP EXERCISE
Solve by hand to find the maximum height and total time that the
all is in the air for a ball projected with an initial velocity of 200 m/s at an
angle of 50o (from a height of 0). Show all formulae used and all work with
interim values.
P1. Calculate the maximum height y that the ball reaches in flight:
a. Formulae used: ____________________________________________________
_____________________________________________________________________
. Solution: _________________________________________________________
_____________________________________________________________________
____________________________________________________________________________________________
____________________________________________________________________________________________
P2. Calculate the length of time that the ball is in the air:
a. Formulae used: ____________________________________________________
_____________________________________________________________________
. Solution: _________________________________________________________
_____________________________________________________________________
____________________________________________________________________________________________
____________________________________________________________________________________________
GETTING STARTED:
• To write the m-file by starting new:
In the Command window, choose File => New => M-file. The Edito
Debugger window will open. You may
egin typing the m-file. The contents can be generated by extracting the relevant commands from the diary file
(that should be executable). Then you will Save as: LastName_ML1.m in your directory.
• To run your m-file:
Return to the Command Window and run the m-file by typing LastName_ML1. It should run; otherwise you
will need to check for e
ors and make co
ections in order to get the appropriate output and figures.
If you get e
or messages, you may have an invalid command or made a typographical e
or. Check your code
and make the co
ection.
Test your program with your answers from the warm up exercise to ensure its working co
ectly!
t = ?
MATLAB EXERCISE
Use my pseudocode to begin writing the code and take note of the details required
%Start with a friendly greeting
%Ask user for required inputs
%Solve for initial velocities
%Solve for the maximum height the ball reaches
• Return answer to the user in a sentence
%Solve for the length of time the ball is in the air
• Return answer to the user in a sentence
%Create a vector for time with 100 values
%Plot height versus time
• Label the plot axes with units
• Have a title
• Choose a marker
%Plot height versus distance
• Don’t overwrite your previous figure!
• Label the plot axes with units
• Have a title
• Choose a new marker
Here is one example of a user interface:
To submit: Upload to Canvas your m-file!!!!!!!
RUBRIC
Points
Upload .m file to Canvas with co
ect name 5
File runs without e
ors and returns co
ect values.
• Max height
• Time in air
• (test with an initial velocity of 200 m/s at an angle of 50o )
30
Program has a clean user interface/ formatting 10
File co
ectly creates 2 figures with proper labels
• X axis with name and units
• Y axis with name and units
• Title
• Distinct marker
• 100 points on the plot
30
Program uses appropriate commands as requested
• (fprintf vs. display) 10
Program Clarity
• Commented thoroughly and cleanly
• Formatted appropriately (indentations/parentheses)
15
Total 100
BONUS CHALLENGE
The following challenge is optional for students looking to challenge themselves.
• Take the program above and change it to include a user input of an initial height.
• In addition to the required code, solve for the total (x) distance traveled by the ball.
• Update all the necessary equations
• Save the file as LastName_BML1.m
• Upload only this file to BB.
If you are successful you will receive three (3) bonus points on this activity
ML-1: Basics of MATLAB
y
x
y
x
y
x
Matlab Basics: Hand Out
1. Circle the bad variable names:
• Donotpassgodonotcollect$200
• Two_nums
• 2BR02B
• Mult2
• Is_this_Valid?
• GClefSign
2. Answer the following assuming:
T=[9 10 ; 11 12]
V=[2 4 ; 6 8 ; 10 12]
• A = T (1,1) + V (1,1) =
• E = V ( : , 2)+T ( : , 2) =
• J = T (1 , : ) .* V ( 1 , : ) =
3. In your own words, what does the following code return?:
Red=[6:2:24]
4. Search the help menu for the randi function. Write a
command that generates 20 random integers between -5
and 5.
Practice Program: AGE_Eval
Hints:
• Use all of the input/output commands: disp(), input(), and fprintf()
• Use single quotes to indicate text.
• Use a double apostrophe to show possession: Bob’s Hat ‘Bob’’s Hat’
• Make sure you suppress the echo with semicolons!
• Use %2i as your variable placement in the frprintf command. The “i” stands
for integer. See the Matlab help for more options!
Write your first program: Data Import and Analysis
The temperatures for the month of September are located in a file on Blackboard.
Write a Matlab code in a script file that does the following:
• Load both files
• Plot the Low and High data together on a plot so that it looks EXACTLY
like the picture below.
o Don’t forget the title, labels and legend!
1 69
2 66
3 70
4 62
5 58
6 61
7 66
8 72
9 72
10 64
11 63
12 60
13 63
14 57
15 62
16 65
17 64
18 65
19 62
20 57
21 53
22 56
23 54
24 58
25 55
26 50
27 48
28 56
29 65
30 57
1 79
2 91
3 82
4 72
5 73
6 82
7 93
8 96
9 93
10 77
11 73
12 71
13 66
14 72
15 84
16 81
17 89
18 85
19 79
20 75
21 66
22 68
23 68
24 74
25 66
26 63
27 64
28 78
29 84
30 78