0% found this document useful (0 votes)
72 views17 pages

How To Plot 1-D, 2-D and 3-D in Matlab

This document provides an overview of how to create various plots in MATLAB, including 1D, 2D, and 3D plots. It discusses how to create basic line, surface, and 3D plots; plot multiple functions on the same graph; customize plots with labels, titles, and grids; and provides examples of code to generate different plot types. The document serves as an introduction for students learning how to visually represent and analyze data using MATLAB's plotting tools.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views17 pages

How To Plot 1-D, 2-D and 3-D in Matlab

This document provides an overview of how to create various plots in MATLAB, including 1D, 2D, and 3D plots. It discusses how to create basic line, surface, and 3D plots; plot multiple functions on the same graph; customize plots with labels, titles, and grids; and provides examples of code to generate different plot types. The document serves as an introduction for students learning how to visually represent and analyze data using MATLAB's plotting tools.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

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

Student name: Aram Nasih Muhammad

Class : 3rd stage

Course title: programing matlab

Department : Chemical engineering

Faculty of engineering

Soran university

Academic year 2019-2020

1|Page
Contents
Introduction..............................................................................................................................................................3
Plot-manipulations...................................................................................................................................................3
Basic 1-D plot :........................................................................................................................................................4
Basic 2-D plot :........................................................................................................................................................6
Presenting multiple functions on the same graph:...................................................................................................8
Basic 3-D plot :........................................................................................................................................................9
Plot Points as Markers Without Lines :.................................................................................................................10
Plot Surface :..........................................................................................................................................................11
Basic 3D Surface Example using SURF...............................................................................................................12
Using MESH..........................................................................................................................................................13
Adding axes labels, titles.......................................................................................................................................14
Another 3D Surface Example................................................................................................................................15
Conclusion.............................................................................................................................................................16

2|Page
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
very useful for engineers and scientists. You can use plots to present results for papers and
presentations or to visually search for approximate solutions to problems. You are highly
encouraged to plot mathematical functions and results of analysis as often as possible. Trying to
understand mathematical equations with graphics is an enjoyable and very efficient way of
learning mathematics. Being able to plot mathematical functions and data freely is the most
important step, and this section is written to assist you to do just that.

Plot-manipulations

3|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);

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');

4|Page
A = [1.0,4.0,16.0,32.0];

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');

5|Page
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)

Output:

6|Page
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
customisation'),
grid on

7|Page
Output:

 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)

output :

12 | P a g e
Using MESH
xd=linspace(-1,1);

yd=linspace(0,2*pi);

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

z=x.*cos(y);

surf(x,y,z);

mesh(x,y,z)

output :

13 | P a g e
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);

mesh(x,y,z)

xlabel('x'),ylabel('y'),zlabel('z')

title('3D Plot Example')

output :

14 | P a g e
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)

xlabel('x'),ylabel('y'),zlabel('z')

title('3D Example with different domains')

hold on

z2=x.^2./y;

mesh(x,y,z2)

hold off

15 | P a g e
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
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.

16 | P a g e
Reference :

https://www.educba.com/2d-plots-in-matlab/

https://www.ludu.co/course/introduction-to-matlab/plotting

https://electrosome.com/introduction-to-2d-plotting-in-matlab/

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

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

http://hmf.enseeiht.fr/travaux/CD0102/travaux/optmfn/micp/reports/s17slie/index.htm
http://computingforscientists.info/1D_Plotting

 
 
 

17 | P a g e

You might also like