Chapter 5 - Matlab - V1 - ISE
Chapter 5 - Matlab - V1 - ISE
Chapter 5
Plotting
xy Plotting function
Plot, Label and Title Commands
plot (x,y)
is used to plot a function y=f(x),
plots the x values on the horizontal axis and the y values on
the vertical axis.
xlabel (‘text’) and ylabel (‘text’): put labels on horizontal and
vertical axis.
title (‘text’): puts a title at the top of the plot.
xy Plotting function
Plot, Label and Title Commands
>> x=[0:0.1:52];
>> y=0.4.*sqrt(1.8.*x);
>> plot(x,y)
[x,y]=fplot(‘string’, limits)
limits may be [xmin xmax] or [xmin xmax ymin ymax]
xy Plotting function
fplot
>> f='cos(tan(x))-tan(sin(x))';
>> fplot(f,[1 2])
>> x=[1:0.01:2];
>> y=cos(tan(x))-tan(sin(x));
>> plot(x,y)
xy Plotting function
Plotting Polynomials
>> x=[-6:0.01:6];
>> p=[3,2,-100,2,-7,90];
>> plot(x,polyval(p,x)),xlabel('x'),ylabel('p')
xy Plotting function
Practice
>> x=[0:0.01:5];
>> y=exp(-1.2*x).*sin(10*x+5);
>> subplot(1,2,1)
>> plot(x,y),xlabel('x'),ylabel('y'),axis([0 5 -1 1])
>> x=[-6:0.01:6];
>> y=abs(x.^3-100);
>> subplot(1,2,2)
>> plot(x,y),xlabel('x'),ylabel('y'),axis([-6 6 0 350])
xy Plotting function
Practice
u 6 log10 v 2 20 for 8 v 8
xy Plotting function
OVERLAY PLOTS
of the matrix A.
xy Plotting function
Data Marked and Line Types
To plot the vector y versus the vector x and mark each point
with a data marker, enclose the symbol for the marker in single
quotes in the plot function.
Some specifiers for data markers, line types, and colors
Data markers Line types Colors
Dot (.) . Solid line - Black k
Asterisk (*) * Dashed line -- Blue b
Cross (x) x Dash-dotted line -. Cyan c
Circle (o) o Dotted line : Green g
Plus sign (+) + Magenta m
Square () s Red r
Diamond () d White w
Five-pointed star p Yellow y
xy Plotting function
Data Marked and Line Types
>> x=[0:0.1:pi];
>> y=sin(x);
>> plot(x,y,x,y,'o')
xy Plotting function
Data Marked and Line Types
Practice:
Using subplot command to plot the function
y= 2(x-1)Sinx + 3
in 6 small windows with 6 different formats in data
markers, line types and colors.
xy Plotting function
LABELING CURVES AND DATA
>> x=[0:0.01:2];
>> y=sinh(x);
>> z=tanh(x);
>> plot(x,y,x,z,'--'),xlabel ('x'),ylabel ('Hyperbolic Sine and
Tangent'),legend('sinh(x)','tanh(x)')
xy Plotting function
Plotting Complex Data
Ex: Plot the real part and imaginary part of this function in one
figure:
y (t ) e 0.2t Cos(t ) i Sin(t) , 0 t 4
- Real part: red, solid line with * marker, linewidth = 1
- Imaginary part: green, dashed line with o marker, linewidth =
2
- Xlabel: t
- Ylabel: y(t)
- Title: Quiz 15/05/2021
- Legend: Real Part, Imaginary Part
xy Plotting function
Plotting Complex Data
t = 0:pi/20:4*pi;
y=exp(-0.2*t).*(cos(t)+i*sin(t));
plot (t,y, ‘LineWidth’,2);
title(‘\bfPlot of complex function vs time’);
xlabel(‘\bf\itt’);
ylabel(‘\bf\ity(t)’);
t = 0:pi/20:4*pi;
y = exp(-0.2*t).*(cos(t)+i*sin(t));
plot(y, ‘b-’, ‘LineWidth’,2);
title(‘\bfPlot of complex function’);
xlabel(‘\bfReal part’);
ylabel(‘\bfImaginary part’);
xy Plotting function
Plotting Complex Data
t = 0:pi/20:4*pi;
y = exp(-0.2*t).*(cos(t)
+i*sin(t));
polar(angle(y),abs(y));
title(‘\bfPlot of complex
function’);
xy Plotting function
LABELING CURVES AND DATA
text command
text(x,y, ‘string’) : adds a text string to the plot at the location
specified by the coordinates x, y.
gtext(‘string’) : place the label next to the plot, where string is a
text string that specifies the label of your choice.
>> x=[0:0.01:1];
>> y=tan(x);
>> z=sec(x);
>>
plot(x,y,x,z),xlabel('x'),ylabel('Tangent
and Secant'),gtext('tan(x)'),text
(0.3,1.2,'sec(x)')
xy Plotting function
ANNOTATING PLOT
You can create text, titles, and labels that contain mathematical
symbols, Greek letters, and other effects such as italics.
The text, gtex, title, xlabel and ylabel commands all require a
string as their argument.
Ex:
z=f(x,y) represents a surface when plotted on xyz axes, and the mesh
ÞGenerate a grid of these points in the xy plane first, and then evaluate
>> [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')
xy Plotting function
Contour plots THREE-DIMENSIONAL PLOTS
contour(X,Y,Z)
>> [X,Y]=meshgrid(-2:0.1:2);
>> Z=X.*exp(-((X-Y.^2).^2+Y.^2));
>> contour(X,Y,Z),xlabel('x'),ylabel('y')
xy Plotting function
THREE-DIMENSIONAL PLOTS
PRACTICE 2
Create the surface mesh plot and a contour plot of the function
z x 2 2 xy y
2 2