Professional Documents
Culture Documents
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
Output:
T =
5×8 table
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.
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 =
ans =
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);
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
ans =
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
solve
x_1 =
-0.3077
1.7692
-0.0000
-0.1538
ans =
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 =
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.
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:
yp =
0.5118
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)
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
____ ____ _________ _________ __________
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).
Output:
Analysis:
Simulink Model:
Output:
Analysis:
Output:
Analysis:
Simulink Model:
Output:
Analysis:
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
Lab 04- On – Off Control of the Level through the Pressure Sensor
TABLE 4-2: READING TABLE
Hysteresis %
0% 15% 30%
Lab 05- On – Off Control of the Pressure through the Solenoid Valve Driver
TABLE 5-3: READING TABLE
Hysteresis %
0% 15% 30%
Output:
Analysis:
Simulink Model:
Output:
Analysis:
Output:
Analysis:
Simulink Model:
Output:
Analysis:
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:
Output:
Analysis:
Simulink Model:
Output:
Analysis:
STEADY STATE ERROR: FOR KP (AT 25% = 0.6, AT 50% = 0.36, AT 75% = 0.24, AT 100% = 0.18)
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%
Kp =
Ki =
Kd =