26/08/2014

how to have multiple plotting in matlab
Multiple plotting.
One of the most admirable features of MATLAB is its extensive device independent plotting capabilities. They make it very easy to plot any data or expression at any time. Any pair of vectors can be plotted versus each other as long as both vectors have the same length.
Here we will discuss......

How to have:

1-     Continuous Plots/graphs.
2-  Discrete Plots/graphs.
3-  Logarithmic scale Plots/graphs.
4-  Multiple plots/graphs.

 We will discuss each one of the plotting schemes one by one in detail along with respective code and examples.
Except this you will learn how to add legend, xlabel, ylabel and grid in your plot.Sorry to say that I am not gonna explain it here but by just visiting the code under each plot you will learn how to use them.

   Continuous Plots/graphs:

Continuous plots are those plots that are so smooth that the human eye is not able to find any space between any two points.
  General function for this purpose is: plot(vect1,vect2)
 Where vect1 and vect2 are the variable names of two vectors to be plotted versus each other. vect1 will define the x-axis and vect2 will define the y-axis in MATLAB. Please note that these variable names are not
fix and final.You can select variable names of your own choice. 
As an example of this illustration visit the given below figure for further assistance.
how to plot continuous plots using matlab.
An example of continuous plot.


MALAB code of above given plot:
clc
clear all
x=1:0.01:10;
y=sin(x.^2)./x;
plot(x,y,'m')
xlabel('x-axis')
ylabel('y-axis')
legend('Continuous plot from "Matlabforeng.blogspot.com"')


   Discrete Plots/graphs:

      Discrete plots are not so smooth, like continuous plots, they have discontinuity at certain points that can be detected by human eye.
  General function for this purpose is: stem(vect1,vect2)
Where vect1 and vect2 are the two vectors to be plotted versus each other. vect1 will define the x-axis and vect2 will define the y-axis in MATLAB.
As an example of this illustration visit the given below figure for further assistance on how to plot a discrete graph using MATLAB.

how to have a discrete plot in matlab
An example of discrete plot.
Matlab code of above given plot:
clc
clear all
x=1:20;
y=x.^2;
stem(x,y,'*m:')
xlabel('x-axis')
ylabel('y-axis')
legend('discrete plot from "Matlabforeng.blogspot.com"')


   Logarithmic scale plots:

It is possible to plot data in logarithmic scale as well as in linear scale in MATLAB. Logarithmic graphs are used to study large functions.There are 3 possible combinations for this purpose.Each combination is produced by a separate function.
All general functions for this purpose are given and discussed below.

1- semilogx(x,y):
 semilogx(x,y) function plots x data in logarithmic scale on x-axis and y data in linear scale on y-axis.
Where x and y are just names of variables.You can use any name as a variable.
 For further assistance on how to have logarithmic scale plots using MATLAB.
Visit:
How to have Logarithmic plot with x-axis in logarithmic scale using matlab
Logarithmic plot with x-axis in logarithmic scale.


You can see in the above given MATLAB's plot that x-axis is in logarithmic scale while y-axis is in linear scale.
MATLAB code of above given plot: 
clc
clear all
x=1:0.01:100;
y=sin(x.^2)./x;
semilogx(x,y,'m')
xlabel('x-axis')
ylabel('y-axis')
legend('Logarithmic scale plot from "Matlabforeng.blogspot.com"')
grid on

2-semilogy(x,y):
semilogy(x,y) is also a MATLAB's unique function to plot logarithmic graphs.This function plots y data/vector in logrithmic scale on y-axis but x data/vector in linear scale on x-axis.Where x and y are variable names that MATLAB uses to store and represent vectors and values.Please note that these variable names are not fix and final.You can select variable names of your own choise. 
For further assistance on how to have logarithmic scale plots using MATLAB.
Visit:
How to have a Logarithmic scale plot with logarithmic scale on y-axis using matlab.
Logarithmic scale plot with logarithmic scale on y-axis.


You can see in the above given MATLAB's plot that y-axis is in logarithmic scale while x-axis is in linear scale.
MATLAB code of above given plot: 
clc
clear all
x=1:0.01:100;
y=sin(x.^2)./x;
semilogy(x,y,'m')
xlabel('x-axis')
ylabel('y-axis')
legend('Logarithmic scale plot from "Matlabforeng.blogspot.com"')
grid on


3-loglog(x,y):
loglog(x,y) function is another MATLAB's unique function that enables you to examine large values.This function plots both x and y data in logarithmic scale on both x-axis and y-axis.
For further assistance on "how to have logarithmic scale plots with both axis is logarithmic scale using MATLAB".
Visit:
How to plot logarithmic graph with both axis in logarithmic scale
Logarithmic plot with both axis in logarithmic scale.





You can see in the above given MATLAB's plot that both axis are in logarithmic scale.
In legend I mistakenly wrote discrete plot, I confirm here that above given plot is also logarithmic scaled plot. 
MATLAB code of above given plot: 
clc
clear all
x=1:0.01:100;
y=sin(x.^2)./x;
loglog(x,y,'m')
xlabel('x-axis')
ylabel('y-axis')
legend('discrete plot from "Matlabforeng.blogspot.com"')
grid on
  Multiple plots/graphs:
Multiple plotting is a great feature, given to MATLAB users, a strong technique that enables us to plot more then 1 graph/plot on the same window.
General functions for this purpose are:
  1-plot(x1,y1,x2,y2,..),semilogx(x1,y1,x2,y2,..),semilogy(x1,y1,x2,y2,..),loglog(x1,y1,x2,y2,..).
         
  2-Hold on

We will discuss each one of them,

   plot(x1,y1,x2,y2,..),semilogx(x1,y1,x2,y2,..),semilogy(x1,y1,x2,y2,..),loglog(x1,y1,x2,y2,..):

I used to write all 4 functions in 1 heading because procedure to use them is same just their working is different.Where x1,y1,x2 and y2 are variables names of vectors whose graph you want to plot.First subscript defines x-axis while 2nd subscript defines y-axis and so on.With plot(x1,y1,x2,y2,..)
function you can only plot continuous graphs But with  semilogx(x1,y1,x2,y2,..),
semilogy(x1,y1,x2,y2,..) and loglog(x1,y1,x2,y2,..) you can plot logarithmic plots according to your demand, I am recalling here them again to show you that they can also plot multiple plots in 1 window.To show this I will provide you a picture and also a code that will yield that graph or picture.But all these functions are limited to plot the graphs of same category not a combination of different ones.
To plot any category of graph on the same window consider hold on command.
Note that we don't need to provide different colours to different vactors as MATLAB will automatically do this for us
To short my article I am explaining here how to use plot(x1,y1,x2,y2,..) and semilogy(x1,y1,x2,y2,..) functions try other two by your self, procedure to use them is same.
 For further assistance consider the given below graph and code:
how to plot Multiple continuous plots using matlab.
Multiple continuous plots.


MATLAB code for above plot:
clc
clear all
x=1:0.01:10;
y=100.*sin(x.^2)./x;
z=x.^2;
w=100.*cos(x);
plot(x,y,x,z,x,w)
xlabel('x-axis')
ylabel('y-axis')
legend('Multiple continuous plots from "Matlabforeng.blogspot.com"')
grid on
In case of semilogy(x1,y1,x2,y2,..)
how to plot multiple semilogarithmic plot using matlab
Multiple semi-logarithmic plot using matlab

MATLAB code:
clc
clear all
x=1:0.01:10;
y=100.*sin(x.^2)./x;
z=x.^2;
w=100.*cos(x);
semilogy(x,y,x,z,x,w)
xlabel('x-axis')
ylabel('y-axis')
legend('semilogarithmic plots from "Matlabforeng.blogspot.com"')
grid on
  Hold on
This is a strong command it has the power to plot more then one graphs of different categories in just 1 window ,other functions plot multiple plots of its own category,that's why it's different from other MATLAB functions.
As an example a plot along with code is shown below that includes continuous plot and discrete plot.
how to plot Continuous and discrete plot in one window using hold on command in matlab.
Continuous and discrete plot using hold on command. 
MATLAB code:
clc
clear all
x=1:1:50;
z=x.^2;
w=100.*cos(x);
stem(x,w,'r*')
hold on
plot(x,z,'g')
legend('discrete plot from "Matlabforeng.blogspot.com"','continuous plot from "Matlabforeng.blogspot.com"')
xlabel('x-axis')
ylabel('y-axis')
grid on
Please note that you could not use stem(x,y) function to have discrete multiple plots.You can just use hold on command for this purpose. 

0 comments:

Post a Comment

Blog Archive

Popular Posts