Вы находитесь на странице: 1из 6

MATLAB

Lecture 1 Outline
- What is Matlab?
- Introduce the command window
- Define scalar variable and scalar operations
- Define vector variables and vector operations
- Define matrices and matrices operations
- Introduce the Help menu
- Introduce scripting editor and scripting file (*.m)
- Introduce plot
What is Matlab?
- Matlab is an acronym for Matrix Laboratory
- Matlab is a software package
Performs engineering and scientific calculation
The calculations are performed in matrices forms
- Save time by using Toolbox
a set of build-in function
- In control we use Control System Toolbox
- Matlab uses scripts
Executes statements as in the programing language
Executes on the top of the program-slow
The programing is sequential (C like)
May use loop statement (for, while)
May use conditional statement (if-then, case)
May create your own functions
May call existing functions-Save time

Introduce the command/help window
>clc to clear the screen
>clear- to clear the workspace. Clear all the memory contents
>help give the library files
>help bode
>help exp
> help elfun
Define scalar variable and scalar operations
>a=-2; b=-3.5; these are scalar value. Semicolon is do not equal. Matlab in default assign as
floating point value.
>c = a+b ; d = a-b ;e = a*b; f = a/b Arithmetic operation
> cos(pi); cosd(30); cos(30) only cos function gives the value in radian.
>abs(b)
> x= 2.5;
> b= exp(x)
For complex number
>x = 3+5i; this will understand the Matlab.
> y = -2.5 + 3j; Electrical Engineers use j as imaginary number.
> z= exp(x);
> cos(x); sin(x);
Define vector variables and vector operations
> a = [1 2 3 5]; Define the row vector. Check the value of a in work space.
> b = [-1.4; 2.5; -7]; Define the column vector.
> c = b gives the transpose of the vector. So you will get a row vector
> d = a transpose of a gives the column vector.
> e = [1+2j 2-5j -3.5-3.4j]
>f=e e hermition gives the complex conjugate of e called hermition transpose
Another way to create the vector which called sequential vector is very important for plot.
>h = 0:1:10; vector start from 0, increment is 1 and final value is 10.
>i=-0.5:0.1:2
How to use vector in arithmetic operation.
>a= [1 2 3 5];
>b = a+3.5; --add 3.5 to each index of a.
> c = 3.5 a;
>d = a*2; e = a/3.4;
Index operation of vector (Note: for index operation the size of the vectors should be equal)
>a = [1 2 5];
> b = [2 1 5];
>c = a .* b; -- index multiplication
> c = a*b; you will have an error.
> d = a ./b;
> e = a +b; for addition you do indexes by default.
>f = a.^3; Each index in a will be cubed.
> x = 0:0.1:0.5;
> y = cos(x); -- each index of vector x will be cosine of the value.
> y = sin(x); y = exp(x);
>> t = 1: 0.001:10;
>> y = 12.5*exp(-t/2).*cos(200*t);
>>plot(t,y); -- exponentially decaying function
>>y1 = 2*exp(-t);
>>plot(t,y1);
>>xlabel(Time);
>>ylabel(Voltage);
>>title(Time Response);
>> grid on;
>>grid off;
Define matrices and matrices operations
Matrices are same as a vector and only difference is that it is two dimensional array.
>> help elmat %elementary matrix operation
a = [1 3;5 7];
b= a*5;
c=a+3;
d= inv(a);
e = [1 3; 1 5; 5 9];
%f = inv(e) -gives error since e is not square matrix
f = a+b; % index addition
f = a .*b; % index multiplication
g= a ./b % index division
h = a/b
i = a*inv(b)
j= a*a;
k= a^2;
l= a.^a
Introduce scripting editor and scripting file (*.m)
>>edit Command for script or m file
It will go on the new script file
% for comment on m-file
% This is the tutorial for the lecture #1
t1 = 0:0.01:10;
y1 = 3*exp(-t1/3);
t2 = 0:0.001:5;
y2 = cos(t2);

plot(t1, y1, t2, y2);
To run the script either you go to command window and type the file name or from script file
with run bottom.
% Subplot example1
t1 = -10:0.001:10;
y1= cos(t1);

t2 = 0:0.001:5;
y2 = cos(t2).*exp(-t2/3).*sin(10*t2);

t3 = 0:0.01:10;
y3=exp(-t3/5);

subplot(1,3,1)
plot(t1, y1);
grid on;
xlabel('Time');
ylabel('Voltage');

subplot(1,3,2)
plot(t2,y2);
grid on;

subplot(1,3,3);
plot(t3,y3);
grid on;
xlabel('Time');
title('Response');



% Subplot example2
t1 = -10:0.001:10;
y1= cos(t1);

t2 = 0:0.001:5;
y2 = cos(t2).*exp(-
t2/3).*sin(10*t2);

t3 = 0:0.01:10;
y3=exp(-t3/5);

t4= 0:0.01:10;
y4= sin(4*t4);

subplot(2,2,1)
plot(t1, y1);
grid on;
xlabel('Time');
ylabel('Voltage');

subplot(2,2,2)
plot(t2,y2);
grid on;

subplot(2,2,3);
plot(t3,y3);
grid on;
xlabel('Time');
title('Response');

subplot(2,2,4);
plot(t4,y4);
grid on;
xlabel('Time');
ylabel('Current');
title('Sine function');

Functions
1. Open FileNewFunction
Note: The file name should be same as function name
function y = square( x )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here

y=x*x;
end

Lecture 2: Basic feedback Systems
Outline
- Partial Fraction Expansion
- Inverse of Laplace Transform
- Transfer function
- State Space Equations
- Block Diagram
--Series, Parallel and Feedback
- Time domain analysis
--Step Response
--Impulse Response h(t)
--Ramp Response
--Response to an Arbitrary Input
--Response to and Arbitrary Initial conditions

Partial Fraction Expansion
Evaluate the partial Fraction Expansion using residue command
- Syntax: [r, p, g] = residue(num,den)
--r is a vector for the partial values
--p is a vector for the poles
--g is a vector for the polynomial coefficients
( )
3
2
2 2 2
1
3 2 2 1
s s s
H s s
s s s s
+
= = +
+ + + +

>>num = [1 2 -1 0]; den=[1 3 2];
>> [r,p,g] = residue(num,den);


%m-file for function
a = 5;
b = square(a);
c=b*1.5;
d= square(c)

%Tutorail 4.m
num = [1 2 -1 0]; % for the
numerator
den=[1 3 2]; % for the
denominator
[r,p,g] = residue(num,den)

r =

-2
2


p =

-2
-1


g =

1 -1
Inverse of Laplace Transform
Unrepeated Real pole
( )
2
1
3 2
s
H s
s s

=
+ +


3 2
2 1 s s

= +
+ +

( ) ( ) ( )
1 2
[3 2 ]
t t
L H s h t e e u t

= = (


--In Matlab, we find the partial terms using residue
--Then we plot the function
%Tutorial5.m
num = [1 -1];
den = [1 3 2];
%printsys(num,den); Print the TF
[r,p] = residue(num,den);
t = 0:0.001:5;
h1 = r(1)*exp(p(1).*t);
h2 = r(2)*exp(p(2).*t);
h = h1+h2; % since LP is linear tranformation
plot(t,h,t,h1,t,h2)
grid on

Unrepeated complex pole
( )
2
3 2
2 1
4 6 4
s s
H s
s s s
+
=
+ + +

( )
( )
( )
( )
1.75 0.25 1.75 0.25
4.5
2 1 1
j j
s s j s j
+
= + +
+ + + +

( ) ( )
171.87 171.87
4.5 1.7678 1.7678
2 1 1
j j
e e
s s j s j

= + +
+ + + +

( ) ( )
2
4.5 3.5356 cos 171.87
t t
e e t u t

(
= + +







%Tutorial6.m
num = [ 1 -2 1];
den = [1 4 6 4];
%printsys(num,den)
[r,p]=residue(num,den);

t = 0:0.001:5;
h1=r(1)*exp(p(1).*t);
h2=2*abs(r(2))*exp(real(p(2)).*t).*cos
(imag(p(2)).*t +angle(r(2)));
h = h1+h2;
subplot(3,1,1);
plot(t, h1);
grid on;
subplot(3,1,2);
plot(t,h2);
grid on;
subplot(3,1,3)
plot(t,h);
grid on;

Вам также может понравиться