Академический Документы
Профессиональный Документы
Культура Документы
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
1|Page
1. Write a script file to convert Fahrenheit to Celsius.
%%Fahrenheit to Celsius%%
Comment-
1. Remembering the formula to convert Celsius to
Fahrenheit.
2. Creating m-file and running it for the result.
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.
4|Page
% With different value of R plotting undamped, overdamped,
underdamped and critically damped for the system given
syms s r
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
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.
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%%
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.)
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.
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.
22 | P a g e
%%Creating s function named reactor_sfcn to include in
Simulink using user defined function%%
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