Sei sulla pagina 1di 3

For Evaluation Only.

Copyright (c) by VeryPDF.com Inc


Edited by VeryPDF PDF Editor Version 2.2

MATLAB Week 3 Notes


2-Dimensional Plotting (Chapter 5 in Gilat)
Notes: MATLAB commands and output are shown in a bold red font

Overview
● Integrated plotting is one of the most useful tools available in MATLAB

Basic Plotting (Gilat 5.1 - 5.1.1)


The most basic way to plot data in MATLAB is using the plot command. The main two arguments to
the plot command are vectors. The first argument is independent variable and the second argument is
the dependent variables. The following is an example using the plot command.

Voltage (V) Current (mA)


10 1
15 5
20 10
25 20

>> V=[10 15 20 25];


>> I=[1 5 10 20];
>> plot(I,V)
>> title('Example Plot')
>> xlabel('Current, mA')
>> ylabel('Voltage, V')
Plotting Functions (Gilat 5.1.2 – 5.2)
Plotting functions can be done in the exact same way as plotting raw data, where the only difference is
that we generate the raw data. For example, given a function, f(x)=3x*sin(x)-2x, plot it and its
derivative:

>> x=[-2*pi:pi/12:2*pi];
>> f_x=3*x.*sin(x)-2*x;
>> df_dx=3*(x.*cos(x)+sin(x))-2;
Plotting them on the same graph
>> figure
>> plot(x,f_x,x,df_dx,'--');
>> title('f(x), df/dx vs. x')
>> legend('f(x)','df/dx')
>> ylabel('f(x), df/dx')
>> xlabel('x')

Multiple Graphs on one Figure (Gilat 5.9)


Sometimes we want to include multiple graphs in one figure. This is accomplished in MATLAB by
using the subplot command. The following sample demonstrates this command.

Sample problem # 12 with a different x(t)


x(t) = 0.8*t^3-6*t+9 m
v(t) = dx_dt = 0.8*3*t^2-6 m/s
a(t) = dv_dt = 0.8*3*2*t m/s^2
clear all
t=0:.5:7;
x=0.8*t.^3 - 6*t + 9;
v=0.8*3*t.^2-6;
a=0.8*3*2*t;
subplot(3,1,1); % 3->number of rows, 1-> number of columns, 1->index (1st row)
plot(t,x)
xlabel('t [seconds]')
ylabel('x(t) [m]')
title ('Position')
subplot(3,1,2); % 2->2nd row
plot(t,v)
xlabel('t [seconds]')
ylabel('v(t) [m/s]')
title ('Velocity')
subplot(3,1,3); % 3->3rd row
plot(t,a)
xlabel('t [seconds]')
ylabel('a(t) [m/s^2]')
title ('Acceleration')

P o s it io n
300

200
x (t) [m ]

100

0
0 1 2 3 4 5 6 7
t [s e c o n d s ]
V e lo c it y
200

100
v (t) [m /s ]

-1 0 0
0 1 2 3 4 5 6 7
t [s e c o n d s ]
A c c e le ra t io n
40
a (t) [m /s 2 ]

20

0
0 1 2 3 4 5 6 7
t [s e c o n d s ]

Potrebbero piacerti anche