MATLAB Basics
MATLAB Basics
MATLAB Basics
EEE DEPARTMENT
MATLAB Applications to
electrical Engineering
S.V.R.LAKSHMI
KUMARI
P.VENKATESH
ASSISTANT PROFESSOR
ASSOCIATE PROFFSSOR
10/9/15 03:16:28 PM
MATLAB PRESENTATION
Introduction to MATLAB
MATLAB Basics
Vectors and Matrices
Loops
Plots
Examples
Simulink
Modelling examples
MATLAB
SIMULINK
CONTENTS
Introduction to MATLAB
MATLAB Basics
Vectors and Matrices
Loops
Plots
Examples
MATLAB
INTRODUCTION
MATLAB has been developed by Math Works ,It is a powerful
software package used for high performance scientific numeric
computation ,data analysis and visualization
MATLAB stands for MATrix Laboratory
MATLAB provides matrix as one of the basic elements. It provides
basic operations like addition , subtraction , multiplication by use of
simple mathematical operators
Programs can be run step by step, with full access to all variables,
functions etc
10/9/15 03:16:29 PM
MATLAB PRESENTATION
We need not declare the type and size of any variable in advance and
MATLAB is case sensitive and so we have to be careful about the case of
variables while using them in our program
MATLAB gives an interactive environment with hundreds of reliable and
accurate built in functions. These functions help in providing the solutions
to a variety of mathematical problems including matrix algebra ,linear
systems ,differential equations ,optimization, nonlinear systems and other
scientific and technical computations
It facilitates access to FORTRAN and C codes by means of external
interfaces
Disadvantages:
Interpreted programming language, slower than C++
and Fortran.
MATLAB BASICS
Introduction to Matlab
MATLAB Basics
Vectors and Matrices
Loops
Plots
MATLAB examples
Workspace
Command
History
10/9/15 03:16:29 PM
Command Window
MATLAB PRESENTATION
MATLAB Windows
MATLAB desktop
Workspace: All the information and details of every variable entered into the
command window is displayed here.
Command History : stores history of every command.
Command window: Every operation which we want to
be entered in command window.
perform is to
2.
3.
10/9/15 03:16:29 PM
MATLAB PRESENTATION
11
Variables
1.
2.
3.
Predefined variables
Break,Case,Catch,continue,else,elseif
end,for,function,global,if,otherwise,return,
switch,try ,while,pi,j,I,ans,NaN,inf etc
Creating Vectors
Create vector with equally spaced intervals
>> x=0:0.5:pi
x =
0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000
Create vector with n equally spaced intervals
>> x=logspace(1,2,7)
x =
10.0000
14.6780
21.5443
68.1292 100.0000
31.6228
46.4159
minimum.
sum(A): summation.
sort(A): sorted vector
median(A): median value
std(A): standard deviation.
det(A) : determinant of a square
matrix
dot(a,b): dot product of two vectors
Cross(a,b): cross product of two vectors
Inv(A): Inverse of a matrix A
Scalar
addition
a+b
a+b
subtraction
ab
ab
multiplication a x b
a*b
division
ab
a/b
Exponentiation
ab
a^b
Precedence
Operation
1
MATLAB PRESENTATION
216
Function
exp(x)
log(x)
log10(x)
sqrt(x)
10/9/15 03:16:29 PM
Description
Exponential (ex)
Natural logarithm
Base 10 logarithm
Square root
MATLAB PRESENTATION
17
Trigonometric Operations
Functio
n
Description
Description
Examples
>> x=1:3
x=1
>> exp(x)
ans = 2.7183 7.3891 20.0855
>> log(x)
ans = 0 0.6931 1.0986
>> sqrt(x)
ans = 1.0000 1.4142 1.7321
clc;
clear;
z1=1+2i;
z2=3+4i;
z3=z1+z2
z4=z1-z2
z5=z1*z2
z6=z1/z2
abs(z2)
angle(z3)
Output
z3 = 4.0000 + 6.0000i
z4 = -2.0000 - 2.0000i
z5 = -5.0000 +10.0000i
z6 = 0.4400 + 0.0800i
ans =5
ans = 0.9828
10/9/15 03:16:29 PM
MATLAB PRESENTATION
24
10/9/15 03:16:30 PM
MATLAB PRESENTATION
25
10/9/15 03:16:30 PM
MATLAB PRESENTATION
26
MATLAB PRESENTATION
27
Example :
10/9/15 03:16:30 PM
MATLAB PRESENTATION
28
Solution by Matrix
Inverse:
Ax = b
A-1Ax = A-1b
x = A-1b
Ax = b
>> A = [ 3 2 -1; -1 3 2; 1
-1 -1];
>> b = [ 10; 5; -1];
>> x = inv(A)*b
x=
-2.0000
5.0000
Answer:
x =-6.0000
-2, x = 5, x = -6
2
MATLAB:
MATLAB:
NOTE:
left division: A\b b A right division: x/y x y
linsolve command
4 y 3z 2
x y 9 z 60
A*S=B
MATLAB Code:
>> A=[5,-2,-3;0,4,3;1,-1,9];
>> B=[-3,-2,60]';
transpose ()
>> S=linsolve(A,B)
S=
1.0000
-5.0000
6.0000
10/9/15 03:16:30 PM
% Note vector
Solve command
The solve command used to solve quadratic equations. The
function returns the roots of the equation
Example :
MATLAB CODE
1)Quadratic equation x2-7x +12
=0
OUTPUT
s=
3
4
>> Eq = X^2-7*X+12;
>>S= Solve(Eq);
(or)
solve('x^2-7*x+12=0')
2) X4-7X3+3X2-5X+9=0
>> Eg= X^4-7*X^3+3X^25X+9=0;
>>S=solve(Eg);
EX:
syms a b c x
fun=a*x^2+b*x+c;
solve(fun,x)
Output:
ans =
-(b + (b^2 - 4*a*c)^(1/2))/
(2*a)
-(b - (b^2 - 4*a*c)^(1/2))/(2*a)
Expandand
theCollectcommands
Theexpandand thecollectcommand expands and collects an
equation respectively. The following example demonstrates the
concepts
MATLAB Code :
10/9/15 03:16:30 PM
MATLAB PRESENTATION
33
Factor command :
Thefactorcommand factorizes an
expression and
thesimplifycommand simplifies an
expression.
MATLAB :code
Examples
Answer
factor(x^3 - y^3)
factor([x^2y^2,x^3+y^3])
simplify((x^4-16)/
(x^2-4))
x^2 + 4
10/9/15 Simplify((x^303:16:30 PM
MATLAB
PRESENTATION
X^2+x*y+y^2
34
Polyval command:
POLYVAL command Evaluates polynomial in a matrix
sense
Ex 1:
>> a=[1 2 3];
>> polyval(a,1)
ans =6
% polynomial X2 + 2X +3
% polynomial is evaluated at 1
Ex 2:
>>a=[1 2 3]
>>b= 1:5
>>C=polyval(a,b)
Output :
C=[6 11 18 27 38]
10/9/15 03:16:30 PM
MATLAB PRESENTATION
35
Diff command
ANSWER
syms t
f = 3*t^2 + 2*t^(-2);
diff(f)
6*t - 4/t^3
f = x*exp(-3*x);
diff(f, 2)
9*x*exp(-3*x) - 6*exp(3*x)
10/9/15 03:16:31 PM
MATLAB PRESENTATION
37
Integration:
MATLAB Code :
syms x
int(2*x)
ans = x^2
For example, to calculate the value of
we write
int(x, 4, 9)
ans = 65/2
10/9/15 03:16:31 PM
MATLAB PRESENTATION
38
Dsolve command:
10/9/15 03:16:31 PM
MATLAB PRESENTATION
39
MATLAB code :
For onvolution
For Deconvolution
>> p1=[1,3,2];
>> p2=[1,5,4,4];
>> pc=conv(p1,p2)
pc =
1
8 21 26 20
8
10/9/15 03:16:31 PM
>> deconv(pc,p2)
ans =
1
3
2
>> deconv(pc,p1)
ans =
1
5
4
4
MATLAB PRESENTATION
40
Mathematics Example:
Polynomial Roots
Find the roots of the following system:
y 12 x 2 x 8
MATLAB code:
>> roots([12 -1 -8])
ans =
0.8592
-0.7759
10/9/15 03:16:31 PM
Our aim is to find some local maxima and minima on the graph, so
let
us find the local maxima and minima for the interval [-2, 2] on the
graph
syms x;
y = 2*x^3 + 3*x^2 - 12*x + 17;
ezplot(y, [-2, 2])
Answer
laplace(a)
1/s^2
laplace(t^2)
2/s^3
ilaplace(1/s^3)
t^2/2
MATLAB Code
Answer
f = exp(-2*x^2);
FT = fourier(f)
FT = (2^(1/2)*pi^(1/2)*exp(w^2/8))/2
f = ifourier(-2*exp(abs(w)))
f =-2/(pi*(x^2 + 1))
MatLab
>>num = [1 -2 1 -6.3];
>>den = [1 0.05 -3.14];
>>f = tf(num,den)
Example
Examples
10/9/15 03:16:31 PM
MATLAB PRESENTATION
49
10/9/15 03:16:31 PM
MATLAB PRESENTATION
50
10/9/15 03:16:31 PM
MATLAB PRESENTATION
51
10/9/15 03:16:31 PM
MATLAB PRESENTATION
52
Introduction to Matlab
Matlab Basics
Vectors and Matrices
Loops
Plots
MATLAB examples
Matrix Index
Given:
A(-2), A(0)
Error: ??? Subscript indices must either be real positive integers or
logicals.
A(4,2)
Error: ??? Index exceeds matrix dimensions.
10/9/15 03:16:31 PM
MATLAB PRESENTATION
54
>> A = [1,2,3;4,-5,6;5,-6,7]
10/9/15 03:16:31 PM
MATLAB PRESENTATION
55
Columns separated by
space or a comma
10/9/15 03:16:31 PM
1 2 3
4 5 6
7 8 9
Rows separated by
semi-colon
MATLAB PRESENTATION
56
>>>A(:,3)
ans=
3
6
9
>>>A(1,:)
ans=
123
10/9/15 03:16:31 PM
>>>A(2,:)
ans=
456
MATLAB PRESENTATION
57
10/9/15 03:16:31 PM
MATLAB PRESENTATION
58
>>>A/3
ans=
0.33330.66671.0000
1.33331.66672.0000
2.33332.66673.0000
10/9/15 03:16:31 PM
MATLAB PRESENTATION
59
2
5
8
3
6
9
A^2 = A * A
10/9/15 03:16:31 PM
MATLAB PRESENTATION
60
A*B
A.*B
10/9/15 03:16:31 PM
>>>B=[111;222;333]
B=
111
222
333
14 14 14
32 32 32
50 50 50
1x1 2 x1 3 x1
4 x 2 5 x 2 6 x 2
7 x3 8 x3 9 x3
MATLAB PRESENTATION
1
8
10 12
21 24 27
61
A./B
A.^B
10/9/15 03:16:31 PM
1/ 1 2 / 1 3 / 1
4 / 2 5 / 2 6 / 2
7 / 3 8 / 3 9 / 3
11 21
2
2
4
5
7 3 83
31
62
93
MATLAB PRESENTATION
2
3
1
16
25
36
62
Tril/triu commands
>> a=[1 2
a=
1
2
4
5
7
8
3;4 5 6;7 8 9]
3
6
9
>> tril(a)
ans =
1
0
4
5
7
8
>> triu(a)
ans =
1
0
0
2
5
0
0
0
9
3
6
9
MATLAB PRESENTATION
64
t =1:10
t =
2
3
4
k =2:-0.5:-1
10
k =
1.5
0.5
-0.5
-1
= [1:4; 5:8]
x =
1
5
10/9/15 03:16:31 PM
2
6
3
7
4
8
MATLAB PRESENTATION
65
10/9/15 03:16:31 PM
x = ones(1,3)
x =
1
1
1
x = rand(1,3)
x =
0.9501 0.2311 0.6068
MATLAB PRESENTATION
66
Concatenation of Matrices
B = [x ; y]
1 2
4 5
C = [x y ;z]
Error:
??? Error using ==> vertcat CAT arguments dimensions are not consistent.
10/9/15 03:16:31 PM
MATLAB PRESENTATION
67
Indexing Matrices
Indexing using parentheses
>> A(2,3)
10/9/15 03:16:32 PM
MATLAB PRESENTATION
68
Indexing Matrices(Cond)
Index complete row or column using
the colon operator
>> A(1,:)
10/9/15 03:16:32 PM
MATLAB PRESENTATION
69
Description
cat
horzcat
vertcat
repmat
blkdiag
Example :
>>A = ones(2, 5) * 6; % 2-by-5 matrix of
6's
% 3-by-5 matrix of
random values
>>C = [A; B]
concatenate A and B
% Vertically
Output :
C=
6.0000
6.0000
0.9501
0.2311
0.6068
6.0000
6.0000
0.4860
0.8913
0.7621
6.0000
6.0000
0.4565
0.0185
0.8214
6.0000
6.0000
0.4447
0.6154
0.7919
6.0000
6.0000
0.9218
0.7382
0.1763
Example :
a=
1
4
7
2
5
8
3
6
9
>> b=repmat(a,2,3)
b=
1
4
7
1
4
7
2
5
8
2
5
8
3
6
9
3
6
9
1
4
7
1
4
7
2
5
8
2
5
8
3
6
9
3
6
9
1
4
7
1
4
7
2
5
8
2
5
8
3
6
9
3
6
9
Creating a Block
Diagonal Matrix
Output:
D=
8 1
3 5
4 9
0 0
0 0
0 0
0 0
6
7
2
0
0
0
0
0
0
0
-5
-4
0
0
0
0
0
-6
-4
0
0
0
0
0
-9
-2
0
0
0
0
0
0
0
8
0
0
0
0
0
0
0
8
Example
10/9/15 03:16:32 PM
MATLAB PRESENTATION
76
10/9/15 03:16:32 PM
MATLAB PRESENTATION
77
10/9/15 03:16:32 PM
MATLAB PRESENTATION
78
10/9/15 03:16:32 PM
MATLAB PRESENTATION
79
10/9/15 03:16:32 PM
MATLAB PRESENTATION
80
Diagonal elements
10/9/15 03:16:32 PM
MATLAB PRESENTATION
81
10/9/15 03:16:32 PM
MATLAB PRESENTATION
82
The fscanf and fprintf commands behave like C scanf and printf functions. They support the following
format codes:
Format Code
Purpose
%s
Format as a string.
%d
Format as an integer.
%f
%e
%g
\n
\t
10/9/15 03:16:32 PM
MATLAB PRESENTATION
83
Fprintf examples :
>> n=2;
>> fprintf('value is %d\n',n)
Output:
value is 2
fprintf('I have %d brother and %d
sister\n',1,1)
Output :
I have 1 brother and 1 sister
>> fprintf(This is %s department , we got %d years of NBA
accrediation/n','EEE',5)
Output : This is EEE department ,we got 5 years of NBA
accrediation/n>>
input command
Example :
x=input('enter the value of x');
y=input('enter the value of y');
z=x+y;
fprintf('the value of z is %d\n',z)
Output:
enter the value of x30
enter the value of y0
the value of z is 30
Input
Write the program to display 6th table
6*1=6
6*2=12
6*3=18
6*4=24
6*5=30
6*6=36
6*7=42
6*8=48
6*9=54
6*10=60
n=input('enter n value');
for i=1:10
fprintf('%d*%d=%d\n',n,i,n*i)
end