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

plot

Uploaded by

mohamsd elharmil
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)
16 views

plot

Uploaded by

mohamsd elharmil
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/ 39

1

2)fplot function

• The MATLAB function fplot can be used


to plot a function of one variable in a
specified range.

Abdelrahman Mohamed fargly


2

See also
•Function plot
>>fplot(‘function’,[xmin xmax],’property’)
Example :
>>fplot(‘sin’,[0 2*pi],’r:’)

Abdelrahman Mohamed fargly


3

fplot function

• » fplot('sin(x)',[-pi pi])

Abdelrahman Mohamed fargly


4

fplot function

• » fplot('sin(x)',[-pi pi])

0.5

-0.5

-1
-2 0 2 Abdelrahman Mohamed fargly
5

fplot function

• » fplot('exp(-x)*sin(x)',[0 2])

Abdelrahman Mohamed fargly


6

fplot function

• » fplot('exp(-x)*sin(x)',[0 2])

0.35

0.3

0.25

0.2

0.15

0.1

0.05

0
0 0.5 1 1.5 2

Abdelrahman Mohamed fargly


7

Adding a grid
• You can use the command:
• grid on
• to add grid lines to your plot. This
increases the readability of the graph.

Abdelrahman Mohamed fargly


8

Adding a grid
• You can use the command:
• grid on
• to add grid lines to your plot. This
increases the readability of the graph.
• To remove these grid lines, use:
• grid off

Abdelrahman Mohamed fargly


9

Adding a grid
• » fplot('exp(x)-x-2',[-3 3])
• » grid on

Abdelrahman Mohamed fargly


10

Adding a grid
• » fplot('exp(x)-x-2',[-3 3])
• » grid on
20

15

10

-5
-3 -2 -1 0 1 2 3

Abdelrahman Mohamed fargly


11

3D Plotting

Abdelrahman Mohamed fargly


12

plot3 function
• The MATLAB function plot3 is used to
plot space curves.

Abdelrahman Mohamed fargly


13

plot3 function
• The MATLAB function plot3 is used to
plot space curves.

• Space curves are usually represented by


parametric equations:
• x = x(t), y = y(t), z = z(t)

Abdelrahman Mohamed fargly


14

plot3 function
• The MATLAB function plot3 is used to
plot space curves.

• Space curves are usually represented by


parametric equations:
• x = x(t), y = y(t), z = z(t)

• Steps followed and options available for


plot3 is similar to that of plot.

Abdelrahman Mohamed fargly


15

plot3 Example 1
• Example: Plot the line
• x = t + 2, y = t - 2, z = t, 0 < t < 1

Abdelrahman Mohamed fargly


16

plot3 Example 1
• Example: Plot the line
• x = t + 2, y = t - 2, z = t, 0 < t < 1

>> t=0:0.01:1;
>> x=t+2;
>> y=t-2;
>> z=t;
>> plot3 (x,y,z,'r.')
>> grid on

Abdelrahman Mohamed fargly


17

plot3 Example

Abdelrahman Mohamed fargly


18

plot3 Example 2

• Example: Plot the circle


• x = 1, y = 5 cos t, z = 5 sin t, - < t < 

Abdelrahman Mohamed fargly


19

plot3 Example 2

• Example: Plot the circle


• x = 1, y = 5 cos t, z = 5 sin t, - < t < 
» t=-pi:0.01:pi;
» x=ones(length(t),1);
» y=5*cos(t);
» z=5*sin(t);
» plot3(x,y,z,'r.')
» grid on

Abdelrahman Mohamed fargly


20

plot3 Example

Abdelrahman Mohamed fargly


21

plot3 Example 3

• Example: Plot the helix


• x = cos t, y = sin t, z = t, 0 < t < 10 

Abdelrahman Mohamed fargly


22

plot3 Example 3

• Example: Plot the helix


• x = cos t, y = sin t, z = t, 0 < t < 10 

t=0:0.01:10*pi;
x=cos(t);
y=sin(t);
z=t;
plot3(x,y,z,'r.')
grid on

Abdelrahman Mohamed fargly


23

plot3 Example

Abdelrahman Mohamed fargly


24

plot3 function

• The MATLAB function plot3 can be used


also to plot multiple 2D curves in 3D.

Abdelrahman Mohamed fargly


25

plot3 Example 4
• Example: Plot together
• sin x, cos x

Abdelrahman Mohamed fargly


26

plot3 Example 4
• Example: Plot together
• sin x, cos x

x=0:0.01:3*pi;
y1=zeros(size(x));
y2=ones(size(x));
z1=sin(x);
z2=cos(x);
plot3(x,y1,z1,'.r',x,y2,z2,'.g')
grid on

Abdelrahman Mohamed fargly


27

plot3 Example 4

Abdelrahman Mohamed fargly


28

mesh function

• The MATLAB function mesh is used to


plot surfaces.

Abdelrahman Mohamed fargly


29

mesh function

• The MATLAB function mesh is used to


plot surfaces.

• Surfaces are usually represented by a


function of two variables:
• z = f(x, y)

Abdelrahman Mohamed fargly


30

mesh function

• The MATLAB function mesh is used to


plot surfaces.

• Surfaces are usually represented by a


function of two variables:
• z = f(x, y)

• To use this function we have to use


meshgrid first to generate to pairs (x, y)
Abdelrahman Mohamed fargly
31

meshgrid by example
>> x=0:2
x = 0 1 2
>> y=0:2
y = 0 1 2
>> [X,Y]=meshgrid(x,y)

Abdelrahman Mohamed fargly


32

meshgrid by example
>> x=0:2
x = 0 1 2
>> y=0:2
y = 0 1 2
>> [X,Y]=meshgrid(x,y)
X = 0 1 2
0 1 2
0 1 2

Y = 0 0 0
1 1 1
2 2 2

Abdelrahman Mohamed fargly


33

mesh Example 1

• Example: Plot the surface


• z = x2 + y2

Abdelrahman Mohamed fargly


34

mesh Example 1

• Example: Plot the surface


• z = x2 + y2

x=-3:0.01:3;
y=-3:0.01:3;
[X,Y]=meshgrid(x,y);
z=X.^2+Y.^2;
mesh(X,Y,z)

Abdelrahman Mohamed fargly


35

mesh Example 1

Abdelrahman Mohamed fargly


36

mesh Example 2

• Example: Plot the surface


• 7 x - y + z=3

Abdelrahman Mohamed fargly


37

mesh Example 2

• Example: Plot the surface


• 7 x - y + z=3

>> x=-3:0.1:3;
>> y=-3:0.1:3;
>> [X,Y]=meshgrid(x,y);
>> z=3-7*X+Y;
>> mesh(X,Y,z)

Abdelrahman Mohamed fargly


38

mesh Example 2

Abdelrahman Mohamed fargly


39

Other Useful Graphics Commands


You may find the following set of commands useful:

Function Used to

plot3 Create a graph for a 3D data representing a space curve

loglog Create a graph with logarithmic scales for both axes

semilogx Create a graph with logarithmic scale for the x axis only

semilogy Create a graph with logarithmic scale for the y axis only

plotyy Create a graph with y-tick labels on the left and right side

gtext Position text using the mouse

clf Clears the figure

ginput Gather data by clicking on points in the plot

Abdelrahman Mohamed fargly

You might also like