Professional Documents
Culture Documents
Plotting: Week 1
Plotting: Week 1
WEEK 1
MATLAB
Plotting graph using given points
clc;
clear;
x = [0 1 1 2 3 3 4]
y = [0 0 2 3 2 0 0]
plot(x,y)
text(x,y,'graph')
text(x,y,'grap
h') labels
every point
with label
'graph'
Ezplot() function
clc;
clear;
ezplot('sin(x)')
If we don’t want to use hold on plot both function at the same time.
Triangle in graphs
Use ^
Use >
Use <
Use v
Defining boundaries
Using axis([]) function
clc;
clear;
x = -pi:0.1:pi
y = sin(x)
plot(x,y,':or')
axis([0,pi,0,1])
clc;
clear;
x = -pi:0.1:pi
y = sin(x)
plot(x,y,':or')
xlim([0,pi])
ylim([0,1])
Subplots
This is a function that allows multiple plots on one tab
clc;
clear;
x = -2:0.1:2
subplot(1,3,1)
plot(x,x.^2)
xlabel('X')
ylabel('Y')
title('Y=x^2')
legend('x^2')
subplot(1,3,2)
plot(x,x.^2)
subplot(1,3,3)
plot(x,x.^4)
if we want to have xlabel and ylabel for the other 2 graphs, we need to include x
and ylabel under every plot function
3D plots
Plot(x,y,z)
Comet3(x,z)
Meshgrid()
Surf
Plot3(x,y,z) function
this creates a 3D drawing.
clc;
clear;
t = 0:pi/10:10*pi
x = exp(-0.02*t).*sin(t)
y = exp(-0.02*t).*cos(t)
plot3(x,y,t)
Comet3(x,y,z)
This creates a 3D animation
clc;
clear;
t = 0:pi/10:10*pi
x = exp(-0.02*t).*sin(t)
y = exp(-0.02*t).*cos(t)
comet3(x,y,t)
Meshgrid(x,y)
Creates a 3D mesh of points
clc;
clear;
x = -4:0.1:4
y = -4:0.1:4
[a,b] = meshgrid(x,y)
z = a.^2+b.^2
mesh(x,y,z)
*You can do mesh(z), since x and y are*
Meshc(x,y,z)
The c in meshc(x,y,z) stands for contour.
clc;
clear;
x = -4:0.1:4
y = -4:0.1:4
[a,b] = meshgrid(x,y)
z = a.^2+b.^2
meshc(x,y,z)
Surf(x,y,z)
This creates a 3D surface
clc;
clear;
x = -4:0.1:4
y = -4:0.1:4
[a,b] = meshgrid(x,y)
z = a.^2+b.^2
surf(x,y,z)
surfc(x,y,z)
the c in surfc(x,y,z) stands for contour
clc;
clear;
x = -4:0.1:4
y = -4:0.1:4
[a,b] = meshgrid(x,y)
z = a.^2+b.^2
surfc(x,y,z)
Contour(x,y,z)
Makes of a 2D contour of a mesh
clc;
clear;
x = -4:0.1:4
y = -4:0.1:4
[a,b] = meshgrid(x,y)
z = a.^2+b.^2
contour(x,y,z)
contour3(x,y,z)
Creates the contour of the mesh, but it is 3D
clc;
clear;
x = -4:0.1:4
y = -4:0.1:4
[a,b] = meshgrid(x,y)
z = a.^2+b.^2
contour3(x,y,z)
View function
Adjusts the viewing angle of the 3D models
clc;
clear;
[x,y] = meshgrid(-3:0.3:3);
z = x .* exp(-x .^2 - y .^2);
subplot(2,2,1);
mesh(z),title('Subplot(2,2,1)')
view(-37.5,90)
subplot(2,2,2)
mesh(z)
view(-37.5,70),title('Subplot(2,2,2)')
subplot(2,2,3)
mesh(z)
view(37.5,-10),title('Subplot(2,2,3)')
subplot(2,2,4)
mesh(z)
view(0,0),title('Subplot(2,2,4)')
Fill()
Fills everthing with a specified colour
clc;
clear;
x = 0:0.1:pi
fill(x,sin(x),'r')
Bar()