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

MODELING AND DIGITAL

SIMULATION LABORATORY
Simulation means imitation or mimicry of a
situation or process.

Biswajit Tikadar
001410801065
BEE III B1 ELECTRICAL ENGG., JADAVPUR UNIVERSITY
BASIC COMMANDS IN MATLAB

• help-help search in MATLAB for any keyword or function


• lookfor-any related document related to the keyword or
function.
• ones(x)-create a x-square matrix with all element 1
• zeroes(x)-create a x-square matrix with all element 0
• diag(x)-extract diagonal of the matrix
• rank(x)-give rank of the matrix
• det(x)-determinant of matrix
• Inv(x)-inverse of matrix x
• sqrt(x)-Square root; x.
• cos(x)-Cosine; cos(x).
• sin(x)-Sine; sin(x).
• tan(x)-Tangent; tan(x).
• abs(x)-Absolute value; |x|.
• mean-Calculates the average.
• median-Calculates the median.
• std-Calculates the standard deviation.
• sym-Creates a symbolic variable.
• syms-Creates one or more symbolic variables.
• ilaplace-Returns the inverse Laplace transform.
• laplace-Returns the Laplace transform.

%% 1. Checking and practicing different commands in


command window.
2. creating m -file (script file). %%

1|Page
1. Write a script file to convert Fahrenheit to Celsius.

%%Fahrenheit to Celsius%%

disp('Fahrenheit to Celsius'); %display it in the command window


F=input('Temperature in Fahrenheit'); %taking input temperature
in Fahrenheit
C=(F-32)/1.8; %Fahrenheit to Celsius formula
fprintf('The value of C is %i.\n',C) %display the result

Comment-
1. Remembering the formula to convert Celsius to
Fahrenheit.
2. Creating m-file and running it for the result.

2.Write a program to solve branch current in given electrical


system.

2|Page
%solving an electrical network given
% formula used to find loop current I=R^-1*V (all are matrix)
%from loop, current finding branch current
% Loop current in Capitals, Branch current in small letters

w=2*3.14*50; %value of w
V=[100;0;0]; %voltage matrix
R=[2+w*01i 1+w*0.1i 0;1+w*0.1i 5+w*0.2i 2;0 2
12+10^6/(w*0.1i)]; % resistance matrix
disp('Loop currents')
I=inv(R)*V %loop currents

%branch currents
disp('All in Amperes RMS')
i1=I(1)
i2=I(1)-I(2)
i3=I(2)
i4=I(2)-I(3)
i5=I(3)

RESULT:

I=
0.0025 - 0.3352i
-0.0092 + 0.1669i
0.0000 + 0.0000i

3|Page
All in Amperes RMS
i1 =0.0025 - 0.3352i
i2 = 0.0117 - 0.5021i
i3 =-0.0092 + 0.1669i
i4 = -0.0092 + 0.1669i
i5 =1.0481e-05 + 5.7462e-07i

Comment-
1. Applying knowledge of network solving in MATLAB.
2. Creating impedance, voltage and current matrix.
3. Checking the result, we get using pen and paper.

3.Write a program to plot system response with different


value of R(resistance).

4|Page
% With different value of R plotting undamped, overdamped,
underdamped and critically damped for the system given

syms s r

disp('1.For Undamped r=0')


disp('1.For Underdamped r<200')
disp('1.For overdamped r>200')
disp('1.For critically damped r=200')

r=input('enter value of r :')

tf=ilaplace(100/(s*s+10*r*s+10^6)); %transfer function


ezplot(tf, [0.0001,.1]) %plotting response

Comment-
1. Plotting Response of the system with different value of
Resistance.
2. With a single code, we can generate plots for undamped,
underdamped, critically damped and over damped
response.
RESULT:
• Response for underdamped, undamped, critically
damped and overdamped respectively.

5|Page
-7
x 10 (31/2 exp(-500 t) sin(500 31/2 t))/15

10

-2

-4

-6

0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1
t

sin(1000 t)/10

0.1

0.05

-0.05

-0.1

0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1
t

6|Page
-6 100 t exp(-1000 t)
x 10
7

0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1
t

-4
x 10 (31/2 exp(-2000 t) sinh(1000 31/2 t))/30

18

16

14

12

10

0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1
t

7|Page
4. Write a m-file to solve the given differential equations.
(I)
function f=sys1(t,y) %creating a function sys1 for ode45
global a1 a2 b
a1=1;
a2=1;
b=1;
if t<10
u=0; %input as unit step with a delay
else
u=1;
end

f(1)=y(2); %defining the differential equation in state variable


f(2)=-a1*f(1)-a2*y(1)+b*u;
f=f';

%Calling sys1 in ode45.


global a1 a2 b
a1=1;
a2=1;
b=1;
[t,y]=ode45(@sys1,[0 25],[0 0]); %function name, time span,
initial value

plot(t,y(:,1));

8|Page
RESULT:

1.2

0.8

0.6

0.4

0.2

-0.2
0 5 10 15 20 25

Comment:
1. By changing the value of constants a1, a2, b different
response can be obtained.
2. By changing the input type another response can be
obtained.
3. Application in real life- we can simulate a second order
system in MATLAB and check with what value of a1, a2, b
it gives satisfactory result.
9|Page
(II)
function f=sys2(t,y) %Function sys2 with PID controller
global a1 a2 b kp ki kd
if t<10
r=0;
else
r=t; %ramp input with delay
end

f(1)=y(2);
f(2)=y(3);
f(3)=ki*r-(a1+b*kd)*f(2)-(a2+b*kp)*f(1)-b*ki*y(1);
f=f';
end
%solving sys2 using ode45
global a1 a2 b kp ki kd
a1=1;
a2=1;
b=1;
kp=1;
ki=1;
kd=1;
[t,y]=ode45(@sys2,[0 25],[0 0 0]);

plot(t,y(:,1));

10 | P a g e
RESULT:

25

20

15

10

-5
0 5 10 15 20 25

Comment:
1. PID controller is included in the transfer function of the
system.
2. With different value of Kp,Ki and Kd we can obtain our
required response.
3. Ramp input is used instead of step input to incorporate all
the practical cases.

11 | P a g e
5.Write a program for Mass Spring Damper.

function f=sys3(t,x) %function for mass spring damper


global m k b
%u=1;
if t>0
u=1; %unit step input
else
u=0;

end

f(1)=x(2);
f(2)=-(b/m)*f(1)-(k/m)*x(1)+(u/m); %state variable of
differential equation.
f=f';
end
%mass spring damper using ode45

global m k b
m=1;
k=1;
b=1;
[t,x]=ode45(@sys3,[0 25],[0 0]);

12 | P a g e
plot(t,x(:,1));
RESULT:

1.4

1.2

0.8

0.6

0.4

0.2

0
0 5 10 15 20 25

Comment:
1. With practical value of mass, spring constant the real
system can be simulated in MATLAB.
2. We can adjust the force acting in the mass, and the
position response can be plotted.

13 | P a g e
6. Write a program in MATLAB for inverted pendulum.

%%INVERTED PENDULUM%%

M = input('Enter Mass(in kg): ');


m = input('Enter Mass of Pendulum(in kg): ');
l1 = input('Enter length of Pendulum(in m): ');
b = input('Enter Co-efficient of friction(in N/m/sec): ');
l = l1/2;
I = input('Enter moment of inertia(in kg.m^2): ');
g = input('Enter gravitational acc.(in m/s^2): ');
q = ((M+m)*(I+m*l^2)-(m*l)^2);
A=m*l/q; B=b*(I+m*l^2)/q; C=-(M+m)*m*g*l/q; D=-
b*m*g*l/q;
sys = tf([A 0 0],[1 B C D]);
t = 0:0.005:1;
u = input('Enter the expression for the \n excitation in
terms of t: ');
[y,t,x]=lsim(sys,u,t);
plot(t,u,'k',t,y,'r');
legend('Input','Output');

RESULT:
For input as follows we get the following result.

14 | P a g e
• Enter Mass (in kg): .9
• Enter Mass of Pendulum (in kg): .1
• Enter length of Pendulum (in m): .25
• Enter Co-efficient of friction (in N/m/sec): 0
• Enter moment of inertia (in kg.m^2): 0.0053
• Enter gravitational acc. (in m/s^2): 9.8
• Enter the expression for the excitation in terms of t:
t>0.1

10
Input
9 Output

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

Comment:
1. The input is entered by the user as per his/her
requirement.

15 | P a g e
2. The Inverted Pendulum is one of the most important
classical problems of Control Engineering is well
known example of nonlinear, unstable control
problem.
3. Practical example of inverted pendulum is flight of a
rocket.
4. Used as a benchmark for testing control algorithms
(PID controllers, state space representation, neural
networks, fuzzy control, genetic algorithms, etc.)

7.Using SIMULINK construct a second order system and


show the response.

16 | P a g e
Comment:
1. Simulink is a graphical extension to MATLAB for
modeling and simulation of systems
2. With different value of Kp,Ki and Kd we can obtain
our required response.
3. The block diagram of the system is made using
SIMULINK.
4. The user interface of SIMULINK is very easy to
understand.
5. Rael life problems can be incorporated using
SIMULINK efficiently and easily.

RESULT:

17 | P a g e
8. Using Simulink construct block diagram of dc servo
motor.

18 | P a g e
Comment:
1. Here In1 is a step input and Out1 is oscilloscope.
2. We can identify system parameters that individually
affect the DC gain and the time constant and vary
these parameters to experimentally verify the
change in system response.
3. At the output, we get change in output angle (𝜃)
w.r.t velocity signal at the input.

9.Using Simulink construct block diagram of Inverted


pendulum.

19 | P a g e
20 | P a g e
RESULT:

Comment:
1. The response of the system with PID controller and
standard coefficient value is an underdamped one.
2. The disturbance should be an impulse input. Since
impulse input is unavailable in Simulink, step input is
first derivative and then fed to the system as
disturbance.

21 | P a g e
10.Write a user defined functions in Simulink using s-
function.

%% creating m file with state space variable of a reactor %%


function dx = reactor(t,x,Tj) % model for reactor
Ca = x(1) ; % lbmol/ft^3
T = x(2) ; % oF
Ea = 32400 ; % BTU/lbmol
k0 = 15e12 ; % hr^-1
dH = -45000 ; % BTU/lbmol
U = 75 ; % BTU/hr-ft^2-oF
rhocp = 53.25 ; % BTU/ft^3
R = 1.987 ; % BTU/lbmol-oF
V = 750 ; % ft^3
F = 3000 ; % ft^3/hr
Caf = 0.132 ; % lbmol/ft^3
Tf = 60 ; % oF
A = 1221 ; % ft^2
ra = k0*exp(-Ea/(R*(T+460)))*Ca;
dCa = (F/V)*(Caf-Ca)-ra;
dT = (F/V)*(Tf-T)-(dH)/(rhocp)*ra-(U*A)/(rhocp*V)*(T-Tj);
dx =[dCa;dT];

22 | P a g e
%%Creating s function named reactor_sfcn to include in
Simulink using user defined function%%

function [sys,x0,str,ts]= reactor_sfcn(t,x,u,flag,Cinit,Tinit)


switch flag
case 0 % initialize
str=[] ;
ts = [0 0] ;
s = simsizes ;
s.NumContStates = 2 ;
s.NumDiscStates = 0 ;
s.NumOutputs = 2 ;
s.NumInputs = 1 ;
s.DirFeedthrough = 0 ;
s.NumSampleTimes = 1 ;
sys = simsizes(s) ;
x0 = [Cinit, Tinit] ;
case 1 % derivatives
Tj = u ;
sys = reactor(t,x,Tj) ;
case 3 % output
sys = x;
case {2 4 9} % 2:discrete
% 4:calcTimeHit
% 9:termination
sys =[];
otherwise
error(['unhandled flag =',num2str(flag)]) ;
end
23 | P a g e
RESULT:

24 | P a g e
Comment:
1. With the complexity of medium-size to large-
size nonlinear models, it may be more efficient
to use a set of differential equations written in
an m-file.
2. m-file can be accessed by Simulink through s
function block.
3. It mixes advantage of m-file which can be solved
using ode45 with graphical links in Simulink.
25 | P a g e
Conclusion
A very general and practical idea about the followings
have been developed during this course; such as:
➢ Basic uses of MATLAB as a mathematical tool.
➢ Solving electrical circuits and equation
programmatically.
➢ Uses of MATLAB for plotting and understanding
system responses.
➢ Solving differential equation programmatically
using ode45 and applying them to solve practical
dynamic systems such as mass-spring damper,
inverted pendulum etc.
➢ Using Simulink for graphical representation of real
system for easier solution and better insight.
➢ Integrating MATLAB programming into Simulink s-
function level-2 for user defined custom Simulink
block for more general uses of Simulink.

26 | P a g e

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