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

Matlab Plotting

matlab notes
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)
21 views

Matlab Plotting

matlab notes
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/ 12

2022-05-12

Matlab Plotting

Instructor: Dr. W. E. Ngongi

Plotting
• To plot the graph of a function, you need to take
the following steps:
• Define x, by specifying the range of values for the
variable x, for which the function is to be plotted
• Define the function, y = f(x)
• Use the plot command, as plot(x, y)
• Let us plot the simple function y = x for the range
of values for x from 0 to 100, with an increment
of 5.

1
2022-05-12

Plotting …
• Create a script file and type the following
code:
• x = [0:5:100];
• y = x;
• plot(x, y)
• When you run the file, MATLAB displays the
following plot −

Plotting …

2
2022-05-12

Plotting …
• Example to plot the function y = x2.
• Create a script file and type the following code:
• Create a script file and type the following code −
x = [1 2 3 4 5 6 7 8 9 10];
x = [-100:20:100];
x = [-100:5:100];
• y = x.^2;
• plot(x, y)

Plotting …

3
2022-05-12

Plotting …

Adding Title, Labels, Grid Lines and


Scaling on the Graph
• MATLAB allows you to add title, labels along the x-axis and
y-axis, grid lines and also to adjust the axes to spruce up
the graph.
• The xlabel and ylabel commands generate labels along x-
axis and y-axis.
• The title command allows you to put a title on the graph.
• The grid on command allows you to put the grid lines on
the graph.
• The axis equal command allows generating the plot with
the same scale factors and the spaces on both axes.
• The axis square command generates a square plot.

4
2022-05-12

Plotting Titles & Labels


• To put a title and label the axes, we use:
• title('Graph of y = sin(3pi x)')
• >> xlabel('x axis')
• >> ylabel('y-axis')

Adding Title, Labels, Grid Lines and


Scaling on the Graph …
• Example: Plot a graph of y=sin(x) with 0≤x≤10;
• Codes:
x = [0:0.01:10];
y = sin(x);
plot(x, y),
xlabel('x'),
ylabel('Sin(x)'),
title('Sin(x) Graph'),
grid on,
axis equal

5
2022-05-12

Adding Title, Labels, Grid Lines and


Scaling on the Graph …

Line Styles & Colours


Code Color
w White
k Black
b Blue
r Red
c Cyan
g Green
m Magenta
y Yellow

6
2022-05-12

Line Styles & Colours …


• The default is to plot solid lines. A solid white
line is
• produced by
• >> plot(x,y,'w-')
• The third argument is a string whose first
character specifies the colour (optional) and
the second the line style.
• The options for colours and styles are:

Line Styles & Colours …


. Point
o circle
x x-mark
+ plus
- solid
* star
: dotted
-. dashdot
-- dashed

7
2022-05-12

Multplots
• You can draw multiple graphs on the same plot.
• >> plot(x, y, 'w-', x, cos(3*pi*x),'g--')
• Example: Plot y = sin(x) and z = cos(x) for 0≤x≤10.
• Codes:
x = [0 : 0.01: 10];
y = sin(x);
z = cos(x);
• plot(x, y, x, z, '.-'), legend('Sin(x)', 'Cos(x)')

Drawing Multiple Functions on the


Same Graph …

8
2022-05-12

Setting Axis Scales


• The axis command allows you to set the axis
scales.
• Example: Plot y =e-xsin(2x + 3);
• Codes:
• x = [0 : 0.01: 10];
• y = exp(-x).* sin(2*x + 3);
• plot(x, y), axis([0 10 -1 1])
• When you run the file, MATLAB generates the
following graph:

Setting Axis Scales …

9
2022-05-12

Subplot
• The graphics window may be split into an m by n
array of smaller windows into which we may plot
one or more graphs.
• The windows are counted 1 to mn row-wise,
starting from the top left. The following
command is used:
• >> subplot(221) or subplot(2,2,1);
• This species that the window should be split into
a 2 by2 array and we select the first subwindow.

Subplot …
• Example
• Let us generate two plots:
y = e−1.5xsin(10x)
y = e−2xsin(10x)
• Create a script file and type the following code:
x = [0:0.01:5];
y = exp(-1.5*x).*sin(10*x);
subplot(1,2,1)
plot(x,y), xlabel('x'),ylabel('exp(–1.5x)*sin(10x)'),axis([0 5 -1 1])
y = exp(-2*x).*sin(10*x);
subplot(1,2,2)
plot(x,y),xlabel('x'),ylabel('exp(–2x)*sin(10x)'),axis([0 5 -1 1])

10
2022-05-12

Subplot …

• Example: Draw graphs of the functions:


• 𝑦 = 𝑠𝑖𝑛𝑥
𝑥

• 𝑢 = (𝑥−1)
1
2 +𝑥
2
• 𝑣 = 𝑥𝑥2+1
−4
1 3
• 𝑤 = (10−𝑥)
(4−𝑥 ) 2
2 1
−2
for 0≤x≤10.

11
2022-05-12

Subplot …
• >> x = 0:0.1:10;
• >> y = sin(x)./x;
• >> subplot(221), plot(x,y), title('(i)')
• >> u = 1./(x-1).^2 + x;
• >> subplot(222),plot(x,u), title('(ii)')
• >> v = (x.^2+1)./(x.^2-4);
• >> subplot(223),plot(x,v),title('(iii)')
• >> w = ((10-x).^(1/3)-1)./sqrt(4-x.^2);
• >> subplot(224),plot(x,w),title('(iv)')

• Tabulate the functions:


• 𝑦 = 𝑥 2 + 3 𝑠𝑖𝑛𝜋𝑥 2 and
𝑠𝑖𝑛2 𝜋𝑥
• 𝑧= For x=0, 0.2, …, 10.
𝑥 2 +3
𝑥2 +3 𝑠𝑖𝑛𝜋𝑥2 𝑠𝑖𝑛2 𝜋𝑥
• hence, tabulate: 𝑤 = (𝑥−2 +3)

• Plot a graph of w over the range 0≤ x≤10.

12

You might also like