You are on page 1of 18

{How to plot 1-D ,2-D and 3-D in matlab}

Department : Chemical engineering

Faculty of engineering
Soran university
Academic year 2019-2020

Introduction

MATLAB has an excellent set of graphic tools. Plotting a given data set or the results of
computation is possible with very few commands. Creating plots of data sets and functions is

1|Page
very useful for engineers and scientists. You can use plots to present results for papers and
presentations or

[-9iawufihawkg.na, XCJb vkns ,kxhv adivjc

Plot-manipulations

2|Page
Basic 1-D plot :

A = [1.0,4.0,16.0,32.0];
x = [1,2,3,4];
clf; % Clear the plot.
plot(x,A);

3|Page
A = [1.0,4.0,16.0,32.0];
x = [1,2,3,4];
clf;
plot(x,A);
grid on;
xlabel('Time [seconds]');
ylabel('Height [meters]');
title('Experiment 1 results');

A = [1.0,4.0,16.0,32.0];

4|Page
x = [1,2,3,4];
clf;
plot(x,A);
% Label x-position labels
% at 1, 2, 3, and 4.
set(gca,'XTick',[1:4]);
grid on;
xlabel('Time [seconds]');
ylabel('Height [meters]');
title('Experiment 1 results');

Basic 2-D plot :


The basic command for producing a simple 2-D plot is
plot(x values, y values, ‘style_color_marker’) .

For Example:

Let’s create 2D line plot for y=sin(x) where x ranges from 0 to 2*pi:

>x=0:pi/100:2*pi

>y=sin(x)

>plot(x,y)
5|Page
Output:

For example :

> x=0:pi/100:2*pi;

y=sin(x);

%Create the graph with labeling x axis as ‘x-axis’, ‘y’ axis as

‘y-axis’

%with title 'Graph customization' and makes the grid for both the

axis

%visible

plot(x, y), xlabel('x-axis'), ylabel('y-axis'), title('Graph

6|Page
customisation'),

grid on

Output:

7|Page
 Presenting multiple functions on the same graph:

MATLAB has extended features to plot multiple functions within one single graph.

Code: The code is written to represent the functions y1 and y2 in one single graph

x = [0 :pi/10: 10];

y1 = sin(x);

y2 = cos(x);

plot(x, y1, x, y2, '.-'), legend('Sin(x)', 'Cos(x)')

Output:

8|Page
Basic 3-D plot :

Plot 3-D Helix

t = 0:pi/50:10*pi;
st = sin(t);
ct = cos(t);
plot3(st,ct,t)

output :

9|Page
Plot Points as Markers Without Lines :

t = 0:pi/20:10*pi;
xt = sin(t);
yt = cos(t);
plot3(xt,yt,t,'-o','Color','b','MarkerSize',10,'MarkerFaceColor','#D9FFFF')

output :

10 | P a g e
Plot Surface :
Create a 2-D grid with uniformly spaced x-coordinates and y-coordinates in the interval [-2,2].
x = -2:0.25:2;
y = x;
[X,Y] = meshgrid(x);
F = X.*exp(-X.^2-Y.^2);
surf(X,Y,F)

output:

11 | P a g e
Basic 3D Surface Example using SURF
xd=linspace(-1,1);

yd=linspace(0,2*pi);

[x,y]=meshgrid(xd,yd);

z=x.*cos(y);

surf(x,y,z)

12 | P a g e
output :

Using MESH
xd=linspace(-1,1);

yd=linspace(0,2*pi);

[x,y]=meshgrid(xd,yd);

z=x.*cos(y);

surf(x,y,z);

13 | P a g e
mesh(x,y,z)

output :

Adding axes labels, titles


xd=linspace(-1,1);

yd=linspace(0,2*pi);

[x,y]=meshgrid(xd,yd);

z=x.*cos(y);

surf(x,y,z);

14 | P a g e
mesh(x,y,z)

xlabel('x'),ylabel('y'),zlabel('z')
title('3D Plot Example')

output :

Another 3D Surface Example


u=linspace(0.75,1.25,51);

v=linspace(-1.25,-0.75,51);

[x,y]=meshgrid(u,v);

z1=y.*exp(x.^2);

mesh(x,y,z1)

15 | P a g e
xlabel('x'),ylabel('y'),zlabel('z')
title('3D Example with different domains')
hold on
z2=x.^2./y;
mesh(x,y,z2)
hold off

Conclusion

2D plot in MATLAB enables a user to visualize the data which helps for further data
processing. It helps to generate the graphs programmatically. Thus it makes the process of
comparing data points, tracking changes in data over time, pattern in data distribution fast and
easy. we can say that this sofware offers strong possibilities. The utilisation is pretty easy for
simple problem. For advanced problem, it is more complex. Moreover, you can access to
introduction course on internet. But for advanced functions, it is more complex for find books
or article. Moreover, there is some books and user manual but there are paying and expensive.
There is a lot of similarities with Matlab. The graphic interface on scilab is less advanced : it is
16 | P a g e
a simple interface, less developed than Matlab. For example, computation on matrix have
similar syntax. Nevertheless, some functions proposed in scilab are limited or complex for a
beginner. For example, the matlab to scilab translator is not very efficient and require strong
skills of matlab. Some functions such as graphics or data interfacing are complex at the
beginning.

Reference :
I. https://www.educba.com/2d-plots-in-matlab/
II. https://www.ludu.co/course/introduction-to-matlab/plotting
III. https://electrosome.com/introduction-to-2d-plotting-in-matlab/

IV. https://ch.mathworks.com/help/matlab/ref/plot3.html

V. http://math.loyola.edu/~loberbro/matlab/html/Plot3Dsurfaces.html

17 | P a g e
VI. http://hmf.enseeiht.fr/travaux/CD0102/travaux/optmfn/micp/reports/s17slie/index.htm

VII. http://computingforscientists.info/1D_Plotting

18 | P a g e

You might also like