How to plot a 2D function in MATLAB? Basic steps for MATLAB plotting

You can easily plot different functions using MATLAB. To plot a function in MATLAB you must first choose an independent variable. Let that be ‘x’. This variable will form an axis of the plot, more likely the horizontal or x-axis. The vertical component of the plot is then found by using the function of this independent variable. Each point in the plot has a vertical and horizontal component. Firstly, the independent variable and its range must be defined. At the same time the number of points to be used in the plot can also be declared.
For example we are going to plot 'Sine' function and 'Sinc' function. The program is self explanatory. We added some labels in the plotted graph to make clear the programming steps. If you have any doubt feel free to contact us through the comment box below.

Program for 2D Plot

clear all;
clc;
close all;
set(0,'defaultlinelinewidth',2);%Intensity of lines
t=-100:.005:100; %Time Samples from -100 to 100 with an interval of 0.005
s=sin(t); %Assigning Sin(x) to a variable 's'
subplot(2,1,1); %The ploting frame divided in to 2 rows and this fig appear at first
plot(t,s,'R'); %Plotting 't' on X-Axis Vs 's' on Y-Axis,'R' indicate the Colour 'RED'
title('Plot of Sine Function');%Title of the plot
xlabel('Time'); %Name of X-Axis
ylabel('Amplitude');%Name of Y-Axis
axis([0 20 -2 2]); %X-Axis varies from 0 to 20 and Y-Axis from -2 to 2
grid on; %Enable Grid lines
z=2*sin(t)./t; %Assigning Sin(x)/x to a variable 'z'
subplot(2,1,2); %To ploting frame divided in to 2 rows and this fig appear at second
plot(t,z,'B'); %Plotting 't' on X-Axis Vs 'z' on Y-Axis,'B' indicate the Colour 'BLUE'
title('Plot of Sinc Function');%Title of the plot
xlabel('Time'); %Name of X-Axis
ylabel('Amplitude');%Name of Y-Axis
axis([-20 20 -1 3]);%X-Axis varies from -20 to 20 and Y-Axis from -1 to 3
grid on; %Enable Grid lines

Plotted output

Click on the image for enlarged view

If you have any doubts about how to execute the program, read 'Getting Started Guide'
Ads


0 comments:

Post a Comment

 

Copyright © 2011 CircuitsGallery| ToS | Privacy Policy |

Contact Us | Write For Us | Advertise on Circuits Gallery | About Us