Professional Documents
Culture Documents
تطبيقات الحاسوب Computer Application 6
تطبيقات الحاسوب Computer Application 6
المرحلة الثانية
المحاضرة السادسة
أسم المادة :تطبيقات الحاسوب
Assist. Lecturer Yousif A. Hamad
Basic Plotting عنوان المحاضرة:
Assist. Lecture: Noor A. Kadhum computer Applications
Basic Plotting
Overview
MATLAB has an excellent set of graphic tools. Plotting a given data set or
the results of computation is possible with very few commands. You are
highly encouraged to plot mathematical functions and results of analysis as
often as possible.
1)When looking at plotting we have also seen how to create a row vector with
elements from a to b with n regularly spaced elements using the linspace
command. To review, linspace(a,b) creates a row vector of 100 regularly
spaced elements between a and b, while linspace(a,b,n) creates a row vector
of n regularly spaced elements between a and b. In both cases MATLAB
determines the increment in order to have the correct number of elements.
>> linspace(1,5)
Create 100 elements begin with 1 and with 5.
>> x = linspace(1,5,3)
x= 135
2) MATLAB also allows you to create a row vector of n logarithmically
spaced elements by typing logspace(a,b,n). This creates n regularly spaced
elements between 10𝑎𝑎 and 10𝑏𝑏 . For example:
>> logspace(1,2,5)
ans = 10.0000 17.7828 31.6228 56.2341 100.0000
Or another example:
>> logspace(-1,1,6)
ans = 0.1000 0.2512 0.6310 1.5849 3.9811 10.0000
3) semilogx(x, y) generates a plot with a logarithmic x axis and a rectilinear y
axis.
4) semilogy(x, y) generates a plot with a rectilinear x axis and a logarithmic y
axis.
Page 32 of 55
Assist. Lecture: Noor A. Kadhum computer Applications
Page 33 of 55
Assist. Lecture: Noor A. Kadhum computer Applications
>> x = 0:pi/100:2*pi;
>> y = sin(x);
>> plot(x,y)
• 0:pi/100:2*pi yields a vector that
-starts at 0,
- takes steps (or increments) of π /100,
-stops when 2 π is reached.
• If you omit the increment, MATLAB automatically increments by 1.
Function fplot gets around our choice of interval used to generate the plot, and
instead decides the number of plotting points. Generally, fplot will allow you
to generate the most accurate plots possible. The formal call to fplot goes like
fplot ( 'function string', [xstart, xend]). The argument function string tells
fplot the function you want to plot while xstart and xend define the range over
which to display the plot.
>> fplot('exp(-2*t)*sin(t)',[0, 4]);
Page 34 of 55
Assist. Lecture: Noor A. Kadhum computer Applications
Page 35 of 55
Assist. Lecture: Noor A. Kadhum computer Applications
Page 36 of 55
Assist. Lecture: Noor A. Kadhum computer Applications
Page 37 of 55
Assist. Lecture: Noor A. Kadhum computer Applications
MATLAB can add a grid to the plot. This is done by adding the phrase grid
on to your plot statement.
Example:
Plot y = tanh(x) over the range −6 ≤ x ≤ 6 with a grid display.
>> x = [-6:0.01:6];
>> y = tanh(x);
>>plot(x,y);grid on
Page 38 of 55
Assist. Lecture: Noor A. Kadhum computer Applications
Page 39 of 55
Assist. Lecture: Noor A. Kadhum computer Applications
Figure 7: A glance at the MATLAB output after our first calls to subplot and
plot
>> y = exp(-2*x).*sin(20*x);
>> subplot(1,2,2);
>> plot(x,y);xlabel('x');ylabel('exp(-2x)*sin(20x)');axis([0 5 -1 1])
Page 40 of 55
Assist. Lecture: Noor A. Kadhum computer Applications
You can add plots to an existing graph using the hold command. When you
set hold on, MATLAB does not remove the existing graph; it adds the new
data to the current graph, rescaling if the new data falls outside the range of
the previous axis limits. For example: Plot cos(x) and sin(x) for 100 linearly
spaced points from 0 to 2π.
>> x = linspace(0,2*pi);
>> plot(x,cos(x))
Plot cos(x) and want to overlay sin(x) on the same graphic for 100 linearly
spaced points from 0 to 2π.
>> x = linspace(0,2*pi);
>> plot(x,cos(x));axis([0 2*pi -1 1])
>> hold on
>> plot(x, sin(x)); axis ([0 2*pi -1 1])
Page 42 of 55