% Plotting in Matlab
% Written by Robert Rosenbaum for MATH 0290 students at U Pitt, 2012.
% This script contains basic commands for plotting in Matlab.
% Lines beginning with a '%' are comments.
% You can copy and paste the commands below into the Matlab command
% window or you can run this script by switching your current
% directory to the directory containing this file, then typing
% PlotSomeStuff into the command window.
% First, let's close any plots that are currently open in Matlab
close all;
% Let's start by plotting a parabola
% First, define the points along the x-axis at which we will
% evaluate the parabola. We'll make a vector that spans from -5
% to 5 in increments of 0.1.
x=-5:.1:5;
% Now make the points for the y-axis.
% The .^ operator is a element-wise exponent. So, we are saying
% to define y as the vector x with every element squared.
y=x.^2;
% Now let's plot it
plot(x,y)
% This line says to hold on to the plot, so that it isn't erased by
% future plot commands
hold on;
% We can plot the parabola directly, without defining vectors.
% Let's plot this only from 0 to 5 and make it red so that we
% can distinguish it from the previous one.
% 'r' is for red. You can also use 'y', 'g', 'k', 'c', or 'm'
plot(0:.1:5,(0:.1:5).^2,'r')
% This plots makes a black parabola, but draws dots and does
% not connect them with a line
plot(-3:.1:3,(-3:.1:3).^2+1,'k.')
% Now let's open a new figure
figure
% and plot an exponential
x=0:.01:5;
plot(x,exp(-2*x))
% hold on to it
hold on;
% Using too coarse of a grid makes an ugly plot
x=0:1:5;
plot(x,1+exp(-2*x),'r')
% We can label the axes,
xlabel('x value')
ylabel('y value')
% we can title the plot,
title('Problem number 0')
% and we can draw a legend
legend('smooth','too coarse')
% We can also change the zoom by moving the axes
axis([-.5 5.5 -.5 2.25])
% I hope this is enough to get you started.
% Feel free to e-mail or come by during office hours for help.
% You can also find a lot of help online.
% Typing 'help CommandName' gives a description of CommandName in matla
% Try typing 'help plot' for a fe more tips and examples.