Matlab Plotting
Matlab Plotting
Matlab Plotting
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 …
4
2022-05-12
5
2022-05-12
6
2022-05-12
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)')
8
2022-05-12
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 …
• 𝑢 = (𝑥−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)')
12