MATLAB Examples - Plotting
MATLAB Examples - Plotting
blog
Hans-Petter Halvorsen
Plotting
MATLAB has powerful Plotting features
>> x = 0:0.1:2*pi;
>> y = sin(x);
>> plot(x,y)
>> x = 0:0.1:2*pi;
>> y = sin(x);
>> y2 = cos(x);
>> plot(x,y, x,y2)
...
>> plot(x,y,'r*', x,y2,'g+')
Plotting
Plotting functions:
Name Description
plot Create a Plot >> x=0:0.1:2*pi;
>> y=sin(x);
figure Define a new Figure/Plot window
>> plot(x,y)
grid on/off Create Grid lines in a plot >> title('Plot Example')
title Add Title to current plot >> xlabel('x')
xlabel Add a Label on the x-axis
>> ylabel('y=sin(x)')
>> grid on
ylabel Add a Label on the x-axis >> axis([0,2*pi,-1,1])
axis Set xmin,xmax,ymin,ymax >> legend(’Temperature')
hold on/off Add several plots in the same Figure
legend Create a legend in the corner (or at a
specified position) of the plot
subplot Divide a Figure into several Subplots
Plotting
Given the following Rain Data for a given Week (Monday to Sunday):
Day Rain Amount
Monday 2,1 mm
Tuesday 10 mm We want to plot these values
Wednesday 9,7 mm
Thursday 6,2 mm
Friday 2,5 mm
Saturday 0 mm
Sunday 8,3 mm
Solution Plotting
Day Rain Amount
Monday 2,1 mm x = [2.1, 10, 9.7, 6.2, 2.5, 0, 8.5]
>> plot(x, 'o')
Tuesday 10 mm
Wednesday 9,7 mm
Thursday 6,2 mm
Friday 2,5 mm
Saturday 0 mm
Sunday 8,3 mm
Plotting
Given the following function (−10 ≤ 𝑥 ≤ 10 ):
!
𝑓 𝑥 = 2𝑥 + 3𝑥 + 1
We will:
• Plot this function
f =
66
Plot of Dynamic System
Given the autonomous system (differential equation):
𝑥̇ = 𝑎𝑥
!
where 𝑎 = − ,where 𝑇 is the time constant
"
The solution for the differential equation is:
𝑥 𝑡 = 𝑒 #$ 𝑥%
Set 𝑇 = 5 and the initial condition 𝑥(0) = 1
→ Create a Script in MATLAB (.m file) where you plot the solution
𝑥(𝑡) in the time interval 0 ≤ 𝑡 ≤ 25
→ Add Grid, and proper Title and Axis Labels to the plot.
clear, clc
%Define Variabes
T=5;
a=-1/T;
%Plotting
plot(t,x);
grid
title('simulation of x'' = ax')
xlabel('time')
ylabel('x')
Sub-plots
Displaying Multiple Plots in one Figure – Sub-Plots
subplot(m,n,p)
% Define x-values
x=0:0.01:2*pi;
% subplot 1
subplot(2,1,1)
plot(x, sin(x))
title('Plotting sin(x)')
xlabel('x')
ylabel('sin(x)')
% Subplot 2
subplot(2,1,2)
plot(x, cos(x))
title('Plotting cos(x)')
xlabel('x')
ylabel('cos(x)')
>> x=0:0.1:2*pi;
Plotting - Subplot
>> y=sin(x);
>> y2=cos(x);
>> y3=tan(x);
>> subplot(3,1,1)
>> x=0:0.1:2*pi; >> plot(x,y)
>> y=sin(x); >> subplot(3,1,2)
>> y2=cos(x); >> plot(x,y2)
>> subplot(3,1,3)
>> subplot(2,1,1) >> plot(x,y3)
>> plot(x,y)
>> x=0:0.1:2*pi;
>> y=sin(x);
>> subplot(2,1,2) >> y2=cos(x);
>> plot(x,y2) >> y3=tan(x);
>> y4=atan(x);
>> subplot(2,2,1)
>> plot(x,y)
>> subplot(2,2,2)
>> plot(x,y2)
>> subplot(2,2,3)
>> plot(x,y3)
>> subplot(2,2,4)
>> plot(x,y4)
Other Plots
• MATLAB offers many different types of plots: loglog, semilogx,
semilogy, plotyy, polar, fplot, fill, area, bar, barh, hist, pie,
errorbar, scatter.
• We will try some of them, e.g., bar, hist and pie.
Check out the help for the different plot functions in MATLAB
We create a bar plot using the bar function:
>> x=rand(10,1)
x =
0.6557
0.0357
0.8491
0.9340
0.6787
0.7577
0.7431
0.3922
0.6555
0.1712
>> bar(x)
Using the hist function gives:
>> x=rand(10,1)
x =
0.6557
0.0357
0.8491
0.9340
0.6787
0.7577
0.7431
0.3922
0.6555
0.1712
>> hist(x)
Using the pie function gives:
>> x=rand(10,1)
x =
0.6557
0.0357
0.8491
0.9340
0.6787
0.7577
0.7431
0.3922
0.6555
0.1712
>> pie(x)
Hans-Petter Halvorsen
University of South-Eastern Norway
www.usn.no
E-mail: [email protected]
Web: https://www.halvorsen.blog