Professional Documents
Culture Documents
NEOM Lab Manual - Experiment 1 To 5
NEOM Lab Manual - Experiment 1 To 5
NEOM Lab Manual - Experiment 1 To 5
(Part-I)
1
EXPERIMENT # 1
1. Introduction:
MATLAB program is a high level interactive graphics oriented high level software
language widely used in engineeringdesign, modeling, simulation, R&D, etc. This
program is having numerous features like math computation, numerical analysis
,electrical circuit analysis, Algorithm development, scientific and engineering
graphics, data analysis and visualization, application development, etc . Further,
the program is having another significant interactive built-in ‘SIMULINK’
program which is operated as ‘drag and drop tool’ for the similar studies.
2. Objective:
In this experiment students will learn the art of MATLAB programming and apply
this for studies and analysis of various numerical optimization techniques Runge-
Kutta 2nd and 4th order methods.
3. Requirements of Apparatus:
(i) LAPTOP/Desktop PC
(ii) MATLAB/Simulink Program by MathWorks Inc.
In order to study and learn the MATLAB/SIMULINK program, it is to be
downloaded (whatever latest version is available) in the PC/Labtop of each
student. The memory requirement for the program may be around 15-20 GW.
Once loaded and operationalize the software, students can work on that.
2
4.1 Typical Command window on the screen:
3
4.2 File Names:
A file may contain a collection of commands & functions. To run, enter m-file,
without .m, e.g.
>> myfile
Pause Stops the execution temporarily of m-files; resume execution after pressing
any key
Pause (n) Stops the execution temporarily of m-files for n-seconds before continuing;
Hold on
Hold off
4
‘ ‘ Arrow key : Display previous line
*
‘ ’ Wild card character: Display variables that match a pattern
;
‘ ‘Semicolon: At the end of a command provides no display of the output
‘ : ‘ Colon : used to create vectors & matrices - for row/column vectors - to specify
sub-matrices &vectors - to specify a range of values in a variable (Loop)
perform iterations -
5.1 VARIABLES
Symbolic variable :
To create symbolic expressions, first create symbolic variables, and then use operations on them.
The syms command is a convenient shorthand for the ‘sym’ syntax. Declare variables x, y, m as
symbolic i.e. syms x, y, m.
For example:
>>syms x y
>>E = x^2+6*x+7;
>>F = subs(E,x,y)
F=
y^2+6*y+7
5
Arithmetic Operations:
(ii) Array Arithmetic operations: carried out element by element and can be
used with multidimensional arrays.
Trigonometric functions:
6
Some Built-in Functions:
7. I/O Commands:
‘input’ command display text on the screen,waits for the user to enter
something from the keyboard and stores in the specified variable e.g
X=input (‘Pl enter value of x=’)
>> X=input (‘Pl enter value of x=’)
>>Pl enter value of x=
Output commands
e,g. disp(A),disp (‘ Hallo, How are You’)
[display values or text on the window]
7
Formats for fprintf
8
9.0 M-Files:
[ Note: Do not give script file name as a MATLAB command or function or same
variable name it computes]
9
User Defined Function File (.m file) & Function Header
Syntax of a function
[This command should be written at the beginning of the m-file and saved with a file name
same as the function name (e.g. functionname1) ;Output parameter 0 or 1, can omit [ ] ; 2
or more parameters separated by commas(,) ]
Ex-1:
10
Ex-3
Coding:1
% Find reactance of a reactor
function y = react_C (c,f)
y(1)=2*pi*f;
w=y(1);
y(2)=1/(w*c);
% main prog-1 script file
c=4;
f=50;
y=react_C(c,f)
11
Ex-4
Coding:2
%IMPEDANCE calculates Xc, Xl and Z(magnitude) and Z(angle) of the RLC
connected in %series
%IMPEDANCE(R,C,L,W) returns Xc, Xl and Z (mag) andZ(angle) at W rad/s
% function file ‘impedance.m’
function x=impedance(r,c,l,w)
if nargin<4 ;%NARGIN Number of function input arguments.
error('not enough input arguments')
% function file end
% Main Script file
x(1) = 1/(w*c);
x(2) = w*l;
Zt = r + (x(2) - x(1))*i;
x(3) = abs(Zt);
x(4)= angle(Zt);
% main script file
R=input('Enter R: ');
C=input('Enter C: ');
L=input('Enter L: ');
w=input('Enter w: ');
y=impedance(R, C, L, w);
fprintf ('\n The magnitude of the impedance at %.1f rad/s is %.3f ohm\n', w,
y(3));
fprintf ('\n The angle of the impedance at %.1f rad/s is %.3f degrees\n\n', w,
y(4));
Ex-5
Coding:3
% function without brakets- whether the give no. lies between 10 & 100 or not
function inrange
a=input(‘enter the number=\n’);
If ((a>10) & (a<100))
disp(‘The number lies betn 10 & 100’);
else
disp(‘The number does not lie betn 10 & 100’);
12
9.1 Anonymous function :
The sign @is the MATLAB operator that constructs a function handle giving a
means of invoking the function. Stores the value of the handle in variable
fhandle
Ex.
Coding:
a=1.3; b=0.2; c=30;
parabola=@(x) a*x.^2+b*x+c;
fplot (parabola, [-25 25])
13
11.0 Solving Differential Equations:
Syntax: dsolve('eq1','eq2',...,'cond1','cond2',...,'v')
eq1, eq2,... are the differential equations wrt the independent variable ,v.
Letter ‘D’ denotes differentiation of the function ‘y’ with respect to the
independent variable (t). Dy is dy/dt, D2y is d2y /dt2 , D3y is d3y /dt3. ...Dny is dny
/dtn, indicating first ,second, third and nth derivative of the function (y) wrt the
independent variable,t.
Example:
>>dsolve(’D2y=c^2*y’,’y(0)=1’,’Dy(0)=0’)
ans = 1/2*exp(c*t)+1/2*exp(-c*t)
>>dsolve(’Dx=3*x+4*y’,’Dy=-4*x+3*y’, ’x(0)=0’,’y(0)=1’)
[x,y] =
x = exp(3*t)*sin(4*t),
y = exp(3*t)*cos(4*t)
14
11.1 For higher order differential equation, the equation is to be transformed
into a set of first-order equations to use ODE Solvers. For example:
15
7.0 Runge-Kutta 2 and RK4 Methods:
dy/dx = f(x,y)
7.1 Pre-requisites
Note that first or second order approximation often suffice in the close neighborhood
of 0.
where Δ = − 0, Δ =y−y0, and all partial derivatives are computed at the operating
point: ( 0, 0).
16
Euler’s Method:
y1 y0 f ( x, y )dx............(1)
x0
h
y1 y0 f ( x0 , y0 ) f ( x1 , y1 ) (4)
2
h
y1( n1) y0 f ( x0 , y0 ) f ( x1 , y1( n) ) (5), where, n 0,1, 2
2
17
Backward Euler’s Method:
We start at the initial point (x0, y0). Here, the derivative is to be evaluated at
point (x + h) instead of at point h. To obtain the next point, (x1, y1), we take the
derivative at x1 (not at x0) and extrapolate it at point (x0, y0).
Now set,
k1 h f 0
and , k2 h f ( x0 h, y0 k1 )
The equ (6) becomes,
1
y1 y0 k1 k2 (7)
2
This is the 2nd order Runge-Kutta formula. The error inthis formula can be
shown to be of order of h3 by expanding both sides by Taylors’s series.
18
7.2.2 Runge-Kutta 4thOrder Method
In each step the derivative is evaluated 4-times. Once at the initial point, twice at
trial midpoints, and once at a trial endpoint. From these derivatives the final
function value (shown as a filled dot).
1
y1 y0 (k1 2 k2 2 k3 k4 ) (10)
6
Where,
k1 h f ( x0 , y0 )
1 1
k2 h f ( x0 h, y0 k1 )
2 2
1 1
k3 h f ( x0 h, y0 k2 ) (11)
2 2
k4 h f ( x0 h, y0 k3 )
19
7.3 Example of Runge-Kutta Method
Solve : y’=f(t,y)=y−t2+1
y(0) = 0.5
for0 ≤ t ≤ 2 and h=0.5
Exact solution for the problem is y = t2 + 2t + 1 –½(et)
y3=y2+(k1+k2)/2
Step 4: t4 = 2.0
y4=y3+(k1+k2)/2
Step 0 t0 = 0, y0 = 0.5.
Step 1 t1 = 0.5
k1 = hf (t0, y0) = 0.5f (0, 0.5) = 0.75
k2 = hf (t0 + h/2, y0 + k1/2) = 0.5f (0.25, 0.875) = 0.90625
k3 = hf (t0 + h/2, y0 + k2/2) = 0.5f (0.25, 0.95312) = 0.94531
k4 = hf (t0 + h, y0 + k3) = 0.5f (0.5, 1.44531) = 1.09765
y1 = y0 + (k1 + 2k2 + 2k3 + k4)/6 = 1.42513
Step 2 t2 = 1
k1 = hf (t1, y1) = 0.5f (0.5, 1.42513) = 1.08756
k2 = hf (t1 + h/2, y1 + k1/2) = 0.5f (0.75, 1.96891) = 1.20320
k3 = hf (t1 + h/2, y1 + k2/2) = 0.5f (0.75, 2.026733) = 1.23211
20
k4 = hf (t1 + h, y1 + K3) = 0.5f (1, 2.65724) = 1.32862
y2 = y1 + (k1 + 2k2 + 2k3 + k4)/6 = 2.63960
Step 3: t3 = 1.5
k1 = hf(t2, y2)= 0.5f(1, 2.63960) = 1.31980
k2 = hf (t2 + h/2, y2 + k1/2) = 0.5f (1.25, 3.29950) = 1.36850
k3 = hf (t2 + h/2, y2 + k2/2) = 0.5f (1.25, 3.32385) = 1.38067
k4 = hf (t2 + h, y2 + K3) = 0.5f (1.5, 4.02027) = 1.38513
y3 = y2 + (k1 + 2k2 + 2k3 + k4)/6 = 4.00681
Step 4: t4 = 2
k1 = hf (t3, y3) = 0.5f (1.5, 4.00681) = 1.37840
k2 = hf (t3 + h/2, y3 + k1/2) = 0.5f (1.75, 4.69602) = 1.31676
k3 = hf (t3 + h/2, y3 + k2/2) = 0.5f (1.75, 4.66519) = 1.30134
k4 = hf (t3 + h, y3 + K3) = 0.5f (2, 5.30816) = 1.15408
y4 = y3 + (k1 + 2k2 + 2k3 + k4)/6 = 5.30160
0.0 0
0.5 0.000509
1.0 0.001256
1.5 0.002336
2.0 0.003866
9. Observations
Plot the characteristics of RK2, RK4 results and True solution on the same plot.
Also plot the error curve in RK2 method iro true solution and error curve in
RK4 method iro true solution on the same plot. Give comments on the
performance of RK2 and RK4 methods.
10. Appendix :
Coding:
% Runge Kutta 4 Solution
%Rungekutta-4 soln for y’=y-t.^2+1
%function file 'f'
function f=f(t,y)
f=y-t.^2+1;
0<=t<=2,x(0)=0.5
x_true=t.^2+2*t+1.0-0.5*exp(t)% True solution
%function file 'f' ends
21
%Main file
clc
h=0.25; % step size
t(1)=0;% zero time
x(1)=0.5;% initial condition
y_true=zeros(1,8)
for i=1:8
k1=h*f(t(i),x(i));
k2=h*f(t(i)+h/2,x(i)+k1/2);
k3=h*f(t(i)+h/2,x(i)+k2/2);
k4=h*f(t(i)+h,x(i)+k3);
x(i+1)=x(i)+(k1+2*k2+2*k3+k4)/6;
t(i+1)=t(i)+h;
y_true=t.^2+2*t+1.0-0.5*exp(t)
end
%t=[0:h:2]
%y_true=t.^2+2*t+1.0-0.5*exp(t) % Solve & plot by dsolve also
plot(t,x,'k-',t,y_true,'m+')
xlabel('t')
ylabel('x')
legend('Runge Kutta 4th Order:TrueSoln vs. RK soln.')
%main file end
22
EXPERIMENT # 2
1. Introduction:
Simulink includes a comprehensive block library of toolboxes for both linear and
nonlinear analyses. As Simulink is an integral part of MATLAB, it is easy to switch
back and forth during the analysis process. It is required to explore or browse the
organisation of the toolboxes and scan their contents according to their categories.
2. Objective:
This experiment is aimed of learning the art of developing Simulink models for
electrical engineering applications. Students are to learn the simulation procedures
using Simulink tool boxes and to apply for electrical circuit simulation and armature
controlled DC motor. The typical Simulink models as illustrated in this experiment
should be tested and develop a new model of an electrical circuit.
3. Requirements of Apparatus:
(i) LAPTOP/Desktop PC
(ii) MATLAB/Simulink Program by MathWorks Inc.
In order to study and learn the Simulink program, it is to be downloaded (whatever latest
version is available) in the PC/Labtop of each student. Once loaded and operationalize
the software, students can work on the Simulink applications.
Simulink includes a comprehensive block library of toolboxes for both linear and
nonlinear analyses. As Simulink is an integral part of MATLAB, it is easy to switch
back and forth during the analysis process. It is required to explore or browse the
organisation of the toolboxes and scan their contents according to their categories. The
construction of a model is simplified with click-and-drag mouse operations.
4.1 Getting Started
While MATLAB program is loaded, it displays the following menu with prompt (⪢)
sign in the command screen and type simulink and enter as given below:
>> simulink
The various Simulink toolboxes can be explored through Library Browser (clicked multi-
coloured red-blue box of four squares as appeared in the menu bar). The Simulink
libraries will be available and cann scan their contents according to their categories.
For example, while browsing the machine (highlighted below on the LHS) tool boxes,
various machine models will be available as shown below. Similarly, other categories
of elementary models can be explored through ‘Simscape/Power Systems/Power
System components/.
4.3 Simulink file name
All Simulink model file will have an extension ".mdl" similar to how MATLAB recognizes
files with the extension .m as an MFile.
Simulink opens with the Library Browser. you will find several toolboxes categorized
on the left side of window pane shown above. Some general blocks used for
mathematical modelling.
To build your model, browse the respective components and add them (blocks) to
your model window. To move the blocks around, simply click on it and drag it to a
desired location. Add a Scope from Sinks library, an Integrator block from
Continuous library, and a Mux block from Signal Routing library ,etc.
If you wish to locate a block knowing its name, you may enter the name in the
SEARCH WINDOW and Simulink will bring up the specified block. To move the
blocks around, simply click on it and drag it to a desired location. Once you have
dragged over all necessary blocks , the workspace window should consist of the all
desired components. You may remove (delete) a block by simply clicking on it once
to turn on the selected mode (with four corner boxex) and use DEL key or keys
combination CTRL-X. To display block's parameters, double click on the block.
To establish connections between the blocks, move the cursor to the output port
represented by ‘>’ sign on the block. Left click while holding down the control key
and drag from source port to a destination port. The cursor will turn into a cross ‘+’
enabling to make connection between blocks.
An example is demonstrated where blocks like sine wave, integrator, mux and scope
are dragged from the library and they are connected as given below, and the output
is obtained after clicking the ‘play’ button in the menubar. Click the scope toolbox
to see the nature of the output based on the parameter set in the sine wave block
(a source block).
You now may run the simulation of the simple system above by clicking on the play
button . Double click on the Scope block to display of the scope.
4.6 Development of Simulink model
4.6.1 Case-1: RLC circuit with unit step voltage input (u0t).
Rt dvc
vcdt + C = 1- vc
L0 dt
t dv
for → R, C, L = 1 → vc = 1- vcdt + c
0 dt
[A]
State-space vector representation:
[B]
[D]
[C]
di
The differential equation of the armature circuit (KVL): La a + iaRa + e = ea …..(1)
dt b
While the DC motor is operated in the linear range of the magnetization curve, the air
gap flux is proportional to the field current i.e.
The torque developed by the motor is proportional to the product of the armature
current and air gap flux, as given below. As the field current is constant, the torque is
proportional to the armature current.
Tm = K1ia = K1iaKf if = KT ia ….........(4) , where KT=K1Kf = a constant
The armature current produces the torque that is applied to the inertia and friction to
obtain the torque equation,
J d + f d = Tm = K ia ……………………(5)
2
dt 2 dt t
Laplace Transform of (1),(3) and (5) become (with initial conditions being zeros),
E (s ) = K (s )LLLLLLLLLLLLLLLLLLLLL(6)
b b
E (s ) − E (s ).
(Las + Ra )Ia (s ) = Ea (s ) − E (s )Lor ,Ia (s ) = a b LLL(7)
b (Las + Ra )
K
(Js2 + fs ) (s ) = Tm (s ) = K Ia (s )Lor ,s (s ) = t I (s ) = (s )L(8)
t (Js + f ) a
The transfer function of the system is worked out from (6), (7) & (8):
K
G(s ) = (s ) = t LLLLL(9)
Ea (s ) s[(Ra + sLa )(Js + f ) + K K ]
t b
Output:
Torque developed by motor , armature current, angular displacement and angular velocity can be
observed by clicking the respective ‘scope’ block.
4.6.3 Case-3:
The RLC electrical circuit (refer to case-1) is to be simulated by considering the
same R,L and C parameters. With an input voltage of u= 2 sin (t+30°) -5 cos (t+60°),
note the voltage profile across the capacitor.
CONCLUSION:
Experiment No. 3
3.1 Aim of Experiment: Develop equation for current flowing through RL circuits and solve
using Matlab/Simulink.
3.2 Objective: Learning to solve the electrical circuits using both Matlab and Simulink
environment.
3.3 Prerequisites:
1. Knowledge of Circuit Theory
2. Knowledge of basic C/C++ programming
3. Knowledge of Numerical Methods
4. Knowledge of Simulink
3.5 Theory:
3.5.1 Introduction to Runge-Kutta Method:
First order differential equation:
The above equation can be solved by many methods namely Euler method, Picard method,
Taylor series method , Runge-Kutta method.
Runge Kutta method is a popular iteration method of approximating solution of ordinary
differential equations. Developed around 1900 by German mathematicians C. Runge and M.
Wilhelm Kutta. Also known as RK method is based on solution procedure of initial value
problem in which initial conditions are known.
This method has advantage over Taylor Series method that we don’t have to calculate higher
order derivatives.
The philosophy behind Runge-Kutta methods is to consider a weighted average of slopes or
approximate slopes at a number of points.
3.5.2 Working Rule:
For finding the increment k of y corresponding to an increment h of x by Runge-Kutta method
from
Calculate successively
Finally compute,
Figure 1
3.6 Procedure:
1. Open Matlab.
2. Open a new script file.
3. Then initialize all the constants in the system.
4. Then initialize the value of step size, range of the time and also define the initial
conditions.
5. Then define the differential equation in the script either using function file or using inline
command.
6. Then define the number of iterations for which you want to solve the equation. Try to go
for large value as it will reduce error.
7. For the given number of iterations, run a For Loop, and for each iteration solve for K1,
K2, K3, K4 and K. Update the value of I correspondingly with the calculated deviation,
K.
8. Display the final value of current I obtained after the iteration as a final result.
3.7 Result:
The result of the written code can be seen in the Command Window. This result can be
verified by simulating a similar circuit in Simulink environment and viewing the result in the
scope.
EXPERIMENT-4
4.1 OBJECTIVE
Solving root finding problems in one dimension by Newton-Rampson and Regula-Falsi methods
to solve algebraic equations applicable to electrical engineering.
4.3 THEORY
4.3.1 Newton-Raphson method
Newton-Raphson method, named after Isaac Newton and Joseph Raphson, is a popular iterative
method to find the root of a polynomial equation. It is also known as Newton’s method. Based
on the first few terms of Taylor’s series, Newton-Raphson method is more used when the first
derivation of the given function/equation is a large value. It is often used to improve the value of
the root obtained using other rooting finding methods in Numerical Methods.
Repeating the above process for xn and xn+1 terms of the iteration process, we get the general
iteration formula for Newton-Raphson Method as:
xn+1 = xn – f(xn)/f’(xn)
This formula is used in the program code for Newton Raphson method in MATLAB to find
new guess roots.
1. Check if the given function is differentiable or not. If the function is not differentiable,
Newton’s method cannot be applied.
2. Find the first derivative f’(x) of the given function f(x).
3. Take an initial guess root of the function, say x1.
4. Use Newton’s iteration formula to get new better approximate of the root, say x2
x2 = x1 – f(x1)/f’(x1)
5. Repeat the process for x3, x4… till the actual root of the function is obtained, fulfilling
the tolerance of error.
1. Start
2. Read x, e, n, d
*x is the initial guess
e is the absolute error i.e the desired degree of accuracy
n is for operating loop
d is for checking slope*
3. Do for i =1 to n in step of 2
4. f = f(x)
5. f1 = f'(x)
6. If ( [f1] < d), then display too small slope and goto 11.
*[ ] is used as modulus sign*
7. x1 = x – f/f1
8. If ( [(x1 – x)/x1] < e ), the display the root as x1 and goto 11.
*[ ] is used as modulus sign*
9. x = x1 and end loop
10. Display method does not converge due to oscillation.
11. Stop
Consider three points on abscissa a1, b1 and c1 with respective ordinates as f(a1), f(b1) and f(c1). From the
principle of similar triangle, we have:
[f(b1) – f(a1)] /[ b1 – a1] = [ f(b1) – 0 ] / [ b1 – c1]
1. Start
2. Read values of x0, x1 and e
*Here x0 and x1 are the two initial guesses
e is the degree of accuracy or the absolute error i.e. the stopping criteria*
3. Computer function values f(x0) and f(x1)
4. Check whether the product of f(x0) and f(x1) is negative or not.
If it is positive take another initial guesses.
If it is negative then goto step 5.
5. Determine:
x = [x0*f(x1) – x1*f(x0)] / (f(x1) – f(x0))
6. Check whether the product of f(x1) and f(x) is negative or not.
If it is negative, then assign x0 = x;
If it is positive, assign x1 = x;
7. Check whether the value of f(x) is greater than 0.00001 or not.
If yes, goto step 5.
If no, goto step 8.
*Here the value 0.00001 is the desired degree of accuracy, and hence the stopping
criteria.*
8. Display the root as x.
9. Stop
4.4 PROCEDURE
(i) Open MATLAB
(ii) Open new M-file
(iii) Type the program
(iv) Save in current directory
(v) Compile and Run the program
(vi) For the output see command window\ Figure window
Experiment 5:
Optimization is essentially about finding the best solution (minima or maxima) to a given
problem from a set of feasible solutions. Number of different methods exist in the available
literature. In this experiment we will be discussing about the Fibonacci and Golden section
methods.
Fibonacci Method: The Fibonacci search is based on the sequence of Fibonacci numbers which
=1
=1
Note: Condition for applying the Fibonacci method is that the function should be unimodal
in a given interval (i.e only one maxima/minima should be there). In other words, the
Procedure:
Let y=f(x) be a given function. We are considering a one variable function and a minimization
problem. Let initial interval of uncertainty is x ∈ [a, ] be given (meaning in this interval we
need to find the value of x for which the value of y is minimum). The objective is to narrow
down this interval iteration by iteration. Initially, we do not know how many iterations are
required to perform for getting the solution. But later we will understand how to deduce it
analytically. In each iteration we take two values of the x. These two values are symmetrical with
respect to the end points of the interval (of the given iteration) that is, they are equidistant from
the end points of the interval. The algorithm is to develop some way of finding these
and
x=a
x=b
Then conduct a two-point test, that is, evaluate and . If then delete
the interval on the right side of the and if then delete the interval on the left
2nd iteration: Let us assume that then in this iteration new initial interval would
be . So, one of the end points is changed and the initial interval is reduced from to
x=b
and in general
where k<=n.
and
The above steps are repeated till the last iteration is reached. Further note that out of the
two new points in any iteration one of the points in it will be equal to one of the points
obtained in the previous iteration. The reduction ratio (RR) is nothing but the ratio of the
And as we know
So, given the desired accuracy (last interval size) and the initial interval we can find RR and
will be equal to RR. From we can deduce the value of n. For example, if =20 then we
know for n=7. So, we will carry out n+1 iteration (function evaluations). At the last
iteration we hope that the two new points will converge to each other (and hence the
solution). If they do not converge then the final interval will be so small that the mid-point
given by , r stands for the radius and h stands for the height. The volume
of the tank is given by . So, on substituting the value of the volume we can
Hint: At the 7th iteration the two points converge approximately to a value equal to 0.85
Golden section Method: In the Fibonacci method the percentage of reduction in the interval
when we go from iteration to the other is not same since gives different values for different
values of k. In golden section/ratio method this percentage of reduction is fixed while moving
from one iteration to the another. That is, the new interval is given by
for k=1,2,3….n
where 0.618 or 1.618 is called as a golden number (and see . Rest all the