BEIT, 6th SemesterMatlab Basics... A/B is valid and equals A.B -l for same size square matrices A & B... Few methods to create vector LINSPACEx1, x2: generates a row vector of 100 linea
Trang 3-External Interface
-Optional Toolbox for Signal Processing,
System analysis, Image Processing
Trang 4How many windows?
Trang 5BEIT, 6th Semester
MATLAB Windows
characterized by ‘>>’ command prompt
Edit window: where programs are written
and saved as ‘M-files’
Graphics window: shows figures,
alternately known as Figure window
Trang 6Command Window
Figure Window
Edit Window
Trang 7BEIT, 6th Semester
Matlab Basics
Trang 8MATLAB Basics: Variables
Variables:
-Variables are assigned numerical values
by typing the expression directly
Trang 10 Global Variables
Local Variables
Trang 12 Matrices : Basic building block
Elements are entered row-wise
Trang 13BEIT, 6th Semester
Basics Matrices
Special Matrices
null matrix: M = [];
nxm matrix of zeros: M = zeros(n,m);
nxm matrix of ones: M = ones(n,m);
nxn identity matrix: M = eye(n);
Try these Matrices
[eye(2);zeros(2)],[eye(2);zeros(3)], [eye(2),ones(2,3)]
Trang 14Matrix Operations
Matrix operations:
A+B is valid if A and B are of same size A*B is valid if A’s number of columns equals B’s number of rows.
A/B is valid and equals A.B -l for same size square matrices A & B.
Element by element operations:
.* , / , ^ etc
a = [1 2 3], b = [2 2 4],
Trang 15BEIT, 6th Semester
Vectors
Trang 16Few methods to create vector
LINSPACE(x1, x2): generates a row vector of 100 linearly equally spaced points between x1 and x2.
LINSPACE(x1, x2, N): generates N points
between x1 and x2.
LOGSPACE(d1, d2): generates a row vector of 50
logarithmically equally spaced points between
decades 10^d1 and 10^d2.
Trang 17BEIT, 6th Semester
Waveform representation
Trang 182D Plotting
plot: creates linear continuous plots of
vectors and matrices;
plot(t,y): plots the vector t on the x-axis
versus vector y on the y-axis
To label your axes and give the plot a title, type
xlabel('time (sec)') ylabel('step response') title('My Plot')
Change scaling of the axes by using the
Trang 20Example: Draw sine wave
Trang 21BEIT, 6th Semester
Matlab Plot
Trang 22Example: Discrete time signal
Trang 23BEIT, 6th Semester
Matlab Plot
Trang 24Example: Draw unit step and delayed
unit step functions
n=input('enter value of n')
t=0:1:n-1;
y1=ones(1,n); %unit step
y2=[zeros(1,4) ones(1,n-4)]; %delayed unit step
Trang 25BEIT, 6th Semester
Matlab Plot
Trang 26Functions
Trang 27BEIT, 6th Semester
Scripts and Functions
Script files are ‘M-files’ with some valid
MATLAB commands
Generally work on global variables present
in the workspace
Results obtained after executing script
files are left in the workspace
Trang 28Scripts and Functions
Function files are also ‘M-files’ but with local variables
Syntax of defining a function
function [output variable] =
function_name (input variable)
File name should be exactly same as function name
Trang 29BEIT, 6th Semester
Example : user defined function
Assignment is the method of giving a value to a variable You have already seen this in the
interactive mode We write x=a to give the value
of a to the value of x Here is a short program
illustrating the use of assignment
Trang 30Loops, flows:
for m=1:10:100
num = 1/(m + 1) end
Trang 31BEIT, 6th Semester
Recapitulate
Trang 32Few in-built Matlab functions
Trang 33BEIT, 6th Semester
Practice
1>Draw a straight line satisfying the equation: y = 3*x + 10
2> Create matrices with zeros, eye and ones
3>Draw a circle with radius unity (use cos θ for x axis, sin θ for y axis)
4>Draw a cosine wave with frequency 10kHz Use both ‘plot’ and
‘stem’ functions to see the difference.
5>Multiply two matrices of sizes 3x3.
6>Plot the parametric curve x(t) = t, y(t) = exp(-t/2) for 0<t<pi/2
using ezplot
7>Plot a cardioid r(θ) = 1 + cos(θ) for 0< θ <pi/2