12.07.2015 Views

II.1 Programming in MATLAB Getting Started Graphing

II.1 Programming in MATLAB Getting Started Graphing

II.1 Programming in MATLAB Getting Started Graphing

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

%% tgraph.m%% This m-file is a template for creat<strong>in</strong>g graphs of functions. It conta<strong>in</strong>s many of the% commands that are commonly used <strong>in</strong> graph<strong>in</strong>g.%% In this template, the graphs of the s<strong>in</strong>e function and the cos<strong>in</strong>e function are created% <strong>in</strong> one figure w<strong>in</strong>dow.%% This template shows:%% - several ways of plott<strong>in</strong>g% - how to create a grid <strong>in</strong> a figure w<strong>in</strong>dow% - how to label axes% - how to create a title for a figure w<strong>in</strong>dow% - how to create a legend when more than one plot is drawn <strong>in</strong> a figure w<strong>in</strong>dow%clear% Optional Feature. This sets all values to zero.for i = 1:361:1% This is the beg<strong>in</strong>n<strong>in</strong>g of a loop. The <strong>in</strong>dex i <strong>in</strong>creases% from 1 to 361 <strong>in</strong> <strong>in</strong>crements of 1 each time through the% loop. More compact ways of do<strong>in</strong>g loops is shown <strong>in</strong>% the template called tarray.%for i = 1:361 % The default <strong>in</strong>crement is 1.theta(i) = (i-1)*2*pi/360; % theta is an array. theta varies from 0 to 2*pi radians.f(i) = s<strong>in</strong>(theta(i));% f is an array. The i-th entry of f is the s<strong>in</strong>e function% evaluated at the i-th value of theta.p(i) = cos(theta(i));% p is an array. The i-th entry of p is the cos<strong>in</strong>e% function evaluated at the i-th value of theta.end% This is the end of the loop.figuregrid onaxis([0 7 -1 1])hold%plot(theta,f)%plot(f)plot(theta,f,'r')plot(theta,p,'g')%plot(theta,p,'--')%plot(theta,p,':')%plot(theta,p,'r.')title('force and position')xlabel('t-axis')ylabel('force or positon')legend('force','position')% This opens a new figure w<strong>in</strong>dow.% Optional Feature. This creates a grid on your graph.% Optional Feature. This specifies the axis limits% ([theta-m<strong>in</strong> theta-max f-m<strong>in</strong> f-max]).% Optional Feature. This holds the figure w<strong>in</strong>dow for more% graphs.% This plots the array f versus theta (<strong>in</strong> radians).% This plots the array f versus the <strong>in</strong>dex (from 1 to% 361).% This plots the array f versus theta <strong>in</strong> red. The 'r'% <strong>in</strong>dicates the color red.% This plots the array p versus theta <strong>in</strong> green. The 'g'% <strong>in</strong>dicates the color green.% This plots the array p versus theta <strong>in</strong> large dashes.% This plots the array p versus theta <strong>in</strong> small dashes.% This plots the array p versus theta <strong>in</strong> red <strong>in</strong> small% red dots (zoom <strong>in</strong> to see them).% Optional Feature. This creates a title for the figure% w<strong>in</strong>dow.% Optional Feature. This creates a label for the% horizontal axis.% Optional Feature. This creates a label for the vertical% axis.% Optional Feature. This creates a legend for the first% and second graphs <strong>in</strong> the figure w<strong>in</strong>dow.Table 1: Template for Graph<strong>in</strong>g3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!