# Blog Arşivleri

## Basic plotting functions on MATLAB

Hi, at my this post i will show some example of plotting signal on matlab.

First of all i want to give brief explanation.

“MATLAB is a high-level language and interactive environment that enables you to perform computationally

intensive tasks faster than with traditional programming languages such as C, C++, and Fortran. ” (1)

“Key Features

High-level language for technical computing

Development environment for managing code, files, and data

Interactive tools for iterative exploration, design, and problem solving

Mathematical functions for linear algebra, statistics, Fourier analysis, filtering, optimization, and numerical integration

2-D and 3-D graphics functions for visualizing data

Tools for building custom graphical user interfaces

Functions for integrating MATLAB based algorithms with external applications and languages,

such as C, C++, Fortran, Java, COM, and Microsoft® Excel®” (1)

At the beginning i will show you , how to draw a sine function.

1) Plot

x = -pi:0.01:pi; % we made fractions as 0.01 between -pi and pi figure(1)&amp;nbsp;&amp;nbsp; %creates figure graphs plot(x,sin(x)), grid on % draws a continues 2D graph and shows the grid. title('sine function that between from -pi to pi') % gives a name at just above the graph figure(2) %creates a second figure graphs plot(sin(x),'r'), grid on % difference of this, it creates a cont. red graph at values of sin(x). title('sine function that between from 0 to 629') xlabel('Radians'); % we can name the axis of graph ylabel('Magnitude');

2) Subplot

Upper code gave us two seperate graphs but if we use subplot command we can show these at same pattern.

x = -pi:0.01:pi; % we made fractions as 0.01 between -pi and pi figure(1)&amp;nbsp;&amp;nbsp; %creates figure graphs subplot(2,1,1);plot(x,sin(x)), grid on % draws a continues 2D graph and shows the grid. title('sine function that between from -pi to pi') % gives a name at just above the graph subplot(2,1,2);plot(sin(x),'r'), grid on % difference of this, it creates a cont. red graph at values of sin(x). title('sine function that between from 0 to 629') xlabel('Radians'); % we can name the axis of graph ylabel('Magnitude');

3) Manipulating the graph

As we learned at the signals an systems courses, let’s manipulate the our signal.

&lt;/pre&gt; x = 0:0.01:2*pi; % we made fractions as 0.01 between -pi and pi figure(1) %creates figure graphs subplot(2,3,1);plot(x,sin(x)), grid on % my original sin wave. title('my original sin wave') % gives a name at just above the graph xlabel('Radians'); ylabel('Magnitude'); subplot(2,3,2);plot(x,sin(x-pi/2)), grid on % signal is shifted by 90 degrees. title('signal is shifted by 90 degrees') xlabel('Radians'); ylabel('Magnitude'); subplot(2,3,3);plot(x,2*sin(x)), grid on % Double magnitude. title('Double magnitude') % gives a name at just above the graph xlabel('Radians'); ylabel('Magnitude'); subplot(2,3,4);plot(x,(-1*sin(x))), grid on % inversed by horizontal axis. title('inversed by horizontal axis') xlabel('Radians'); ylabel('Magnitude'); subplot(2,3,5);plot(-x,sin(x)), grid on % inversed by horizontal axis. title('inversed by vertical axis') % gives a name at just above the graph xlabel('Radians'); ylabel('Magnitude'); subplot(2,3,6);plot(x,sin(x),x,cos(x)), grid on % two graphs at same area. title('two graphs at same area') xlabel('Radians'); ylabel('Magnitude');

3) Dicrete time signals

We can use ‘stem’ function at this section.

x1= 0:0.2:2*pi; figure(1); subplot(4,1,1);plot(x1,sin(x1)); title('Continuous sine wave between zero and 2*pi'); grid on; subplot(4,1,2);stem(x1,sin(x1)); title('Discrete sine wave between zero and 2*pi'); grid on; subplot(4,1,3);plot(x1/2,cos(x1)); title('Continuous cosine wave between zero and pi '); grid on; subplot(4,1,4);stem(x1/2,cos(x1)); title('Discrete cosine wave between zero and pi '); grid on;

4) Plotting unit impulse input and unit step input

x1= zeros(1,10); x1(1)=1; x2= ones (1,10); figure(1); subplot(2,1,1);stem(x1); title('Unit Impulse response'); subplot(2,1,2);stem(x2); title('Unit Step Response');

(1)From matlab’s help section.

Regards