Computational LAB
Computational LAB
Nov 28 2022
Experiment 1
Bisection method(Bracketing Method)
Objective : Write a program for bisection method in MATLAB
Example find the roots of a function �(�) = �3 + �2 − 3� − 3 = 0 using Bisection method with error
tolerance of 0.001%
plot in matlab program to find the possible intervals where roots can exist
use those intervals and to write the above algorithms to a code
x=-2:0.1:2;
y=x.^3+x.^2-3.*x-3;
plot(x,y,'r');
xlabel('x-axis');
ylabel('y-axis');
gtext('f(x)=x.^3+x.^2-3.*x-3')
grid
1
Computational Laboratory Exercises Prepared By Belaineh Esk. Nov 28 2022
From the above figure, the roots lie in the intervals (-2,-1.5),(-1.5,-0.5) and (1.5 ,2)
format long
clc;
clear all;
close all;
a=input('enter lower interval');
b=input('enter upper interval');
i=1;
f=inline('x.^3+x.^2-3.*x-3','x');
err=5;
va1=zeros(20,1);
va2=zeros(20,1);
va3=zeros(20,1);
err2=zeros(20,1);
if f(a).*f(b)>0
disp("Error in interval");
else
while(err>0.0000000001)
c=(a+b)./2;
if f(a).*f(c)>0
a=c;
else
b=c;
end
va1(i,1)=a;
va2(i,1)=b;
va3(i,1)=c;
err=abs((b-a)./c);
err2(i,1)=err;
i=i+1;
end
i=i-1;
disp( [va1(1:i,1) va2(1:i,1) va3(1:i,1) err2(1:i,1)]);
end
fprintf('root is :%f\n',c);
2
Computational Laboratory Exercises Prepared By Belaineh Esk. Nov 28 2022
Outputs
enter lower interval -2
enter upper interval -1.5
x1 x0 x2 error
-2.000000000000000 -1.500000000000000 -1.750000000000000 0.142857142857143
-1.750000000000000 -1.500000000000000 -1.625000000000000 0.076923076923077
-1.750000000000000 -1.625000000000000 -1.687500000000000 0.037037037037037
-1.750000000000000 -1.687500000000000 -1.718750000000000 0.018181818181818
-1.750000000000000 -1.718750000000000 -1.734375000000000 0.009009009009009
-1.734375000000000 -1.718750000000000 -1.726562500000000 0.004524886877828
-1.734375000000000 -1.726562500000000 -1.730468750000000 0.002257336343115
-1.734375000000000 -1.730468750000000 -1.732421875000000 0.001127395715896
-1.732421875000000 -1.730468750000000 -1.731445312500000 0.000564015792442
-1.732421875000000 -1.731445312500000 -1.731933593750000 0.000281928390189
-1.732421875000000 -1.731933593750000 -1.732177734375000 0.000140944326991
-1.732177734375000 -1.731933593750000 -1.732055664062500 0.000070477130171
-1.732055664062500 -1.731933593750000 -1.731994628906250 0.000035239806886
-1.732055664062500 -1.731994628906250 -1.732025146484375 0.000017619592987
-1.732055664062500 -1.732025146484375 -1.732040405273438 0.000008809718882
-1.732055664062500 -1.732040405273438 -1.732048034667969 0.000004404840038
-1.732055664062500 -1.732048034667969 -1.732051849365234 0.000002202415168
-1.732051849365234 -1.732048034667969 -1.732049942016602 0.000001101208797
-1.732051849365234 -1.732049942016602 -1.732050895690918 0.000000550604095
-1.732050895690918 -1.732049942016602 -1.732050418853760 0.000000275302123
-1.732050895690918 -1.732050418853760 -1.732050657272339 0.000000137651043
-1.732050895690918 -1.732050657272339 -1.732050776481628 0.000000068825517
-1.732050895690918 -1.732050776481628 -1.732050836086273 0.000000034412757
-1.732050836086273 -1.732050776481628 -1.732050806283951 0.000000017206379
-1.732050836086273 -1.732050806283951 -1.732050821185112 0.000000008603189
-1.732050821185112 -1.732050806283951 -1.732050813734531 0.000000004301595
-1.732050813734531 -1.732050806283951 -1.732050810009241 0.000000002150797
-1.732050810009241 -1.732050806283951 -1.732050808146596 0.000000001075399
-1.732050808146596 -1.732050806283951 -1.732050807215273 0.000000000537699
-1.732050808146596 -1.732050807215273 -1.732050807680935 0.000000000268850
-1.732050807680935 -1.732050807215273 -1.732050807448104 0.000000000134425
-1.732050807680935 -1.732050807448104 -1.732050807564519 0.000000000067212
root is :-1.732051
3
Computational Laboratory Exercises Prepared By Belaineh Esk. Nov 28 2022
Exercises
1 find the other roots in the remaining intervals
2. Function definition instead of inline function to solve the above problem
3. Find the roots of a function f(x) = sin (�) + �−� − � = 0 using bisection method
Experiment 2
False Position Method(Bracketing method)
Objective :solve roots of non linear equations in method of False_Position using matlab code
Algorithm
1. input a,b errth,f(x)
2. check if f(a)*f(b)<0 else go to step7
3. c=(a*f(b)-b*f(a))/(f(b)-f(a))
4. if(f(a)*f(c)>0 a=c else b=c;
5. err=|(b-a)/c|;
6. if err<errth go to step 7 else go to step 3
7. end
Example find the root of �(�) = �3 + �2 − 3� − 3 = 0 in False position method with error tolerance
of less than 0.00000000001 in the interval (0,2);
clc;
clear all;
close all;
a=0;
b=2;
i=1;
err=100;
f=inline('x.^3+x.^2-3.*x-3','x');
aa=zeros(100,1); cc=zeros(100,1);
bb=zeros(100,1); er=zeros(100,1);
if f(a).*f(b)>0
disp("no root in the interval");
else
while(err>0.00000000000001)
c=(a.*f(b)-b.*f(a))./(f(b)-f(a));
aa(i)=a;
bb(i)=b;
cc(i)=c;
if f(a).*f(c)>0 a=c;
else b=c;
end
err=abs(a-b);
er(i)=abs(err);
i=i+1;
end end
i=i-1;
disp(' a b c err');
disp( [aa(1:i,1) bb(1:i,1) cc(1:i,1) er(1:i,1)])
fprintf('root = %f\n',c);
4
Computational Laboratory Exercises Prepared By Belaineh Esk. Nov 28 2022
Output
a b c err
0 2.000000000000000 1.000000000000000 1.000000000000000
1.000000000000000 2.000000000000000 1.571428571428571 0.428571428571429
1.571428571428571 2.000000000000000 1.705410821643287 0.294589178356713
1.705410821643287 2.000000000000000 1.727882728491074 0.272117271508926
1.727882728491074 2.000000000000000 1.731404865845108 0.268595134154892
1.731404865845108 2.000000000000000 1.731950852749072 0.268049147250928
1.731950852749072 2.000000000000000 1.732035343851165 0.267964656148835
1.732035343851165 2.000000000000000 1.732048415307787 0.267951584692213
1.732048415307787 2.000000000000000 1.732050437484425 0.267949562515575
1.732050437484425 2.000000000000000 1.732050750316604 0.267949249683396
1.732050750316604 2.000000000000000 1.732050798711920 0.267949201288080
1.732050798711920 2.000000000000000 1.732050806198701 0.267949193801299
1.732050806198701 2.000000000000000 1.732050807356910 0.267949192643090
1.732050807356910 2.000000000000000 1.732050807536086 0.267949192463914
1.732050807536086 2.000000000000000 1.732050807563804 0.267949192436196
1.732050807563804 2.000000000000000 1.732050807568092 0.267949192431908
1.732050807568092 2.000000000000000 1.732050807568756 0.267949192431244
1.732050807568756 2.000000000000000 1.732050807568859 0.267949192431141
1.732050807568859 2.000000000000000 1.732050807568874 0.267949192431126
1.732050807568874 2.000000000000000 1.732050807568877 0.267949192431123
1.732050807568877 2.000000000000000 1.732050807568877 0.000000000000000
root = 1.732051
Exercises find solutions of the following problems using FPM
1. sin (�) = cos (�)
2. f(x) = sin (�) + �−� − � = 0
Experiment 3
Open methods of root finding for non linear equations
Objectives: Root finding of non linear functions in Fixed Point Iteration ,Newton Raphson and Secant
methods
5
Computational Laboratory Exercises Prepared By Belaineh Esk. Nov 28 2022
6
Computational Laboratory Exercises Prepared By Belaineh Esk. Nov 28 2022
root = 0.770917
7
Computational Laboratory Exercises Prepared By Belaineh Esk. Nov 28 2022
8
Computational Laboratory Exercises Prepared By Belaineh Esk. Nov 28 2022
Exercises: find roots of a function defined below using FPI,NRM and SECANT METHODS
a. �(�) = ���(�) + ���(�) + �−� + ��(�) − � = 0
b. �(�) = ���(�) + ���(�) + �−� 2 + ��(�� ) − � = 0
(hint plot both functions using matlab program to settle possible intervals and initial point of guess )
9
Computational Lab2 Prepared by Belaineh Esk Dec-19-2022
1− 1 1 𝑥1 5
[ −1 1 𝑥
2 ] [ 2 ] = [7]
2 1 −1 𝑥3 4
Then, EROs are applied to get the augmented matrix into an upper triangular form (which, for a 2 x 2 matrix
means finding an ERO that would change a21 to 0):
Computational Lab2 Prepared by Belaineh Esk Dec-19-2022
Here, the primes indicate that the values may have been changed. Putting this back into the equation form, we
have
Example: find a solution for the linear system equation given below using Gauss-Jordan elimination method in
MATLAB code
𝑥1 − 𝑥2 + 2𝑥3 − 4 = −8
2𝑥1 − 2𝑥2 + 3𝑥3 − 3𝑥4 = −20
x1 + 𝑥2 + 𝑥3 = −2
x1 − 𝑥2 + 4𝑥3 + 3𝑥4 = 4
Computational Lab2 Prepared by Belaineh Esk Dec-19-2022
clear all;
clc;
disp('GAUSS Jordan method');
n = input('DIMENTION OF MATRIX : ');
fprintf('MATRIK A IS %d x %d',n,n);
disp(' ');
for i=1:n
for j=1:n
fprintf('A(%d,%d)', i,j);
A(i,j) = input(' = ');
end
end
fprintf('MATRIK B IS %d x 1\n',n);
for i=1:n
fprintf('B(%d,1)',i);
B(i,1) = input(' = ');
end
fprintf('DETEMINANT OF A = %0.2f\n', det(A));
detA = det(A);
if detA ~= 0
M = [A B] ;
fprintf('PROCESS OF GAUESS ELIMINATION\n');
[n,m]=size(M);
En = M;
disp(M);
end
for i=1:n-1
if M(i,i)==0
a=i;
while M(a,i)==0
a=a+1;
end
T=M(i,:);
M(i,:)=M(a,:);
M(a,:)=T;
disp(['B' num2str(i) '<--> B' num2str(a)])
disp(M)
end
for j=i+1:n
if M(j,i)~=0
disp(['B' num2str(j) ' - (' num2str(M(j,i)) '/' num2str(M(i,i)) ')B' num2str(i)])
M(j,:)=M(j,:)-((M(j,i)./M(i,i))*M(i,:));
disp(M);
end
end
end
if M(n,n)==0&&M(n,n+1)~=0
disp ('SOLUTION DOES NOT EXIST')
else
for i=n:-1:2
for j=i-1:-1:1
disp(['B' num2str(j) ' - (' num2str(M(j,i)) '/' num2str(M(i,i)) ')B' num2str(i)])
M(j,:)=M(j,:)-((M(j,i)./M(i,i))*M(i,:));
disp (M)
end
end
for i=1:n
disp(['(1/' num2str(M(i,i)) ')B' num2str(i)])
M(i,:)=(1/M(i,i))*M(i,:);
disp (M)
end
disp ('FORWARD ELIMINATION MATRICES')
disp (M)
disp (' SOLUTIONS ARE ')
for i=1:n
disp(['x' num2str(i) ' = ' num2str(M(i,n+1))])
Computational Lab2 Prepared by Belaineh Esk Dec-19-2022
end
end
2 -2 3 -3 -20
1 1 1 0 -2
1 -1 4 3 4
Computational Lab2 Prepared by Belaineh Esk Dec-19-2022
B2 - (2/1)B1
1 -1 2 -1 -8
0 0 -1 -1 -4
1 1 1 0 -2
1 -1 4 3 4
B3 - (1/1)B1
1 -1 2 -1 -8
0 0 -1 -1 -4
0 2 -1 1 6
1 -1 4 3 4
B4 - (1/1)B1
1 -1 2 -1 -8
0 0 -1 -1 -4
0 2 -1 1 6
0 0 2 4 12
B2<--> B3
1 -1 2 -1 -8
0 2 -1 1 6
0 0 -1 -1 -4
0 0 2 4 12
B4 - (2/-1)B3
1 -1 2 -1 -8
0 2 -1 1 6
0 0 -1 -1 -4
0 0 0 2 4
Computational Lab2 Prepared by Belaineh Esk Dec-19-2022
B3 - (-1/2)B4
1 -1 2 -1 -8
0 2 -1 1 6
0 0 -1 0 -2
0 0 0 2 4
B2 - (1/2)B4
1 -1 2 -1 -8
0 2 -1 0 4
0 0 -1 0 -2
0 0 0 2 4
B1 - (-1/2)B4
1 -1 2 0 -6
0 2 -1 0 4
0 0 -1 0 -2
0 0 0 2 4
B2 - (-1/-1)B3
1 -1 2 0 -6
0 2 0 0 6
0 0 -1 0 -2
0 0 0 2 4
B1 - (2/-1)B3
1 -1 0 0 -10
0 2 0 0 6
0 0 -1 0 -2
0 0 0 2 4
B1 - (-1/2)B2
1 0 0 0 -7
0 2 0 0 6
0 0 -1 0 -2
Computational Lab2 Prepared by Belaineh Esk Dec-19-2022
0 0 0 2 4
(1/1)B1
1 0 0 0 -7
0 2 0 0 6
0 0 -1 0 -2
0 0 0 2 4
(1/2)B2
1 0 0 0 -7
0 1 0 0 3
0 0 -1 0 -2
0 0 0 2 4
(1/-1)B3
1 0 0 0 -7
0 1 0 0 3
0 0 1 0 2
0 0 0 2 4
(1/2)B4
1 0 0 0 -7
0 1 0 0 3
0 0 1 0 2
0 0 0 1 2
𝑥1 − 𝑥2 + 2𝑥3 − 4 = −8
2𝑥1 − 2𝑥2 + 3𝑥3 − 3𝑥4 = −20
𝑥1 + 𝑥2 + 𝑥3 = −2
𝑥1 − 𝑥2 + 4𝑥3 + 3𝑥4 = 4
clc;close all;
A=[1 -1 2 -1;2 -2 3 -3;1 1 1 0 ;1 -1 4 3];
b=[-8 -20 -2 4]';
n=length(b); outputs
%forward elimination
for i=1:n-1 ..........Upper Triangular Matrix....
if A(i,i)==0
a=i; 1 -1 2 -1 -8
while A(a,i)==0
a=a+1;
0 2 -1 1 6
end
T=A(i,:);
0 0 -1 -1 -4
t=b(i);
A(i,:)=A(a,:);
0 0 0 2 4
b(i)=b(a);
b(a)=t;
A(a,:)=T;
end
for j=i+1:n
x=
if A(j,i)~=0
fact=A(j,i)/A(i,i);
-7
A(j,:)=A(j,:)-((A(j,i)./A(i,i))*A(i,:));
b(j)=b(j)-fact*b(i);
3
end
end
2
end
disp('..........Upper Triangular Matrix....'); 2
disp([A b])
Experiment -3 : LU DECOMPOSITION
Exercise
Solve a system of equations using LU decomposition with Crout's method for the problem
below in MATLAB code
clc;
% This script file solves a system of equations by using
% the Crout's LU decomposition method.
a = [9 -4 -2 0; -4 17 -6 -3; -2 -6 14 -6; 0 -3 -6 11];
b = [24; -16; 0; 18];
[L,U]=LUdecompCrout(a)
y=ForwardSub(L,b);
x=BackwardSub(U,y)
function [L,U] = LUdecompCrout(A)
% The function decomposes the matrix A into a lower triangular matrix L
% and an upper triangular matrix U, using Crout's method, such that A= LU.
% Input variables:
% A The matrix of coefficients.
% b Right-hand-side column vector of constants.
% Output variable:
% L Lower triangular matrix.
% U Upper triangular matrix.
[R,C]=size(A);
for i=1:R
L(i,1)=A(i,1);
U(i,i)=1;
end
for j=2:R
U(1,j)=A(1,j)/L(1,1);
end
Computational Lab2 Prepared by Belaineh Esk Dec-19-2022
for i=2:R
for j=2:i
L(i,j)=A(i,j)-L(i,1:j-1)*U(1:j-1,j);
end
for j=i+1:R
U(i,j)=(A(i,j)-L(i,1:i-1)*U(1:i-1,j))/L(i,i);
end
end
end
function y = BackwardSub(a,b)
% The function solves a system of linear equations ax = b
% where a is an upper triangular matrix by using back substitution.
% Input variables:
% a The matrix of coefficients.
% b A column vector of constants.
% Output variable:
% y A column vector with the solution.
n=length(b);
y(n,1)=b(n)/a(n,n);
for i=n-1:-1:1
y(i,1)=(b(i)-a(i,i+1:n)*y(i+1:n,1))./a(i,i);
end
end
function y = ForwardSub(a,b)
% The function solves a system of linear equations ax = b
% where a is lower triangular matrix by using forward substitution.
% Input variables:
% a The matrix of coefficients.
% b A column vector of constants.
% Output variable:
% y A column vector with the solution.
n=length(b);
y(1,1)=b(1)/a(1,1);
for i=2:n
y(i,1)=(b(i)-a(i,1:i-1)*y(1:i-1,1))./a(i,i);
end
end
outputs
L=
9.0000 0 0 0
-4.0000 15.2222 0 0
U=
0 0 1.0000 -0.7049
I=
0 0 0 1.0000
4.0343
1.6545
>>
2.8452
3.6395
Computational Lab2 Prepared by Belaineh Esk Dec-19-2022
Exercise Solve the above problem using a built in function from matlab library
clc;
a = [9 -4 -2 0; -4 17 -6 -3; -2 -6 14 -6; 0 -3 -6 11];
b = [24; -16; 0; 18];
[L U P]=lu(a)
y=inv(L)*P*b;
I=inv(U)*y Output
L=
1.0000 0 0 0
-0.4444 1.0000 0 0
U=
0 0 10.4380 -7.3577
0 0 0 5.2224
P=
1 0 0 0
0 1 0 0
I=
0 0 1 0
4.0343
0 0 0 1
1.6545
2.8452
>>
3.6395
Exercise
clc;
k = 1; x1=0; x2=0; x3=0; x4=0;
disp('OUTPUTS')
disp('k xl x2 x3 x4');
fprintf('%-2.0f %-8.5f %-8.5f %-8.5f %-8.5f\n',k,x1,x2,x3,x4);
for k=2:10
x1=(54.5-(-2*x2+3*x3+2*x4))/9;
x2=(-14-(2*x1-2*x3+3*x4))/8;
x3=(12.5-(-3*x1+2*x2-4*x4))/11;
x4=(-21-(-2*x1+3*x2+2*x3))/10;
fprintf('%-2.0f %-8.5f %-8.5f %-8.5f %-8.5f\n',k,x1,x2,x3,x4);
end
disp('soutions ')
disp([x1 x2 x3 x4]')
Computational Lab2 Prepared by Belaineh Esk Dec-19-2022
OUTPUTS
k xl x2 x3 x4
1 0.00000 0.00000 0.00000 0.00000
2 6.05556 -3.26389 3.38131 -0.58598
3 4.33336 -1.76827 2.42661 -1.18817
4 5.11778 -1.97723 2.45956 -0.97519
5 5.01303 -2.02267 2.51670 -0.99393
6 4.98805 -1.99511 2.49806 -1.00347
7 5.00250 -1.99981 2.49939 -0.99943
8 5.00012 -2.00040 2.50031 -0.99992
9 4.99979 -1.99990 2.49995 -1.00006
10 5.00005 -2.00000 2.49999 -0.99999
soutions
5.0001
-2.0000
2.5000
-1.0000
Solve the above problem using Gauss-Jacobi Methods using MATLAB code
OUTPUTS
k xl x2 x3 x4
soutions
5.0000
-2.0000
2.5000
-1.0000
>>
Computational Lab manual Prepared by Belaineh Esk Dec-24-2022
Experiment : Least Squares Regression and Interpolation
Objective: In this lab, students will get acquainted with least squares linear regression and interpolation
techniques.
Introduction: First, we will come up with a Matlab function to compute slope and intercept of the best fit
straight line along with correlation coefficient and standard error of the estimate. For given inputs vectors x
and y, the output slope and intercept along with the best fit plot are provided.
For a given set of n data points (xi, yi), the overall error calculated
The function E has a minimum at the values of a 1 and a0 where the partial derivatives of E with respect
to each variable is equal to zero. Taking the partial derivatives and setting them equal to zero gives:
Computational Lab manual Prepared by Belaineh Esk Dec-24-2022
Experiment -1 curve fitting
Exercise Write Matlab Program that best fit Linear regression line for the following data
x 10 20 30 40 50 60 70 80
y 25 70 380 550 610 1220 830 1450
Exercise Implement the above problems using built in MATLAB function polyfit,
clc;
close all;
x=[10 20 30 40 50 50 70 80];
outputs
y=[25 70 380 550 610 1220 830 1450]; a =19.0580 -191.9122
a=polyfit(x,y,1)
Exercise
Experiment -2 Interpolation
✓ Lagrange Interpolating Polynomials
✓ Newton's Interpolating Polynomials
✓ PIECEWISE (SPLINE) INTERPOLATION
Lagrange’s Method
Computational Lab manual Prepared by Belaineh Esk Dec-24-2022
Computational Lab manual Prepared by Belaineh Esk Dec-24-2022
Exercise The set of the following five data points is given:
Develop a MATLAB program that finds the interpolation of y at x=3 in Lagrange interpolation polynomial
Write the polynomial in Lagrange form that passes through the points; then use it to calculate the
interpolated value of y at x = 5.4 .
Computational Lab manual Prepared by Belaineh Esk Dec-24-2022
Newton's Interpolating Polynomials
Computational Lab manual Prepared by Belaineh Esk Dec-24-2022
Exercise find the interpolation of the newton divided difference at x=0.5 for the given data points
{(−2, −6), (−1, 0), (1, 0), (2, 6), (4, 60)}
clc;
x1 = [-2 -1 1 2 4];
y1 = [-6 0 0 6 60];
x0=0.5;
interpolated=Newtint(x1,y1,x0)
function yint = Newtint(x,y,xx)
% Newtint: Newton interpolating polynomial
% yint = Newtint(x,y,xx): Uses an (n - 1)-order Newton
% interpolating polynomial based on n data points (x, y)
% to determine a value of the dependent variable (yint)
% at a given value of the independent variable, xx.
% input:
% x = independent variable
% y = dependent variable
% xx = value of independent variable at which
% interpolation is calculated
% output:
% yint = interpolated value of dependent variable
% compute the finite divided differences in the form of a
% difference table
n = length(x);
if length(y)~=n, error('x and y must be same length'); end
b = zeros(n,n);
% assign dependent variables to the first column of b.
b(:,1) = y(:); % the (:) ensures that y is a column vector.
for j = 2:n
for i = 1:n-j+1
b(i,j) = (b(i+1,j-1)-b(i,j-1))/(x(i+j-1)-x(i));
end
end
disp('Newton divided difference table');
disp(b);
disp('Newton Coefficents are');
disp(b(1,:));
outputs
Newton divided difference table
-6 6 -2 1 0
0 0 2 1 0
0 6 7 0 0
6 27 0 0 0
60 0 0 0 0
-6 6 -2 1 0
m=
0 1 0 -1 0
interpolated =
-0.3750
>>
Exercise use polyfit function to check the results you obtained in the above problem
{(−2, −6), (−1, 0), (1, 0), (2, 6), (4, 60)}
clc;
x1 = [-2 -1 1 2 4];
y1 = [-6 0 0 6 60];
a=polyfit(x1,y1,4)
xx=(-2:0.02:4);
yy=polyval(a,xx);
plot(xx,yy,'b-',x1,y1,'*')
grid
Outputs
a=
-0.0000 1.0000 0.0000 -1.0000 -0.0000
Computational Lab manual Prepared by Belaineh Esk Dec-24-2022
Exercise the measure of voltage drop V across a resistor for a number of different values of current i. The
results are
Estimate the voltage drop for i = 1.15 using newton polynomial interpolation
For n data points (i = 1,2,...,n), there are n - 1 intervals. Each interval i has its own spline function, si(x).
For linear splines, each function is merely the straight line connecting the two points at each end of the
interval, which is formulated as
Computational Lab manual Prepared by Belaineh Esk Dec-24-2022
Table Lookup
. First, it would search the independent variable vector to find the interval containing the unknown. Then it
would perform the linear interpolation using one of the techniques described
For ordered data, there are two simple ways to find the interval
➢ sequential search this method involves comparing the desired value with each element of the vector in
sequence until the interval is located
➢ binary search. For situations for which there are lots of data, the sequential sort is inefficient because
it must search through all the preceding points to find values The approach is akin to the bisection
method for root location. Just as in bisection, the index at the midpoint iM is computed as the average
of the first or “lower” index iL = 1 and the last or “upper” index iU = n. The unknown xx is then
compared with the value of x at the midpoint x(iM) to assess whether it is in the lower half of the array
or in the upper half. Depending on where it lies, either the lower or upper index is redefined as being
the middle index. The process is repeated until the difference between the upper and the lower index is
less than or equal to zero. At this point, the lower index lies at the lower bound of the interval
containing xx, the loop terminates, and the linear interpolation is performed
Computational Lab manual Prepared by Belaineh Esk Dec-24-2022
Exercise use both sequential and binary search methods to find the linear spline interpolation of the following
data at T=350
Outputs
ans =
0.5705
ans =
0.5705
Computational Lab manual Prepared by Belaineh Esk Dec-24-2022
Cubic Splines
Derivation of Cubic Splines
Computational Lab manual Prepared by Belaineh Esk Dec-24-2022
Computational Lab manual Prepared by Belaineh Esk Dec-24-2022
Exercise implement a spline interpolation (natural, clamped ) for the data given below and find
interpolation at x=1.5
x=[0 1 2 3 4 5] y=[3 1 4 1 2 0]
clc
xd=[0 1 2 3 4 5];
yd=[3 1 4 1 2 0];
xx=1.5;
nd=length(xd);
[a,b,c,d]=genspline(xd,yd,nd);
evalspline(a,b,c,d,xx,xd,nd)
plotspline(a,b,c,d,xd,nd)
function[a,b,c,d]=genspline(x,y,n)
for i=1:n-1
h(i)=x(i+1)-x(i);
end
A(1,1)=1;
A(n,n)=1;
f=zeros(n,1);
for i=2:n-1
A(i,i)=2*(h(i)-h(i-1));
f(i)=6.*(y(i+1)-y(i))./h(i)-(y(i)-y(i-1))./h(i-1);
end
for i=2:n-2
A(i,i+1)=h(i+1);
end
for i=3:n-1
A(i,i-1)=h(i);
end
s=A\f;
for i=1:n-1
a(i)=(s(i+1)-s(i))/(6*h(i));
b(i)=s(i)/2;
c(i)=(y(i+1)-y(i))/h(i)-(2*h(i)*s(i)+h(i)*s(i+1))/6;
d(i)=y(i);
end
[a' b' c' d']
end
function yy=evalspline(a,b,c,d,xx,x,n)
i=1;
while(xx>x(i+1)&i<n-1)
i=i+1;
end
yy=a(i).*(xx-x(i)).^3+b(i).*(xx-x(i)).^2+c(i).*(xx-x(i))+d(i);
end
function plotspline(a,b,c,d,x,n)
dx=(x(n)-x(1))/100;
for j=1:101
xx(j)=x(1)+dx*(j-1);
yy(j)=evalspline(a,b,c,d,xx(j),x,n);
end
xd=[0 1 2 3 4 5];
yd=[3 1 4 1 2 0];
plot(xx,yy,'-',xd,yd,'*')
end
Computational Lab manual Prepared by Belaineh Esk Dec-24-2022
Outputs coefficients
ans =
ans =
1.7500
>>
n=5;
x=[0 1 2 3 4 5];
a=[3 1 4 1 2 0];
% for i = 0:n
% fprintf('Enter x(%d) and f(x(%d)) on separate lines: \n', i, i);
% x(i+1) = input(' ');
% a(i+1) = input(' ');
% end
m = n - 1;
h = zeros(1,m+1);
for i = 0:m
h(i+1) = x(i+2) - x(i+1);
end
Computational Lab manual Prepared by Belaineh Esk Dec-24-2022
xa = zeros(1,n+1);
xa(1) = 3.0 * (a(2) - a(1)) / h(1) - 3.0 * fp0;
xa(n+1) = 3.0 * fpn - 3.0 * (a(n+1) - a(n)) / h(n);
for i = 1:m
xa(i+1) = 3.0*(a(i+2)*h(i)-a(i+1)*(x(i+2)-x(i))+a(i)*h(i+1))/(h(i+1)*h(i));
end
xl = zeros(1,n+1);
xu = zeros(1,n+1);
xz = zeros(1,n+1);
xl(1) = 2.0 * h(1);
xu(1) = 0.5;
xz(1) = xa(1) / xl(1);
for i = 1:m
xl(i+1) = 2.0 * (x(i+2) - x(i)) - h(i) * xu(i);
xu(i+1) = h(i+1) / xl(i+1);
xz(i+1) = (xa(i+1) - h(i) * xz(i)) / xl(i+1);
end
for i = 1:n
j = n - i;
c(j+1) = xz(j+1) - xu(j+1) * c(j+2);
b(j+1) = (a(j+2)-a(j+1))/h(j+1)-h(j+1)*(c(j+2)+2.0*c(j+1))/3.0;
d(j+1) = (c(j+2) - c(j+1)) / (3.0 * h(j+1));
end
for i = 0:n
fprintf(' %5.4f', x(i+1));
end
fprintf('\n\nThe coefficients of the spline on the subintervals are: \n');
fprintf(' a(i) b(i) c(i) d(i)\n');
for i = 0:m
fprintf('%11.8f %11.8f %11.8f %11.8f\n',a(i+1),b(i+1),c(i+1),d(i+1));
end
Computational Lab manual Prepared by Belaineh Esk Dec-24-2022
>>
Exercise :Analyse the out come for different methods of interpolation for the above problems
(nearest, linear,pchip)
Computational Lab4 Prepared By Belaineh Esk Jan -10-2023
Exercise 1: find the derivative of 𝑓(𝑥) at x=1 with h=0.1 using Richardson Extrapolation Method
𝒇(𝒙) = 𝒙𝟐 𝐜𝐨𝐬 (𝒙)
format long
clc;
h=0.1;
f=inline('x*x*cos(x)','x');
p=input('enter extrapolation stage');
D=zeros(p,1);
M=zeros(p,p);
x=1;
for k=1:p
D(k,1)=(f(x+h)-f(x-h))/(2*h);
h=h/2;
end
A=D;
for k=1:p
for m=k+1:p
M(m,k)=((4.^(k))*D(m,1)-D(m-1,1))/(4^k-1);
end
D(:,1)=M(:,k);
end
D=[A M];
D(:,1:p)
outputs
enter extrapolation stage 5
ans =
0.226736163128552 0 0 0 0
0.236030920594742 0.239129173083472 0 0 0
>>
Exercise 2: find the integral of the following function, 𝑓(𝑥), within its integration limits
Using composite trapezoidal rule ,divided in to 8 subintervals Output
2.5 −𝑥 2
𝐼 = ∫1 (𝑒 + ln (𝑥 2 ))𝑑𝑥 smm =
1.719116634925203
Exercise 3: find the integral of the following function, 𝑓(𝑥), within its integration limits
Using composite Simpson’s 1/3 rule for h=0.25
2
𝐼 = ∫0 𝑥 2 ln (𝑥 2 + 1) 𝑑𝑥
clc;
clear all;
close all;
f=inline('(x^2)*log((x^2+1))','x');
x0=0;
xn=2;
h=0.25;
n=(xn-x0)/h;
sm1=0;
for k=1:n/2
xi=x0+(2*k-1)*h;
sm1=sm1+f(xi);
end
sm1=4*sm1;
sm2=0;
for k=1:(n-2)/2 Output
x=x0+2*k*h;
sm2=sm2+f(x);
smTot =
end
sm2=2*sm2;
3.109337126508865
smTot=(f(x0)+f(xn)+sm2+sm1)*h/3
>>
Exercise 4: find the integral of the following function, 𝑓(𝑥), within its integration limits
Using composite Simpson’s 3/8 rule for 12 subintervals
2.5 2
𝐼 = ∫1 (𝑒 −𝑥 + ln (𝑥 2 ))𝑑𝑥 Outputs
smm =
for k=1:n-1
clc; xk=x0+k.*h; 1.720479875852111
clear all; if(mod(k,3)==0)
close all; sm1=sm1+f(xk);
x0=1; else
xn=2.5; sm2=sm2+f(xk);
n=12; end
h=(xn-x0)/n; end
f=inline(' exp((-x.^2))+log(x.^2)','x'); smm=(f(x0)+f(xn)+2*sm1+3*sm2)*3*h/8;
sm1=0; smm
sm2=0;
Computational Lab4 Prepared By Belaineh Esk Jan -10-2023
Exercise 5: find the integral of the following function, 𝑓(𝑥), within its integration limits
output
stages of extrapolation 6
ans =
2.289514908532539 0 0 0 0 0
2.361059693355689 2.384907954963406 0 0 0 0
>>