Professional Documents
Culture Documents
2D and 3D PLOTS
MATLAB PLOTTING
Creating Vectors
Create vector with equally spaced intervals
>> x=0:0.5:pi
x =
0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000
Create vector with n equally spaced intervals
>> x=linspace(0, pi, 7)
x =
0 0.5236 1.0472 1.5708 2.0944 2.6180 3.1416
Equal spaced intervals in logarithm space
>> x=logspace(1,2,7)
x =
10.0000 14.6780 21.5443 … 68.1292 100.0000
1 2
10 10
Note: The logspace creates 7 equally distributed values between and
100
Length of the
90
Initial increment
80
Value Final value
70
60
50
40
30
>> x=[0:5:100]; 20
>> y=x; 10
>> plot(x,y)
0 10 20 30 40 50 60 70 80 90 100
100
>> stem(x,y) 90
80
70
60
50
40
NOTE: stem( ) is used to plot 30
discrete sequence data. 20
10
0
0 10 20 30 40 50 60 70 80 90 100
10000
9000
8000
7000
6000
5000
>> x=[‐100:20:100]; 4000
3000
>> y=x^2; 2000
??? Error using ==> mpower 1000
Matrix must be square. 0
-100 -80 -60 -40 -20 0 20 40 60 80 100
>>y=x.^2; 10000
>> plot(x,y) 9000
8000
>> stem(x,y) 7000
6000
5000
4000
3000
2000
1000
0
-100 -80 -60 -40 -20 0 20 40 60 80 100
6
x 10
1
0.8
0.6
0.4
0.2
>> x=[‐100:5:100]; -0.2
-0.4
>> y=x.^3; -0.6
-0.8
>> plot(x,y) -1
-100
6
-80 -60 -40 -20 0 20 40 60 80 100
x 10
>> stem(x,y) 1
0.8
0.6
0.4
0.2
NOTE: 0
-0.2
If the length of the increments -0.4
are small, then curve will be -0.6
more smoother. -0.8
-1
-100 -80 -60 -40 -20 0 20 40 60 80 100
2D PLOTS
To Plot the function sin(x) between 0≤x≤4π
Final value
Initial value No.of increments
0.8
0.6
>> x=linspace(0,4*pi,100); 0.4
>> y=sin(x); 0.2
0
>> plot(y); -0.2
-0.4
-0.6
-0.8
-1
0 10 20 30 40 50 60 70 80 90 100
2D PLOTS
To Plot the function e(‐x/3) between 0≤x≤4π
1
0.9
0.8
>> x1=linspace(0,4*pi,100); 0.7
0.6
>> y1=exp(‐x1/3); 0.5
>>plot(y1);
0.4
0.3
0.2
0.1
0
0 10 20 30 40 50 60 70 80 90 100
To Plot the function cos(x) between 0≤x≤4π
1
0.8
>> a=linspace(0,4*pi,100); 0.6
0.4
>> b=cos(a); 0.2
>> plot(a,b); -0.2
-0.4
-0.6
-0.8
-1
0 2 4 6 8 10 12 14
2D PLOTS
To Plot the function e(‐x/3)sin(x) between 0≤x≤4π
0.7
0.6
0.5
>> z=linspace(0,4*pi,100); 0.4
0.3
>> y1=sin(z); 0.2
0.1
>> y2=exp(‐z/3); 0
-0.1
>>y3=y1*y2; -0.2
-0.3
0 10 20 30 40 50 60 70 80 90 100
??? Error using ==> mtimes
Inner matrix dimensions must 0.7
agree.
0.6
0.5
>>y3=y1.*y2; 0.4
0.3
>>plot(y3); 0.2
0.1
>>stem(y3); 0
-0.1
-0.2
-0.3
0 10 20 30 40 50 60 70 80 90 100
AXIS SCALES
MATLAB COMMANDS
Using M‐file, plotting with title and labels
0.8
x=linspace(0,2*pi,100); 0.6
y=sin(x); 0.4
plot(x,y); 0.2
f(x)-values
title('this is sinus function'); 0
xlabel('x‐values'); -0.2
ylabel('f(x)‐values'); -0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
x-values
MULTIPLE GRAPHES
MULTIPLE GRAPHES
Using M‐file, plotting multiple graphs in a figure window
1
t = 0:pi/100:2*pi;
0.8
0.6
y1=sin(t); 0.4
0.2
y2=sin(t+pi/2); 0
-0.2
NOTE:
plot(t,y1,t,y2) -0.4
-0.6
When we plot
grid on -0.8
-1
multiple graphs,
the command
0 1 2 3 4 5 6 7
t = 0:pi/100:2*pi; 0.8
y1
y2 “legend ( )”
y1=sin(t);
0.6
0.4
is used for
y2=sin(t+pi/2); 0.2
identification of
0
legend('y1','y2') -0.6
grid off -0.8
-1
0 1 2 3 4 5 6 7
SUB PLOTS
SUB PLOTS
Using M‐file, plotting multiple plots by dividing
the figure window
1 1
y1=sin(t); 0 0
-1 -1
subplot(2,2,1) 0 2 4 6 8 0 2 4 6 8
plot(t,y1)
subplot(2,2,2)
plot(t,y2)
SUB PLOTS
SUB PLOTS
clc
clear all SUB PLOTS
x=0:.1:2*pi;
subplot(2,2,1);
plot(x,sin(x),'b*'); 1
sin(x)
1
cos(x)
subplot(2,2,2); 0 0
-1 -1
title('cos(x)') 0 2 4 6 8 0 2 4 6 8
exp(-x) sin(3x)
subplot(2,2,3) 1 1
0.5
plot(x,exp(‐x),'g.'); 0.5 0
title('exp(‐x)') -0.5
subplot(2,2,4); 0
0 2 4 6 8
-1
0 2 4 6 8
plot(x,sin(3*x),'m‐o');
title('sin(3x)')
COLOR PLOTS
2D PLOTS
To plot with color and special symbols
2D PLOTS
2D PLOTS
To plot with color and special symbols
100
80
60
40
x = (‐pi/2)+0.01:0.01:(pi/2)‐0.01; 20
plot(x,tan(x),'g‐*'); 0
-20
grid on -40
-60
-80
-100
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
2D PLOTS
To plot with color and special symbols
This is cosine function
1
0.8
x = ‐pi:0.01:pi; 0.6
plot(x,cos(x),'y‐*'); 0.4
0
function'); -0.2
grid on -0.4
-0.6
-0.8
-1
-4 -3 -2 -1 0 1 2 3 4
2D PLOTS
To draw a circle with centre (1, 3)
clc 5
Circle
clear all 4.5
t = linspace(0, 2*pi, 101); 4
x = 1 +2*cos(t); 3.5
y = 3 +2*sin(t); y-axis
3
plot(x,y,'r.') 2.5
axis equal 2
xlabel('x‐axis') 1.5
ylabel('y‐axis') 1
-1.5 -1 -0.5 0 0.5 1
x-axis
1.5 2 2.5 3 3.5
title('Circle‘)
2D PLOTS
To plot with color and special symbols
1
cos(x)
0.8 cos(2x)
0.6
y = linspace(‐10,10,1000) 0.4
plot(y,cos(y),'b.',y,cos(2*y),'g.') 0.2
y a x is
xlabel('x axis') 0
-0.2
legend('cos(x)','cos(2x)‘) -0.6
-0.8
-1
-10 -8 -6 -4 -2 0 2 4 6 8 10
x axis
Plotting Polynomials
Polynomial function A(s)=S3+3S2+3S+1
coeff = [ 1 3 3 1]; 200
150
plot (s, A)
A(s)
50
title('Polynomial function 0
A(s)=S^3+3S^2+3S+1') -50
ylabel ('A(s)')
Drawing Bar Charts
x = [1:10];
y = [75, 58, 90, 87, 50, 85, 92, 75, 60, 95];
bar(x,y), xlabel('Student'),ylabel('Score'),
title('First Sem‘)
First Sem:
100
90
80
70
60
Score
50
40
30
20
10
0
1 2 3 4 5 6 7 8 9 10
Student
DRAWING CONTOURS
[x,y] = meshgrid(‐5:0.1:5,‐3:0.1:3);
g = x.^2 + y.^2;
contour(x,y,g)
-1
-2
-3
-5 -4 -3 -2 -1 0 1 2 3 4 5
DRAWING CONTOURS
[x,y] = meshgrid(‐5:0.1:5,‐3:0.1:3);
g = x.^2 + y.^2;
[C, h] = contour(x,y,g);
set(h,'ShowText','on','TextStep',get(h,'LevelStep')*2)
3
30
30
20
20
10
10
1
10
10
20
20
0
-1
-2
10
10
20
20
30
30
-3
-5 -4 -3 -2 -1 0 1 2 3 4 5
Plotting with command “ezplot ( )”
x 3 - 27 x
80
60
40
20
>>syms x; 0
>> y=x^3‐27*x; -20
>> ezplot(y) -40
-60
-80
-6 -4 -2 0 2 4 6
x
NOTE: By default, ezplot graphs from x=‐2*pi to x=+2*pi.
You can change the domain by adding a second argument
to the call to ezplot. For example, to plot the above function
from x=2 to x=4 you type:
x 3 - 27 x
-44
-45
-46
-47
-48
-49
ezplot(y,[2,4]) -50
-51
-52
-53
-54
x 3 - 27 x
-40
-42
-44
-46
y=x^3‐27*x; -48
ezplot(y,[2,4]) -50
-52
ylim([‐60,‐40]) -54
-56
-58
-60
2 2.2 2.4 2.6 2.8 3 3.2 3.4 3.6 3.8 4
x
Note that you don’t have to define y first to use ezplot;
you can just type in the function you want to graph:
cos(2 x) cos(x)
0.8
0.6
0.4
0.2
ezplot(cos(x)*cos(2*x)) 0
-0.2
-0.4
-0.6
-0.8
-1
-6 -4 -2 0 2 4 6
x
POLAR PLOTS
90
1
120 60
0.8
0.6
150 30
0.4
>> ezpolar(cos(2*theta)) 180 0
210 330
240 300
270
r = cos(2 θ)
3D PLOTS
t=linspace(0,2*pi,500);
x=cos(t);
y=sin(t);
z=sin(5*t);
xlabel('x‐axis');
ylabel('y‐axis');
zlabel('z‐axis');
title('3D Curve');
comet3(x,y,z);
plot3(x,y,z);
Curves in space
You can use the ”ezplot3” function to graph curves
in space. For example, we can define a helix by the
parametric equations: x = cos(t), y = sin(t), z = t
x = cos(t), y = sin(t), z = t
>> syms t
>> ezplot3(cos(t),sin(t),t,[0,6*pi]) 20
15
10
z
5
0
1
0.5 1
0 0.5
0
-0.5 -0.5
y -1 -1
x
3D PLOTS
[x,y] = meshgrid(‐2:.2:2);
g = x .* exp(‐x.^2 ‐ y.^2);
surf(x, y, g)
0.5
-0.5
2
1 2
0 1
0
-1 -1
-2 -2
3D PLOTS
x=‐1:.05:1;
1
y=‐1:.05:1; 0.5
[x,y]=meshgrid(x,y); 0
z=x.*y.^2‐x.^3 -0.5
surf(x,y,z); -1
1
0.5 1
colormap spring 0
-0.5 -0.5
0
0.5
-1 -1
shading interp
3D PLOTS
2 x2 + 2 y2
syms x y
150
f = 2*(x^2+y^2)
100
ezsurf(f)
colormap cool 50
0
5
5
0
0
-5 -5
y x
3D PLOTS x 2+y 2
200
150
100
50
0
10
5 10
0 5
0
-5 -5
y -10 -10
x
250
200
z 150
100
50
0
20
10 20
0 10
0
-10 -10
y -20 -20
x
SAVE PLOTS