Plotting Trajectory Function Output in MATLAB

 

 

·       To see of the types of xy plots MATLAB will generate use the demo page >> demo.

demo à Visualization à 2D Plots

 

·       The most common types of plots are scatter plots and line plots.  Both of these are generated using the MATLAB’s function plot( ).  For details on this function use

>>     help plot

 

Standard Scatter and Line Plots

 

·       Let’s generate some data to plot…

 

[x,y,t]=trajectory(45,100,20);

 

·       Let’s take a closer look at the plot( ) command options…

 

figure(1)

plot(x,y,'ro')

legend('trajectory')

xlabel('horizontal path (feet)')

ylabel('vertical path (feet)')

title('Projectile Trajectory');

grid

axis equal

 

·       The code above produces a scatter plot.  Try a few other options by changing the text string following the data arrays ('ro').  MATLAB is looking for up to 3 characters.  One character representing the color, one the data symbol, and one for the line type.  Look at the help text for a full list of options.

 

plot(x,y,'ro-')

plot(x,y,'r-')

plot(x,y,'b--')

plot(x,y, 'g:')

 

·       The command axis equal forces the plot to have equal scale on the vertical and horizontal axes.  This is nice here because in will make the initial angle look correct on the plot.  The command axis tight gets rid of any space in the plot outside the range of the data.  Using axis([startx,endx,starty,endy]) forces the horizontal axis to go from startx to endx.  It goes from starty to endy on the vertical axis.  See the help text for more details.

 

Adding Text to Your Plot

 

·       The function text(x,y, 'stuff you want printed'), lets you put text at the coordinates x,y on your plot.  If you want to get fancy and put numerical variable values along with text, you need to use sprintf( ) to create your text string.  Look at how it is used below…

 

figure(1)

plot(x,y,'ro')

legend('trajectory')

xlabel('horizontal path (feet)')

ylabel('vertical path (feet)')

title('Projectile Trajectory');

grid

axis tight

 

for k=1:1:length(t)

S=sprintf('t=%.2f s',t(k));

text(x(k)+5,y(k),S);

end

 

 

 

Multiple Plots on Same Axes

 

·       As you have already seen, we can put multiple curves on the same axes…

 

figure(2)

plot(t,y,'b-',t,x,'r--')

legend('vertical trajectory','horizontal trajectory')

xlabel('time (seconds)')

ylabel('path (feet)')

grid

 

 

Using Subplot

 

·       The function subplot( ) lets you put multiple plots on one figure window.  Try this…

 

figure(3)

 

subplot(211)

plot(t,y,'bo-')

legend('vertical trajectory')

xlabel('time (seconds)')

ylabel('vertical path (feet)')

 

subplot(212)

plot(t,x,'bo-')

legend('horizontal trajectory')

xlabel('time (seconds)')

ylabel('horizontal path (feet)')