Você está na página 1de 8

How to Plot a Projectile Motion Using Mat Lab

(Penn State Students)


University students often need to preform complex calculations. Mat Lab
is a programming tool that is used to preform complex calculations and
plotting. This instruction set will help students at Penn State to plot
projectile motion using Mat Lab through webapps (Penn State Website).
Before starting, reading the Mat Lab Concepts section will help you recall
some important Mat Lab concepts.

Mat Lab Concepts:


Mat Lab is case sensitive
When multiplying variables in mat lab, make sure to insert a * sign
between variables that will be multiplied otherwise the multiplication
will not occur
A dot before a multiplication sign should be added incase of
multiplying arrays (matrices)
Some functions are built up in the program (sin, cos, logetc.)
Do NOT use function names when naming files on Mat Lab
Writing in Mat Lab has two different forms:
1. Command form (Done by typing commands)
2. Note form (Done by typing notes and comments after % sign)
Do NOT use variables without defining them first; otherwise an error
message will show up in the command window.

Step 1: Open Mat Lab


1. Go to webapps.psu.edu using any browser engine you prefer.
A screen will show
up displaying
some programs
that Penn State
offer to its students
(Fig.1).
2. Double click on
MATLAB2014a icon
on the display screen.

Fig. 1: Webapps display screen

A screen will show up


that asks you to enter
your Penn State
username and password.
3. Enter your Penn State user
name and password.
After this step, Mat Lab
display screen will show
up (Fig. 2).

Fig. 2: Mat Lab display screen

Step 2: Create a new Script


Click on the New Script icon on the top left
corner under the home menu (Fig. 3).
This will create a field above the command
window to insert commands that you can
save.
Fig. 3: New Script icon

Note: you can use the command window to insert


your commands but these commands cannot be saved for later use.
At this point you will start organizing your script and insert you commands
in the script field.
Step 3: Write details about the file
This step and the next step are NOT mandatory. However, these steps will
help the user to understand the script use.
Type the following information in a note form (Use % before each title):
%File name: ProjectilePlot.m
%Programmer name: Your name
%Date: The date you created the file on (xx/xx/xxxx)
%Short description: Plotting an object shot in different angles as a
projectile motion.

This will provide a helpful description for the user and now you will
start describing the variables you will be using
Step 4: List the variables
Using a note form, list the variables that you will use to calculate and plot
the projectile motion.
Type the following:
%List of Variables:
% v_initial is used to represent the initial velocity of the shot in m/s
% theta is used to represent the varies angles between 0 and pi/2 in radians
% range is used to represent the range (height) of an object at varies
% angels in meters
% g is used to represent the value of gravity, g=9.81 m/s^2
% air resistance is neglected
This detailed list of variables will provide an easier understanding
for the user of each variable meaning.
Step 5: Insert initialization and calculations
Starting from this step every command is mandatory to plot the projectile
motion.
1. Write the initialization in a command form under Variable initialization:
%Variable Intialization:
v_initial=input('Please enter a value for the initial velocity of the object in
m/s >> ');
Command Structure:
input is a command that allows the user to insert a certain value for the
variable
Parenthesis should follow the command
Apostrophes indicate the comment that will show up later in the
command field when running the script.

2. Write the calculation commands in a command form under


Calculations:
%Calculations:
theta= 0:(pi/100):(pi/2);
g = 9.81;
range = (((v_initial).^2)/g).*sin(2.*theta);
Command Structure:
theta=0:pi/100:pi/2 sets the variable theta equal to an array of value
from 0 to pi/2 with an increment of pi/100
g=9.81 indicates that g is a constant
range = (((v_initial).^2)/g).*sin(2.*theta) is the equation that will be used
to plot the projectile motion graph
Step 6: Display result
Write the following commands in a command form to display the plot of
the projectile motion:
% Displaying the results:
fprintf('The plot of the entire range of an object shot at angles between 0
and pi/2 radians,\n with the initial velocity of %0.2f m/s \n is given in the
plot window\n', v_initial );
plot (theta,range)
title('Range versus angles')
xlabel('Angles in radains')
ylabel('Range / height in meters ')
Commands Structure:
fprintf is a command that is used to display a statement and value using
the variables that were defined
Parenthesis should follow the command
Apostrophes indicate the comment that will show up later in the
command field when running the script.
%0.2f indicates a number with two decimals.
\n indicates an ending of a comment.

A comma is needed after the closing apostrophe in order to insert the


variable.
v_intail should be used after the comma so that the program will
display the value for the initial velocity in the command field.
plot is command to plot a graph based on two variables
theta is going to be the horizontal variable
range is going to be the vertical variable
title, xlabel, and ylabel are commands used to label the plot and each
coordination of the plot
Parenthesis are needed after each command
Apostrophes are needed to indicate a comment on the plot
After this step you have completed writing the program and now you need
to test it.
Step 7: Run the program and use a sample
1. Click on the Run icon under the Editor menu (Fig. 4).
A box will show up that will ask you to save to save
the script in a file.
2. Name the file with the same name you described earlier
Make sure to save it as an m.file.

Fig. 4:
Run
icon

In the command window, you will be asked to insert a value for the
initial velocity
(Fig. 5).
Fig. 5: Input field

3. Insert 10 as a
value for the initial velocity (any other number is acceptable)
In the command window a description of the plot will appear (Fig.
6).

Fig. 6: Result statement

Also, the
plot of the
projectile
motion will
appear in an
external
box.

Fig. 7: Projectile motion plot

Congratulation! At this point you have successfully completed the


assessment.

Você também pode gostar