You are on page 1of 20

Numerical Computation LabDepartment of Electrical & Computer Engineering

Lab 05- Newton’s Divided-Difference Formula


Task 01: Complete the divided difference table for the data used in Example 1 of Section 3.2, and
Table 3.10 reproduced in Table 3.10 and construct the interpolating polynomial that uses all this data.
0
f [x0, x1 ] = f [ x 1 ]− f [x0]
x1 − x0
Matlab Code:

Script File:

clc;
clear all;
close all;
x=input('Enter x: ');
f=input('Enter f(x): ');
n=length(x)-1;
xi=1.5;
y=newtondifference(x,n,xi,f);

Function File:

function[y]=newtondifference(x,n,xi,f)
x=transpose(x);
f=transpose(f);
F=zeros(n+1);
F(:,1)=f;
s=0;
for i=2:n+1;
p=1;
for j=2:i;
i1=[1;2;3;4;5];
F(i,j)=(F(i,j-1)-F(i-1,j-1))/(x(i)-x(i-j+1));
p=p*(xi-x(j-1));
FirstDiff=F(:,2);
SecondDiff=F(:,3);
ThirdDiff=F(:,4);
ForthDiff=F(:,4);
FifthDiff=F(:,5);
Fx=f;
end
s=s+(F(i,j)*p);
end
T=table(i1,x,Fx,FirstDiff,SecondDiff,ThirdDiff,ForthDiff,FifthDiff)
y=F(1,1)+s
end

Registration No: FA19-BSEE-007


Numerical Computation LabDepartment of Electrical & Computer Engineering

Output:

Enter x: [1 1.3 1.6 1.9 2.2]


Enter f(x): [0.7651977 0.6200860 0.4554022 0.2818186 0.1103623]

T =

5×8 table

i1 x Fx FirstDiff SecondDiff ThirdDiff ForthDiff FifthDiff


__ ___ _______ _________ __________ _________ _________ _________

1 1 0.7652 0 0 0 0 0
2 1.3 0.62009 -0.48371 0 0 0 0
3 1.6 0.4554 -0.54895 -0.10873 0 0 0
4 1.9 0.28182 -0.57861 -0.049443 0.065878 0.065878 0
5 2.2 0.11036 -0.57152 0.011818 0.068069 0.068069 0.0018251

y =

0.5118

Analysis:
The obtained result shows the difference system of f(x) for different values of x. As it can be observed from
the table the newton divided difference method use the technique of difference to get the exact value.

Registration No: FA19-BSEE-007


Numerical Computation LabDepartment of Electrical & Computer Engineering

Task 02: Solve Using Crout Factorization for Tridiagonal Linear Systems

Matlab Code:

Script File:

clc;
clear all;
close all;
A=input('Enter A: ');
B=input('Enter B: ');
a=[A,B];
n = length(A);
croutt(A,B,a,n)

Function File:

function x=croutt(A,B,a,n)

L = zeros(n);
z = zeros(n);
u = eye(n);
L(1,1)=a(1,1);
u(1,2)=(a(1,2))/L(1,1);
z(1)=(a(1,n+1))/L(1,1);
for i=2:n-1
L(i,i-1)=a(i,i-1);
L(i,i)=a(i,i)-(L(i,i-1)*u(i-1,i));
u(i,i+1)=(a(i,i+1))/L(i,i);
z(i)=(a(i,n+1)-(L(i,i-1)*z(i-1)))/L(i,i);
end
L(n,n-1)=a(n,n-1);
L(n,n)=a(n,n)-(L(n,n-1)*u(n-1,n));
z(n)=(a(n,n+1)-(L(n,n-1)*z(n-1)))/L(n,n);

x(n)=z(n);
for i=n-1:-1:1
x(i)=z(i)-(u(i,i+1)*x(i+1));

end
disp('Solution is: ')
x
linsolve(A,B);
end

Output:
Enter A: [2 -1 0 0; -1 2 -1 0; 0 -1 2 -1; 0 0 -1 2]

Enter B: [1; 0; 0; 1]

Solution is:

x =

1.0000 1.0000 1.0000 1.0000

Registration No: FA19-BSEE-007


Numerical Computation LabDepartment of Electrical & Computer Engineering

ans =

1.0000 1.0000 1.0000 1.0000

Registration No: FA19-BSEE-007


Numerical Computation LabDepartment of Electrical & Computer Engineering

Lab 06- LU Factorization of Linear System


Task 01:
a. Determine the LU factorization for matrix A in the linear system Ax = b, where

b. Then use the factorization to solve the system


x1 + x2 +3x4 = 8,
2x1 + x2 − x3 + x4 = 7,
3x1 − x2 − x3 +2x4 = 14,
−x1 + 2x2 + 3x3 − x4 = −7.
Matlab Code:

Script File:

clc;
close all;
b=input('Entre B: ');
n=input('Entre n: ');
a=input('Entre Matrix A: ');
LU(b,n,a)

Function File:

function x=LU(b,n,a)
global l u
l=zeros(n);
u=eye(n);
l(1,1)=a(1,1)/u(1,1);
if l(1,1)*u(1,1)==0
disp('Factorization impossible');
return
end
for j=2:n
u(1,j)=a(1,j)/l(1,1);
l(j,1)=a(j,1)/u(1,1);
end
for i=2:n-1
sum=0;
for k=1:i-1
sum= sum + l(i,k)*u(k,i);
end
l(i,i)=(a(i,i)-sum)/u(i,i);
if l(i,i)*u(i,i)==0;
disp('Factorization impossible')
break;
end
for j=i+1:n
sum_1=0;
sum_2=0;
for k=1:i-1
sum_1=sum_1+l(i,k)*u(k,j);
sum_2=sum_2+l(j,k)*u(k,i);

Registration No: FA19-BSEE-007


Numerical Computation LabDepartment of Electrical & Computer Engineering

end
u(i,j)=(a(i,j)-sum_1)/l(i,i);
l(j,i)=(a(j,i)-sum_2)/u(i,i);
end
end
sum_3=0;
for k =1:n-1
sum_3=sum_3+l(n,k)*u(k,n);
end
l(n,n)=(a(n,n)-sum_3)/u(n,n);
if l(n,n)*u(n,n)==0
disp('A is singular');
end
l;
u;
A=l*u;
a==A
y(1)=b(1)/l(1,1);
for i= 2:n
sum_4=0;
for j=1:i-1
sum_4=sum_4+l(i,j)*y(j);
end
y(i)=(b(i)-sum_4)/l(i,i);
end
x(n)=y(n)/u(n,n);
for i=n-1:-1:1
sum_5=0;
for j=i+1:n
sum_5=sum_5+u(i,j)*x(j);
end
x(i)=(y(i)-sum_5)/u(i,i);
end
disp('solve');
x;
x_1=linsolve(a,b)
end

Output(a):

Entre B: [1;1;-3;4]

Entre n: 4

Entre Matrix A: [1 1 0 3;2 1 -1 1;3 -1 -1 2;-1 2 3 -1]

ans =

4×4 logical array

1 1 1 1

1 1 1 1

1 1 1 1

1 1 1 1

solve

x_1 =

-0.3077

1.7692

Registration No: FA19-BSEE-007


Numerical Computation LabDepartment of Electrical & Computer Engineering

-0.0000

-0.1538

ans =

-0.3077 1.7692 0 -0.1538

Output (b):

Entre B: [8;7;14;-7]
Entre n: 4
Entre Matrix A: [1 1 0 3;2 1 -1 1;3 -1 -1 2;-1 2 3 -1]

ans =

4×4 logical array

1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1

solve

x_1 =

3.0000
-1.0000
-0.0000
2.0000

ans =

3 -1 0 2

Analysis:
Here output show two results one for the algorithm I use to solve the matrix system and second is the
solution of same matrix system that I get using linsolve command. The same result is showing that I
implement the algorithm correctly as it is giving the exact solution.

Registration No: FA19-BSEE-007


Numerical Computation LabDepartment of Electrical & Computer Engineering

Lab 07- Lagrange AND Natural Cubic Spline Method

Task 01: Implement Lagrange Interpolation Method for given example.


Matlab Code:

Script File:

clc;
clear all;
close all;
x=input('Entre x data point;');
y=input('Entre f(x)=');
xp=input('Entre xp for which value f(x)will evaluate=');
Lagrange(x,y,xp)

Function File:

function Lagrange(x,y,xp)
n=length(x)-1;
yp=0;
for i=1:n
p=1;
for j=1:n
if i~=j
p=p*((xp-x(j))/(x(i)-x(j)));
if i==j
end
end
end
yp=yp+(p*y(i));
end
yp

Output:

Entre x data point;[1 1.3 1.6 1.9 2.2]


Entre f(x)=[0.7651977 0.6200860 0.4554022 0.2818186 0.1103623]
Entre xp for which value f(x)will evaluate=1.5

yp =

0.5118

Registration No: FA19-BSEE-007


Numerical Computation LabDepartment of Electrical & Computer Engineering

Task 02: Implement Natural Cubic Spline Method for given example.
Matlab Code:

Script File:

clc
clear all;
close all;
xj=input('Enter the value of x: ');
f=input('Enter the value of f: ');
NCS(f,xj)

Function File:

function Natural(f,xj)
n=length(xj)-1;
h=zeros(1,n+1);
a=zeros(1,n+1);
c=zeros(1,n+1);
d=zeros(1,n+1);
for i=1:n
h(1,i)=xj(i+1)-xj(i);
end

for i=2:n
a(1,i)=((3/h(1,i))*(f(i+1)-f(i)))-((3/h(1,i-1))*(f(i)-f(i-1)));
end
l(1)=1;
u(1)=0;
z(1)=0;
for i=2:n
l(i)=2*(xj(i+1)-xj(i-1))-(h(1,i-1)*u(i-1));
u(i)=h(1,i)/l(i);
z(i)=(a(1,i)-(h(1,i-1)*z(i-1)))/l(i);
end
l(n+1)=1;
z(n+1)=0;
c(n+1)=0;

for j=n:-1:1
c(j)=z(j)-(u(j)*c(j+1));
b(j)=((f(j+1)-f(j))/h(1,j))-(h(1,j)*(c(j+1)+2*c(j))/3);
d(j)=(c(j+1)-c(j))/(3*h(1,j));
end

for j=1:n
f(j);
b(j);
c(j);
d(j);
end
size=1:n+1;
b(1,21)=0;
xj=xj';
aj=f';
bj=b';
cj=c';
dj=d';
T=table(xj,aj,bj,cj,dj)

Registration No: FA19-BSEE-007


Numerical Computation LabDepartment of Electrical & Computer Engineering

Output:

Enter the value of x: [0.9 1.3 1.9 2.1 2.6 3.0 3.9 4.4 4.7 5.0 6.0 7.0 8.0 9.2
10.5 11.3 11.6 12.0 12.6 13.0 13.3]
Enter the value of f: [1.3 1.5 1.85 2.1 2.6 2.7 2.4 2.15 2.05 2.1 2.25 2.3 2.25
1.95 1.4 0.9 0.7 0.6 0.5 0.4 0.25]

T =

21×5 table

xj aj bj cj dj
____ ____ _________ _________ __________

0.9 1.3 0.53962 0 -0.24765


1.3 1.5 0.42075 -0.29718 0.94691
1.9 1.85 1.0868 1.4073 -2.9564
2.1 2.1 1.2949 -0.36657 -0.44663
2.6 2.6 0.5934 -1.0365 0.44505
3 2.7 -0.022191 -0.50246 0.17416
3.9 2.4 -0.50341 -0.032226 0.078076
4.4 2.15 -0.47708 0.084888 1.3142
4.7 2.05 -0.071316 1.2676 -1.5812
5 2.1 0.26234 -0.15546 0.043115
6 2.25 0.080776 -0.026109 -0.0046663
7 2.3 0.014558 -0.040108 -0.02445
8 2.25 -0.13901 -0.11346 0.017471
9.2 1.95 -0.33583 -0.050564 -0.012728
10.5 1.4 -0.53183 -0.1002 -0.020325
11.3 0.9 -0.73118 -0.14898 1.2134
11.6 0.7 -0.49295 0.94308 -0.83927
12 0.6 -0.14134 -0.064048 0.036382
12.6 0.5 -0.1789 0.0014396 -0.44797
13 0.4 -0.39277 -0.53613 0.5957
13.3 0.25 0 0 0
Analysis:

From the above results I found that the Lagrange Interpolation method uses a Lagrange Polynomial that takes some
values at an arbitrary point to find new data points within a fixed range of data points. And in the Natural Cubic Spline
two parameters are added at the boundaries (at each end).

Registration No: FA19-BSEE-007


Numerical Computation LabDepartment of Electrical & Computer Engineering

Task 01: Write statement of task1 here


Matlab Code:

Output:

Analysis:

Simulink Model:

Output:

Analysis:

Task 02: Write statement of task2 here


Matlab Code:

Output:

Analysis:

Simulink Model:

Output:

Analysis:

Registration No: FA19-BSEE-007


Numerical Computation LabDepartment of Electrical & Computer Engineering

TABLE ERROR! NO TEXT OF SPECIFIED STYLE IN DOCUMENT.- 1: READING TABLE

Increasing voltage on Set point 1 for Start Decreasing voltage on Set point 1 for Stop
Sr. No.
of Motor (V) of Motor (V)

1.

2.

3.

Average
Value

Registration No: FA19-BSEE-007


Numerical Computation LabDepartment of Electrical & Computer Engineering

Lab 04- On – Off Control of the Level through the Pressure Sensor
TABLE 4-2: READING TABLE

Hysteresis %

0% 15% 30%

Set point (cm)

Lower Limit Set point (cm)

Up-rising time of the level (sec)

Upper Limit Set point (cm)

Lowering time of the level (sec)

Registration No: FA19-BSEE-007


Numerical Computation LabDepartment of Electrical & Computer Engineering

Lab 05- On – Off Control of the Pressure through the Solenoid Valve Driver
TABLE 5-3: READING TABLE

Hysteresis %

0% 15% 30%

Set point (cm) 1cm 1cm 1cm

Lower Limit Set point (cm) 6.1cm 5.8cm 6cm

Up-rising time of the level (sec) 25.8sec 30.5sec 39.1sec

Upper Limit Set point (cm) 6.5cm 8cm 8.5cm

Lowering time of the level (sec) 19.1sec 4.9sec 11.2sec

Registration No: FA19-BSEE-007


Numerical Computation LabDepartment of Electrical & Computer Engineering

Lab 06- State Space Representation using MATLAB and Response of


Second Order System
Task 01: Write statement of task1 here
Matlab Code:

Output:

Analysis:

Simulink Model:

Output:

Analysis:

Task 02: Write statement of task2 here


Matlab Code:

Output:

Analysis:

Simulink Model:

Output:

Analysis:

Registration No: FA19-BSEE-007


Numerical Computation LabDepartment of Electrical & Computer Engineering

Lab 07- Analysis of Steady State Error using MATLAB and Routh Hurwitz
Criteria using MATLAB
Task 01: Write statement of task1 here
Matlab Code:

Output:

Analysis:

Simulink Model:

Output:

Analysis:

Task 02: Write statement of task2 here


Matlab Code:

Output:

Analysis:

Simulink Model:

Output:

Analysis:

Registration No: FA19-BSEE-007


Numerical Computation LabDepartment of Electrical & Computer Engineering

Lab 08 - Closed Loop Proportional Control & Closed Loop Proportional -


Integral Control of the Pressure

READING TABLE FOR CLOSED LOOP DYNAMICS RESPONSE OF PROPORTIONAL CONTROL


Tim
e 10 12 13 15 16 18 19 21 22 24
0 15 30 45 60 75 90
(sec 5 0 5 0 5 0 5 0 5 0
)
Kp
= 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
25 13 02 2 26 3 32 34 35 36 36 37 38 38 39 39 39 40
%
Kp
= 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
50 14 43 52 57 59 60 61 61 62 62 62 63 63 63 64 64 64
%
Kp
= 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
75 15 62 68 70 72 73 74 74 74 75 75 75 75 75 75 76 76
%
Kp
= 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
100 20 72 76 78 79 80 80 80 81 81 81 81 81 81 82 82 82
%

STEADY STATE ERROR: FOR KP (AT 25% = 0.6, AT 50% = 0.36, AT 75% = 0.24, AT 100% = 0.18)

READING TABLE FOR CLOSED LOOP DYNAMICS RESPONSE OF PROPORTIONAL-INTEGRAL CONTROL


Tim
e 10 12 13 15 16 18 19 21 22 24
0 15 30 45 60 75 90
(sec 5 0 5 0 5 0 5 0 5 0
)
Kp
=
25
0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
%
12 28 55 61 66 70 73 75 77 79 80 81 83 84 84 85 86
Ki =
25
%
Kp
=
25
0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
%
12 39 67 73 77 82 84 86 88 90 91 92 93 94 94 95 96
Ki =
50
%
Kp 0. 1. 1. 1. 1. 1. 1. 2 2. 2. 2. 2. 2. 2. 2. 2. 2.
= 13 51 79 89 93 96 98 02 03 03 04 04 05 05 05 06

Registration No: FA19-BSEE-007


Numerical Computation LabDepartment of Electrical & Computer Engineering

25
%
Ki =
75
%
Kp
=
25
1. 1. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.
%
68 95 03 09 11 13 14 16 16 17 17 17 17 17 17 17
Ki =
100
%

STEADY STATE ERROR: FOR KI (AT 25% = 0.14, AT 50% = 0.04, AT 75% = -0.06, AT 100% = -0.16
Where Kp is constant at 25%

Registration No: FA19-BSEE-007


Numerical Computation LabDepartment of Electrical & Computer Engineering

Lab 09 - Closed Loop Proportional-Derivative Control of the Pressure


TABLE 9- 4: READING TABLE FOR CLOSED LOOP DYNAMICS RESPONSE OF PROPORTIONAL-DERIVATIVE
CONTROL
Time 1 3 4 6 7 9 10 12 13 15 16 18 19 21 22 24 25 27 28 30
0
(sec) 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0
Kp =
25%
Kd =
25%
Kp =
25%
Kd =
50%
Kp =
25%
Kd =
75%
Kp =
25%
Kd =
100
%

STEADY STATE ERROR: _________________

Registration No: FA19-BSEE-007


Numerical Computation LabDepartment of Electrical & Computer Engineering

Lab 10 - Closed Loop Proportional-Integral-Derivative Control of the


Pressure
TABLE 10-5: READING TABLE FOR CLOSED LOOP DYNAMICS RESPONSE OF PID CONTROL
Tim
1 3 4 6 7 9 10 12 13 15 16 18 19 21 22 24 25 27 28 30
e 0
5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0
(sec)

Kp =

Ki =

Kd =

STEADY STATE ERROR: _________________

Registration No: FA19-BSEE-007

You might also like