Eng.
Shahenaz AlRajfi
• MATLAB allows you to plot data sets for
better visualization and interpretation.
• There are different types of plots available
in MATLAB (see next) including 2D and 3D
plots.
• You can control all aspects of the plot:
lines, colors, grids, labels, etc.
• Plotting clear and easy-to-read figures is
an important skill, which you gain from
experience.
[Link] AlRajfi
x = 0:2*pi/100:2*pi;
y1 = sin(x);
plot(x,y1);
xlabel('x');
ylabel('y');
title('Example');
plot(y1): Plots values of y1 versus their indices
if y1 is a vector.
[Link] AlRajfi
x = 0:2*pi/100:2*pi;
y1 = sin(x);
y2 = cos(x);
y3 = sin(x)+cos(x);
plot(x,y1);
hold on;
plot(x,y2);
plot(x,y3);
xlabel('x');
ylabel('y');
title('Example');
hold off;
[Link] AlRajfi
x = 0:2*pi/100:2*pi;
y1 = sin(x);
y2 = cos(x);
y3 = sin(x)+cos(x);
plot(x,y1,x,y2,x,y3);
xlabel('x');
ylabel('y');
title('Example');
[Link] AlRajfi
• For full details enter help plot in
MATLAB.
[Link] AlRajfi
x = 0:2*pi/100:2*pi;
y1 = sin(x);
y2 = cos(x);
y3 = sin(x)+cos(x);
plot(x,y1,'r-.',x,y2,'g-x',x,y3,'b+');
xlabel('x');
ylabel('y');
[Link] AlRajfi
• Legends:
1. With multiple lines on the same plot it is a good idea to add a legend.
[Link] can also move the legend with the mouse.
legend('sin','cos','sin + cos');
• gtext(‘text’):
Places a string in the Figure window at a point specified by the mouse.
• text(x,y,’text’):
Places a string in the Figure window at a point specified by coordinates x, y.
• axis([xmin xmax ymin ymax]):
This command sets the scaling for the x- and y-axes to the minimum and maximum
values indicated.
[Link] AlRajfi
• grid on:
To add gridlines.
• grid off:
To stop plotting gridlines.
• figure:
To open a new figure window.
• Complex Plot: Real vs. Imaginary
n = [0:0.01:10];
y = (0.1+0.9j).^n;
plot(y);
xlabel('Real');
ylabel('Imaginary');
[Link] AlRajfi
Your Output:
plot the polynomial 3x5 + 2x4
– 100x3 + 2x2 – 7x + 90 over
the range –6 ≤ x ≤6 with a
spacing of 0.01, you type
[Link] AlRajfi
Load is governed by:
Solution;
-What is the equation for the
practical source (i_source)? -The equation for the power supply is:
Assume: R1 = 30Ω, v1 = 15V.
-Find the correct value for v2 -If we draw both equations, we can see
between 0 and 20V, and also the solution point (the one that satisfies
both equations).
i1 value.
[Link] AlRajfi
• Subplots:
You can use the subplot command to obtain several smaller “subplots”
in the same figure. The syntax is subplot(m,n,p). This command divides
the Figure window into an array of rectangular panes with m rows and n
columns. The variable p tells MATLAB to place the output of the plot
command following the subplot command into the pth pane.
[Link] AlRajfi
Your Output:
Write a program to generate
two plots:
[Link] AlRajfi
[Link] AlRajfi
bar STAIRS
x = [0:pi/20:pi]; x = [0:pi/20:2*pi];
stairs(x,sin(x));
bar(x,sin(x));
grid;
axis([0 2*pi -1 1]);
POLAR STEM
theta = [0:pi/90:2*pi]; x = [-2*pi:pi/20:2*pi];
polar(theta , sin(2*theta)); x = x + (~x)*eps;
grid; y = sin(pi*x)./(pi*x);
stem(x,y);
[Link] AlRajfi axis([-2*pi 2*pi -.25 1]);
The following program uses the plot3 function
to generate the spiral curve shown in the next
slide.
t = 0:pi/50:10*pi;
x = exp(-0.05*t).*sin(t);
y = exp(-0.05*t).*cos(t);
z = t;
plot3(x, y, z);
xlabel('x'),ylabel('y'),zlabel('z'),grid;
x = e−0.05t sin t, y = e−0.05t cos t, z =
t
[Link] AlRajfi
The following session shows how to generate the
surface plot of the function z = xe−[(x−y2)2+y2], for
and , with a spacing of 0.1
[X,Y] = meshgrid(-2:0.1:2);
Z = X.*exp(-((X-Y.^2).^2+Y.^2));
mesh(X,Y,Z);
xlabel('x'),ylabel('y'),zlabel('z');
[X,Y] = meshgrid(-2:0.1:2);
Z = X.*exp(-((X-Y.^2).^2+Y.^2));
surf(X,Y,Z);
xlabel('x'),ylabel('y'),zlabel('z'),colorbar
[Link] AlRajfi
• A movies is just successive plots seen in quick succession
• We can plot data repeatedly on a single figure.
• For example the function
x = 0:2*pi/100:2*pi;
for t = 0:0.05:5 % 5 seconds
y = sin(x+t);
plot(x,y,’k’)
pause(0.2); % 200 ms between frames
end
[Link] AlRajfi