0% found this document useful (0 votes)
37 views

MATLAB Examples - Plotting

The document discusses various plotting functions in MATLAB for creating line plots, bar plots, histograms, pie charts, and subplots. It provides examples of how to plot simple functions like sine and cosine waves, plot rain data, and simulate solutions to differential equations. The plotting functions covered include plot, bar, hist, pie, subplot, and others.

Uploaded by

Ndalah'wa Witaro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

MATLAB Examples - Plotting

The document discusses various plotting functions in MATLAB for creating line plots, bar plots, histograms, pie charts, and subplots. It provides examples of how to plot simple functions like sine and cosine waves, plot rain data, and simulate solutions to differential equations. The plotting functions covered include plot, bar, hist, pie, subplot, and others.

Uploaded by

Ndalah'wa Witaro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

https://www.halvorsen.

blog

Plotting with MATLAB

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

• Use the Plot to find out:


- For which value of 𝑥 is 𝑓(𝑥) = 0?
- What is 𝑓(5) =?
>> x = -10:10;
>> f = 2*x.^2 + 3*x + 1;
>> plot(x,f)
>> grid on
𝑓(5) =?
𝑓(5) =?
x = 5;

>> f=2*x.^2 + 3*x + 1

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;

%Start Condition, etc


x0=1;
t=[0:1:25]

%Define the function


x=exp(a*t)*x0;

%Plotting
plot(t,x);
grid
title('simulation of x'' = ax')
xlabel('time')
ylabel('x')
Sub-plots
Displaying Multiple Plots in one Figure – Sub-Plots

We will in this example:


• Plot Sin(x) and Cos(x) in 2 different subplots.
• Add Titles and Labels.

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

You might also like