Professional Documents
Culture Documents
LAB RECORD
ON
MATLAB SIMULATION
Submitted By:
Submitted To:
J.S. LATHER
Professor
To define a row matrix in MATLAB, the user must enter the elements of the row separated by
commas or just space in a pair or square parenthesis. If the user wishes to define a matrix with
more than one row, then the current row must be terminated using a semicolon before entering
the elements of the next row.
Once all the matrices are defined, the user can perform various arithmetic operations with
them.
Matrix multiplication can be done with ‘*’ operator, addition with ‘+’ operator, transpose with
‘ ’ ’, determinant with det() function, inverse with inv() function. In Output functions, x is
showing multiplication and AT is showing transpose of A.
PROGRAM:
A = [2 5; 1 9]; %Defining Ma trix A of order 2x2 Where ;[] is used for new row
B = [6 1; 9 0]; %Defining Ma trix B of order 2x2 Where ; i n [] is used for new row
C = [4 2 8; 1 9 5];%Defi ning Ma trix C of order 3x2 Where ; i n [] is used for new row
AxB=A*B % Multiplying Matrix A and Matrix B
BxA=B*A % Multiplying Matrix B and Matrix A
sum_AB=A+B % Adding Matrix A and Matrix B
BxC=B*C % Multiplying Matrix B and
Matrix C
CxCT=C*C' % Multiplying Matrix C and Matrix
C’(C Transpose)
inv_B=inv(B) % Calculating inverse of Matrix B
BTxAT=B'*A' % Multiplying Matrix B’ and Matrix
A’ Det_A=det(A) % Calculating determent of Matrix
A Det_B=det(B) % Calculating determent of Matrix B
Solution:
AxB =
57 2
87 1
BxA =
13 39
18 45
sum_AB
=86
10 9
BxC =
25 21 53
36 18 72
CxCT =
84 62
62 107
inv_B =
0 0.1111
1.0000 -0.6667
BTxAT
= 57 87
2 1
Det_A
= 13
Det_B
=-9
Solution:-
To define a row matrix whose elements form an arithmetic progression, the user must follow
the program: [first element: step size: last element]
PROGRAM:
Q = [1:1:10]; %Defining Matrix Q where :1 is used for unit difference in the span of 1-10
R = [1:2:19]; %Defining Matrix R where :2 is used for 2 difference in the span of 1-19
S = [1:-0.05:0] ; %Defining Matrix S where :-0.05 is used for -0.05 difference in the span of 1-0
T = [1 2 3]; %Defining Matrix T
M = (Q-R).^2 %Defining Matrix M, which is showing the square of difference of Matrix Q,R
RESULT
M=
0 1 4 9 16 25 36 49 64 81
A = vander(v) returns the Vander monde Matrix such that its columns are powers of the
vector v
For input vector v= [v1v2…vN], the Vander monde matrix is
(N−j)
The matrix is described by the formula A(i,j)=v(i) such that its columns are powers of the
vector v.
PROGRAM:
L = vander(1:1:5) % This command is used for vander matrix of order 5x5 with one difference
RESULT
L=
1 1 1 1 1
16 8 4 2 1
81 27 9 3 1
256 64 16 4 1
625 125 25 5 1
OBJECTIVE 4:-To form using MATLAB the matrix A= [1 11 111 1111; 2 22 222 2222; 3 33 333 3333]
How do you pick from matrix A:
• Second element of 3rd row
• Last two element of 1st row
• 2nd row entirely
• The last column entirely
Solution:-
To retrieve an element of a matrix from a certain row and column, the user must follow the
program: Matrix(row, column). If the user wishes to retrieve a row from a matrix the program
is Matrix(row,:) and for an entire column it is Matrix(:, column).
PROGRAM
J = [1 11 111 1111;2 22 222 2222;3 33 333 3333]
A = J(3,2)
B=J(1,3:4)
C =J(2,:)
D =J(:,4)
RESULT
J=
1 11 111 1111
2 22 222 2222
3 33 333 3333
A=
33
B=
111 1111
C=
2 22 222 2222
D=
1111
2222
3333
OBJECTIVE 5: To write a program to solve a general quadratic equation using MATLAB. Then
solve the following equations using it:
• 2 z2 + 0.68 z - 4 = 0
• 2z2 + 0.68 z + 4 = 0
Solution:-
PROGRAM
X = [2 0.68 -4]
A = [2 0.68 4]
W = roots(A)
Y = roots(X)
RESULT
X=
A=
W=
-0.1700 + 1.4040i
-0.1700 - 1.4040i
Y=
-1.5944
1.2544
OBJECTIVE 6: Find the singular value decomposition, Lower and Upper decomposition
of the matrix given by
101
A=234
167
Solution:-
s = svd(A) returns the singular values of matrix A in descending order.
[U,S,V] = svd(A) performs a singular value decomposition of matrix A, such that A = U*S*V'.
PROGRAM:
RESULT
U=
S=
10.6865 0 0
0 1.6376 0
0 0 0.3429
V=
Upper =
1 0 1
0 3 4
0 0 7
OBJECTIVE 7:- To determine the values of x1, x2, and x3 for the following set of linear
algebraic equations:
x2 – 3x3 = – 5
2x1 + 3x2 – x3 = 7
4x1 + 5x2 – 2x3 = 10
Solution:-
We can solve a set of linear algebraic equation using MATLAB using the equation
Ax = B
Where A is then formed by the coefficients and B is a column matrix formed by the constants
on the right side of the equation. So the solution can be obtained by calculating
x = A-1*B
PROGRAM:
RESULT
A=
0 1 -3
2 3 -1
4 5 -2
B=
-5
7
10
K=
-1.0000
4.0000
3.0000
ASSIGNMENT 2
PROGRAM:
A = [4 2 -3;-1 1 3;2 5 7]
[V,D]=eig(A)
RESULT
A=
4 2 -3
-1 1 3
2 5 7
V=
D=
0.5949 0 0
0 3.00 0
0 0 8.4051
OBJECTIVE 2:
To solve
x2 – 3x3 = – 5
2x1 + 3x2 – x3 = 7
4x1 + 5x2 – 2x3 = 10
Solution:-
We can solve a set of linear algebraic equation using MATLAB using the equation
Ax = B
Where A is then formed by the coefficients and B is a column matrix formed by the constants
on the right side of the equation. So the solution can be obtained by calculating
x = A-1*B
PROGRAM:
RESULT
A=
0 1 -3
2 3 -1
4 5 -2
B=
-5
7
10
K=
0.1667 -2.1667 1.3333
0 2.0000 -1.0000
-0.3333 0.6667 -0.3333
X=
-1.0000
4.0000
3.0000
OBJECTIVE 3: To generate an overlay plot for plotting three lines
Y1=sin t , Y2=t , Y3=t-t.^3/factorial(3)+t.^5/factorial(5)+t.^7/factorial(7); 0 ≤ t ≤
2π Using
(1) the plot command
(2) the hold command
(3) the line command
Solution:-
In MATLAB any definition is done in the form of a matrix. That means by the instructions
t =0:0.5:2*pi
y1 = sin(t)
we are defining y1 as a row matrix of size 13.To perform any mathematical operation at an
elemental level the dot (.) operator must be used.For example if the user wishes to calculate a
matrix x, whose elements are the squares of the matrix y1, then the instruction that needs to be
executed is
x =y1.^2
But not
x = y1^2
PROGRAM
Columns 12 through 21
3.4558 3.7699 4.0841 4.3982 4.7124 5.0265 5.3407 5.6549 5.9690 6.2832
y1 =
Columns 1 through 11
0 0.3090 0.5878 0.8090 0.9511 1.0000 0.9511 0.8090 0.5878 0.3090 0.0000
Columns 12 through 21
-0.3090 -0.5878 -0.8090 -0.9511 -1.0000 -0.9511 -0.8090 -0.5878 -0.3090 -0.0000
y2 =
Columns 1 through 11
0 0.3142 0.6283 0.9425 1.2566 1.5708 1.8850 2.1991 2.5133 2.8274 3.1416
Columns 12 through 21
3.4558 3.7699 4.0841 4.3982 4.7124 5.0265 5.3407 5.6549 5.9690 6.2832
y3 =
Columns 1 through 11
0 0.3090 0.5878 0.8093 0.9530 1.0092 0.9838 0.9045 0.8287 0.8526 1.1233
Columns 12 through 21
1.8524 3.3330 5.9595 10.2505 16.8757 26.6864 40.7506 60.3920 87.2348 123.2526
z = – 7 / (1 + x2 + y2) | x | ≤ 5 , | y | ≤ 5
Solution:-
PROGRAM:
X=linspace(-5,5)
Y=linspace(-5,5)
[x,y] = meshgrid(x,y); %limit the x,y surface
z= -7./(1+x.^2+y.^2); %Define the surface equation
mesh(x,y,z) %To plot the surface graph in 3-D
RESULT
Fig. Z curve
OBJECTIVE 5: To use diff command for symbolic differentiation of the following functions:
(a) S1 = ex8
(b) S2 = 3x3 ex5
(c)S3 = 5x3 – 7x2 + 3x + 6
PROGRAM:
syms x %To define the variable x
s1 = exp(x^8)
s2 = 3*x^3*exp(x^5)
s3 = 5*x^3-7*x^2+3*x+6
diff(s1) %To differentiate function s1
diff(s2) %To differentiate function s2
diff(s3) %To differentiate function s2
RESULT
s1 =
exp(x^8)
s2 =
3*x^3*exp(x^5)
s3 =
5*x^3 - 7*x^2 + 3*x + 6
ans =
8*x^7*exp(x^8)
ans =
9*x^2*exp(x^5) +
15*x^7*exp(x^5) ans =
15*x^2 - 14*x + 3
OBJECTIVE 6: To Use MATLAB’s symbolic commands to find the values of the following integrals :
Solution:
PROGRAM
syms x,y a %Define the variables
f1 = abs(x)
f2 =
cos(y)+7*y^2 f3
= sqrt(x)
f4 =7*x^5-6*x^4+11*x^3+4*x^2+8*x+9
f5= cos(a)
int(f1,0.2,0.7) %integrate the function f1 with in limit
int(f2,0.2,pi) %integrate the function f2 with in limit
int(f3) %integrate the function f3
int(f4) %integrate the function f4
int(f5) %integrate the function f4
RESULT
f1 =
abs(x)
f2 =
cos(y) +
7*y^2 f3 =
sqrt(x)
f4 =
7*x^5 - 6*x^4 + 11*x^3 + 4*x^2 + 8*x +
9 f5 =
cos(a)
ans =
9/40
ans =
(7*pi^3)/3 - sin(1/5) - 7/375
ans =
(2*x^(3/2))/3
ans =
(7*x^6)/6 - (6*x^5)/5 + (11*x^4)/4 + (4*x^3)/3 + 4*x^2 + 9*x
ans =sin(a)
OBJECTIVE 7: To obtain the partial-fraction expansion:
F(s) = 8(s+1)(s+3)/(s+2)(s+4)(s+6)(s+6)
PROGRAM:
b = [8 32 24] %Define the numerator coefficients
a =[1 18 116 312 288] %Define the denomator coefficients
[r,p,k] = residue(b,a) %To obtain residue and pole
RESULT:
b=
8 32 24
a=
1 18 116 312 288
r=
3.2500
15.0000
-3.0000
-0.2500
p=
-6.0000
-6.0000
-4.0000
-2.0000
k
=
[]
OBJECTIVE 8:- Generate the transfer function using MATLAB. Using ratio of
polynomials. G(s)= 3(s+9)(s+21)(s+57)/s(s+30)(s^2+5s+35)(s^2+28s+42)
PROGRAM:
RESULT:
Transfer function:
3 s^3 + 261 s^2 + 5697 s + 32319
--------------------------------------------------------
s^6 + 63 s^5 + 1207 s^4 + 7700 s^3 + 37170 s^2 + 44100 s
OBJECTIVE 9: -If a stone is thrown is vertically upwards with an initial speed u, its
vertical displacement s after a time t has elapsed is given by the formula s=u*t-0.5*g*t^2,
where g is the acceleration due to gravity. Air resistance has been ignored. Compute the
value of s over a period of about 12.3 seconds at intervals of 0.1seconds, and plot the
distance-time graph over this period.
Solution:-
PROGRAM:
RESULT:
OBJECTIVE 10: Find 10!.
Solution:-
PROGRAM:
OBJECTIVE 1. Consider the function h(s) where n(s) = s^4 + 6s^3 + 5s^2 + 4s + 3 and
d(s) = s^5 + 7s^4 + 6s^3 + 5s^2 + 4s + 7. Find n(– 10), d(– 5), h(– 3) ,h(S)=n(s)/d(s)
Solution:-
PROGRAM:
a= [1 6 5 4 3] % Defining Matrix
b= [1 7 6 5 4 7] % Defining Matrix
h=tf(a,b) % Defining Transfer Function
A=polyval(a,-10) %Calculating Value of function n at -10
B=polyval(b,-5) %Calculating Value of function d at -5
H=evalfr(h,-3) %Calculating Value of function h at -3
RESULT
a=
1 6 5 4 3
b=
1 7 6 5 4 7
h=
A=
4463
B=
612
H=
-0.2228
OBJECTIVE 2. Expand the following function F(s) into partial fractions using
MATLAB. Determine the inverse Laplace transform of F(s) = 1/(s^4+5s^3+7s^2)
Solution:-
PROGRAM:
RESULT
L= t/7 + (5*exp(-(5*t)/2)*(cos((3^(1/2)*t)/2) + (11*3^(1/2)*sin((3^(1/2)*t)/2))/15))/49 - 5/49
OBJECTIVE 3.For each of the second order systems below, find ξ, ωn, Ts, Tp, Tr,
% overshoot, poles, zeroes and plot the step response using MATLAB.
(a) T(s) = 130/(s^2+15s+130)
Solution:
PROGRAM
a = [130]; %defining Numerator
b = [1 15 130]; %defining Denominator
h = tf(a,b); % Defining Transfer Function using num, and den
step response=step(h) %command for getting step response
zeroes=zero(h) %calculating zeros of transfer function h
poles=pole(h) %calculating poles of transfer function h
damp(h) %calculating damping of transfer function h
Solution
zeroes =
poles =
-7.5000 + 8.5878i
-7.5000 - 8.5878i
RESULT:
a=
0.0450
b=
1.0000 0.0250 0.0450
Transfer function:
0.045
--------------------------------
s^2 + 0.025 s + 0.045
ans =
Empty matrix: 0-by-1
ans =
-0.0125 + 0.2118i
-0.0125 - 0.2118i
Eigenvalue Damping Freq. (rad/s)
-1.25e-002 + 2.12e-001i 5.89e-002 2.12e-001
-1.25e-002 - 2.12e-001i 5.89e-002 2.12e-001
RESULT
a= 100000000
b = 1 132500000 100000000
Transfer function:
1e008
-------------------------
s^2 + 1.325e008 s + 1e008
ans =
Empty matrix: 0-by-1
ans =
1.0e+008 *
-1.3250
-0.0000
Eigenvalue Damping Freq. (rad/s)
-7.55e-001 1.00e+000 7.55e-001
-1.32e+008 1.00e+000 1.32e+008
Solution:-
PROGRAM
b= [1 5]
a= [1 7 25]
g= tf(b,a)
rlocus(g)
RESUL
T
b=
1 5
a=
1 7 25
Transfer function:
s+5
--------------
s^2 + 7 s + 25
OBJECTIVE-5:Determine the close loop poles and closed loop step response of system
PROGRAM:
b= [30 -150 90];
a= [1 12 49 78 40];
g= tf(b,a);
H=
feedback(g,1)
pole(H)
step(H)
ans =
-6.5005 + 6.9514i
-6.5005 - 6.9514i
0.5005 + 1.0885i
0.5005-1.0885i
RESULT:
G(s) = 29.125(s+0.57)^2/s
Solution:-
PROGRAM:
G = zpk([-0.57 -0.57],[0],29.125);
bode(g)
RESULT
ASSIGNMENT NO. 4
OBJECTIVE 1: For the unity feedback system determine the range of K for Stability
using MATLAB G(s) = k(s+1)/s(s+1)(s+5)(s+6)
Solution:
PROGRAM
RESULT:
a=
1 12 41 30 0
b=
1 12 41 55 25
c=
-7.4043 + 0.0000i
-1.7978 + 0.3797i
-1.7978 - 0.3797i
-1.0000 + 0.0000i
d=
-7.4043
-1.7978
-1.7978
-1.0000
e=
-1.0000
K
=
25
OBJECTIVE:-2. Write a program in MATLAB for the unity feedback system with so that
the value of gain k can be input. Display the bode plots of a system for the input value of K.
Determine and display the gain and phase margin for the input value of K. G(s) =
k/[s(s+3)(s+12)]
PROGRAM
k=input('enter the value of k') %Input the value of gain
G= zpk([ ],[0 -3 -12] ,10) %Transfer function in pole-zero form
bode(G); %To draw the bode plot of given transfer function
[Gm,Pm,Wg,Wp] = margin(G) %To obtain the different characteristics of bode plot
RESULT:
enter the value of k;25
k=
25
G=
10
--------------
s (s+3)
(s+12)
Continuous-time zero/pole/gain model. Gm
=
54.0000
Pm =
83.4143
Wg=6
Wp =0.2765
OBJECTIVE 3: Write a program in MATLAB for the system shown below so that the
value of K can be input (K=40)
(a) Display the closed loop magnitude and phase frequency response for unity
feedback system with an open loop transfer function, KG(s).
(b) Determine and display the peak magnitude, frequency of the peak magnitude, and
bandwidth for the closed loop frequency response for the input value
of K. C(s)/R(s) = K(s+5)/s(s^2+3s+15)
Solution:
PROGRAM
syms w %Defining system variable
w=0.1:0.1:1000; %Define the range of frequency
k=input('enter the value of open loop gain') %enter the value of k
G=tf([k 5*k],[1 3 15 0]) %Display the open loop transfer function
gc=feedback(G,1) %Display the closed loop transfer function
[a,b]=bode(gc,w)
bode(gc) %plot the bode graph
[d,i]=max(a)
w1=w(i)
b=0.707*d
for(i=1:length(a))
if(a(i)==b)
Break
End
end
bandwidth = w(i)
RESULT:
Enter the value of open loop gain 40
k = 40
Transfer function: 40 s + 200 / s^3 + 3 s^2 + 15 s
Transfer function: 40 s + 200 / s^3 + 3 s^2 + 55 s +
200
d = 11.0512
i = 75
w1 = 7.500
b = 7.8132
Bandwidth =1000
ASSIGNMENT NO. 5
OBJECTIVE 1: Design a compensator for following transfer functions to meet the given
specifications G(s)=1/((s+1)*(s+2)*(s+3))
Mp ≤ 16%
tr ≤0.6sec
Solution:-
PROGRAM:
s=zpk('s') %Defining system variable
h=zpk([ ],[-1 -2 -3],1) %Defining pole zero form
sisotool(h) %opening the tool window for designing compensator
Zero/pole/gain:
1
H(s)= ---------------------
(s+1) (s+2) (s+3)
Root locus plot without compensator
Step response without compensator
After adding design constraints in sisotool to achieve required compensator we get following
plot
RESULT:-The Bode and Root locus plot shows desired characteristic with compensator.
RESULT:-The Step Response plot shows desired characteristic with compensator.
OBJECTIVE 2: Design a compensator for following transfer function to Meet the given specifications
4500
G(s) = -----------
-- s(s+361.2)
(i) Steady-state error due to unit-ramp input 0.000443
(ii) (ii) Maximum overshoot ≤ 5 percent
(iii) (iii) Rise time tr ≤ 0.002 sec
(iv) (iv) Settling time ts ≤ 0.005sec
Solution:
PROGRAM:
num=4500; %Defining numerator
den=[1 361.2 0]; %Defining denomator
G=tf(num,den) %Defining transfer function
sisotool(G) %Opening tool window for designing
compensator Plot without compensator
Now adding design constraints
RESULT- The desired characteristics are achieved and shown in the figure.
Figure:- Root locus plot of the desired system
OBJECTIVE 3: Design a controller for following transfer function to meet the given specifications.
1.5
G(S)= --------------------
s^2 + 14 s + 40.2
• Rise timeless than 0.5 sec
• Overshoot less than 10%
• Gain margin greater than 20dB
• Phase margin greater than 40◦
PROGRAM:
syms s
n=1.5; % Defining numerator
d=[1 14 40.2]; %Defining denomator
G=tf(n,d); % Defining transfer function
sisotool(G) % open sisotool window for designing controller
Plot without controller
Figure – Step response without controller
Solution:-
Solution:-
OBJECTIVE 3:- Find resulting transfer function, step Response
OBJECTIVE 4:- Find resulting transfer function’s Step Response
PROGRAM:
A=
B=
1
0
0
C=
D=
0
OBJECTIVE 7:- Compute state space transition matrix for a given system matrix at
time t = 0.2 sec [A]=[2 -4 -6;3 1 2;2 -3 4]
PROGRAM:
ans =