Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

Plotting

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'

Figure 1 Graph with points labeled using text function

Ezplot() function
clc;
clear;
ezplot('sin(x)')

Figure 2 Graph plotted using ezplot, interval of -6 to 6


ezplot has a predetermined range of -6 to 6
Plotting 2 graphs at once
Using hold on function
clc;
clear;
ezplot('sin(x)')
hold on
ezplot('cos(x)')

Figure 3 two plots in one graph using hold on function


hold on allows matlab to plot another line to the same graph
Plotting both at the same time
x = -pi:0.1:pi
y1 = sin(x)
y2 = cos(x)
plot(x,y1,x,y2)
plotyy(x,y1,x,y2)

Figure 4 two plots plotted in one graph by plotting both at once

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

Figure 5 graph before we define boundaries

clc;
clear;
x = -pi:0.1:pi
y = sin(x)
plot(x,y,':or')
axis([0,pi,0,1])

Figure 6 graphs after defining boundaries


Using xlim and ylim
*It is the same as using axis, but x and y are defined separately*

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

Figure 7 three individual plots including one legend

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()

Fills inside the graph with bars


clc;
clear;
x = 0:0.1:pi
bar(x,sin(x),'r')

You might also like