0% found this document useful (0 votes)
11 views46 pages

Computational LAB

The document contains a series of computational laboratory exercises focused on root-finding methods in MATLAB, including the Bisection method and the False Position method. It provides detailed algorithms, example code, and outputs for finding roots of specific functions, as well as exercises for further practice. Additionally, it covers open methods like Fixed Point Iteration, Newton-Raphson, and Secant methods for solving nonlinear equations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views46 pages

Computational LAB

The document contains a series of computational laboratory exercises focused on root-finding methods in MATLAB, including the Bisection method and the False Position method. It provides detailed algorithms, example code, and outputs for finding roots of specific functions, as well as exercises for further practice. Additionally, it covers open methods like Fixed Point Iteration, Newton-Raphson, and Secant methods for solving nonlinear equations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Computational Laboratory Exercises Prepared By Belaineh Esk.

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

Fixed point iteration


Algorithm
1. find x=g(x) from f(x) ,errth and initial guess a;
2. x=g(a)
3. err =|x-a|/|x|
4. if(err<threshold) go to step 7
5. else a=x;
6. go to step 2;
7. end
Newton_Raphson
Algorithm
1. Initials x0 ,f(x) and f’(x)
2. x1=x0-f(x0)/f’(x0)
3. err=|x1-x0|/|x1|
4. if err<errth go to step 6
5. else x0=x1 and goto step 2
6. end
Secant Method
Algorithm
1. Initials a,b errth and function f;
2. If|f(a)|<f(b) do swapping else go to step 3
3. c=b-f(b)*(b-a)/(f(b)-f(a))
4. err=|c-b|/|c|
5. a=b;b=c
6. If(err>errth) goto step 3 else goto step 7
7. output c
8. end
Example solve the following problems using Fixed Point iteration ,Newton_Raphson and Secant methods to
find roots the given equations
a. �(�) = �3 + 2� − 2 = 0
b. �(�) = �3 + �2 − 3� − 3 = 0
c. f(x) = sin (�) + �−� − � = 0

6
Computational Laboratory Exercises Prepared By Belaineh Esk. Nov 28 2022

% FIXED POINT ITERATION METHOD for problem(a) xi error


clc 10.000000000000000 9.980392156862745
clear all; 0.019607843137255 0.980199960053212
close all; 0.999807803190467 0.333055712983997
format long; 0.666752090206470 0.151391604407311
a=10; 0.818143694613781 0.068900179109780
f=inline('2./(x.^2+2)','x'); 0.749243515504000 0.031589883791298
err=abs(a-f(a));
0.780833399295298 0.014462019882618
i=1;
val=zeros(100,1);
0.766371379412681 0.006627733383626
err2=zeros(100,1); 0.772999112796306 0.003036176446576
while(err>0.0000000001) 0.769962936349730 0.001391156097255
x=f(a); 0.771354092446985 0.000637362369785
err=abs(x-a); 0.770716730077200 0.000292021506629
val(i)=a; 0.771008751583829 0.000133793537797
err2(i)=err; 0.770874958046032 0.000061299816039
a=x; 0.770936257862071 0.000028085457145
i=i+1; 0.770908172404926 0.000012867809009
end 0.770921040213936 0.000005895591128
i=i-1; 0.770915144622807 0.000002701159761
disp([val(1:i,1) err2(1:i,1)]) 0.770917845782568 0.000001237579511
fprintf(' root = %f\n',x) 0.770916608203057 0.000000567016876
0.770917175219932 0.000000259787854
0.770916915432078 0.000000119025964
0.770917034458042 0.000000054533650
0.770916979924392 0.000000024985465
0.770917004909857 0.000000011447491
0.770916993462366 0.000000005244851
0.770916998707217 0.000000002403012
0.770916996304205 0.000000001100979
0.770916997405183 0.000000000504431
0.770916996900752 0.000000000231113
0.770916997131866 0.000000000105888
0.770916997025977 0.000000000048514

root = 0.770917

7
Computational Laboratory Exercises Prepared By Belaineh Esk. Nov 28 2022

%NEWTON RAPHSON METHOD for problem (b)


clc;
clear all;
close all;
format long;
f=inline('x.^3+x.^2-3.*x-3','x');
df=inline('3.*x.^2+2.*x-3','x');
x0=1;
err=5; xi error
val=zeros(20,1); 1.000000000000000 4.000000000000000
ERR=zeros(20,1);
3.000000000000000 24.000000000000000
i=1;
while(err>0.0000001) 2.200000000000000 5.888000000000002
x1=x0-f(x0)./df(x0); 1.830150753768844 0.989001222876588
err=norm(f(x0)); 1.737795453142821 0.054572577966995
val(i)=x0; 1.732072291544954 0.000203329392602
ERR(i)=err; 1.732050807871055 0.000000002859845
x0=x1;
i=i+1; root = 1.732051
end
i=i-1;
disp([val(1:i,1) ERR(1:i,1)]);
fprintf('root = %f\n',x1)

%SECANT METHOD for problem(c)


clc;
clear all;
f=inline('sin(x)+exp(-x)-x','x');
x1=1;
x2=2;
err=5;
i=1;
X1=zeros(20,1);
X2=zeros(20,1);
ERR=zeros(20,1); xi-1 xi error
if(norm(f(x1))<f(x2)) 1.000000000000000 2.000000000000000 0.052122350654423
t=x1; 2.000000000000000 1.179743489017428 0.012254150810282
x1=x2; 1.179743489017428 1.222179357109213 0.000231158202408
x2=t; 1.222179357109213 1.235222723235471 0.000001005810133
end 1.235222723235471 1.234981232931624 0.000000000082177
while(err>0.0000000001)
X1(i)=x1; roor = 1.234982
X2(i)=x2;
x3=x2-(f(x2).*(x2-x1))./(f(x2)-f(x1));
x1=x2;
x2=x3;
err=norm(f(x3));
ERR(i)=err;
i=i+1;
end
i=i-1;
disp([X1(1:i,1) X2(1:i,1) ERR(1:i,1)])
fprintf('roor = %f\n',x3);

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

Lab Experiment on Solutions of Linear equations


Experiment -1 Direct Methods (cramers rule)
Find the solution of the linear system equation given below using Cramer’s rule
𝑥1 − 𝑥2 + 𝑥3 = 5
−𝑥1 + 𝑥2 + 2𝑥3 = 7
2𝑥1 + 𝑥2 − 𝑥3 = 4
[𝐴][𝑥] = [𝐵]

1− 1 1 𝑥1 5
[ −1 1 𝑥
2 ] [ 2 ] = [7]
2 1 −1 𝑥3 4

% CRAMER'S RULE OF 3 equations and 3unkowns


clc;
%[A]{x}=B
A=[1 -1 1;-1 1 2;2 1 -1]; % A 3x3
B=[5 7 4]';%B 3x1
C=A; outputs
C(:,1)=B;
x1=det(C)/det(A); x1 =3.000000
C(:,1)=A(:,1);
C(:,2)=B; x2 =2.000000
x2=det(C)/det(A);
C(:,2)=A(:,2); x3 =4.000000
C(:,3)=B;
x3=det(C)/det(A);
fprintf('x1 =%f\n',x1);
fprintf('x2 =%f\n',x2);
fprintf('x3 =%f\n',x3);

Experiment-2 Elimination Methods


Objective: In this lab, students will get acquainted with Gauss elimination, Reduced row echelon form, Gauss-
Jordon elimination, and LU decomposition methods
Introduction: Gaussian Elimination: The Gauss Elimination method is a method for solving a matrix equation
Ax=b for x. The process is:
1. Start by augmenting the matrix A with the column vector b.
2. Perform elementary row operations to transform this augmented matrix to upper triangular form.
3. Use back-substitution to solve for the unknowns in x.
For example, for a 2 x 2 system, the first step is to augment the matrix [A b]:

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

So, we now use the last line which says


a’22 x2 = b’2
to solve first for x2 = b’2 / a’22
and then go back to the first line which says
a’11 x1 + a’12 x2 = b’1
to solve for
x1 = (b’1 – a’12 x2) / a’11
Gauss-Jordan Elimination Method(Reduced Echelon Form):
In Gauss-Jordan elimination method, we first transfer the Linear Equation System (LES) to a matrix system.
Then, after applying necessary elementary row operations, we try to achieve a matrix which consists of an Identity
matrix in the first part and the solution column in the second part. Therefore, we may find the solution directly by
this solution column. For example
𝑥1 − 𝑥2 + 𝑥3 = 5
−𝑥1 + 𝑥2 + 2𝑥3 = 7
2𝑥1 + 𝑥2 − 𝑥3 = 4

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

GAUSS Jordan Method


DIMENTION OF MATRIX : 4
MATRIK A IS 4 x 4
A(1,1) = 1
A(1,2) = -1
A(1,3) = 2
A(1,4) = -1
A(2,1) = 2
A(2,2) = -2
A(2,3) = 3
A(2,4) = -3
A(3,1) = 1
A(3,2) = 1
A(3,3) = 1
A(3,4) = 0
A(4,1) = 1
A(4,2) = -1
A(4,3) = 4
A(4,4) = 3
MATRIK B IS 4 x 1
B(1,1) = -8
B(2,1) = -20
B(3,1) = -2
B(4,1) = 4
DETEMINANT OF A = 4.00
PROCESS OF GAUESS ELIMINATION
1 -1 2 -1 -8

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

FORWARD ELIMINATION MATRICES


1 0 0 0 -7
0 1 0 0 3
0 0 1 0 2
0 0 0 1 2
Computational Lab2 Prepared by Belaineh Esk Dec-19-2022
SOLUTIONS ARE
x1 = -7
x2 = 3
x3 = 2
x4 = 2
>>

GAUSS ELIMNATION USING ( Backward Substitution):

𝑥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])

% back substitution phase


for k = n:-1:1
b(k) = (b(k) - A(k,k+1:n)*b(k+1:n))/A(k,k);
end
x=b
Computational Lab2 Prepared by Belaineh Esk Dec-19-2022

Experiment -3 : LU DECOMPOSITION

LU decomposition can be implemented in 3 ways


Computational Lab2 Prepared by Belaineh Esk Dec-19-2022
Computational Lab2 Prepared by Belaineh Esk Dec-19-2022

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

-2.0000 -6.8889 10.4380 0

0 -3.0000 -7.3577 5.2224

U=

1.0000 -0.4444 -0.2222 0

0 1.0000 -0.4526 -0.1971

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

-0.2222 -0.4526 1.0000 0

0 -0.1971 -0.7049 1.0000

U=

9.0000 -4.0000 -2.0000 0

0 15.2222 -6.8889 -3.0000

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

Experiment -4 Iterative Methods (Jacobi, G.seidel )


Computational Lab2 Prepared by Belaineh Esk Dec-19-2022

Jacobi Iterative Method

Gauss-Seidel Iterative Method


Computational Lab2 Prepared by Belaineh Esk Dec-19-2022

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

Exercise >> >>

Solve the above problem using Gauss-Jacobi Methods using MATLAB code

%GAUESS Jacobi METHOD


clc;
k = 1; x11=0; x22=0; x33=0; x44=0;
disp('OUTPUTS')
disp('k xl x2 x3 x4');
fprintf('%-2.0f %-8.5f %-8.5f %-8.5f %-8.5f\n',k,x11,x22,x33,x44);
for k=2:20
x1=(54.5-(-2*x22+3*x33+2*x44))/9;
x2=(-14-(2*x11-2*x33+3*x44))/8;
x3=(12.5-(-3*x11+2*x22-4*x44))/11;
x4=(-21-(-2*x11+3*x22+2*x33))/10;
fprintf('%-2.0f %-8.5f %-8.5f %-8.5f %-8.5f\n',k,x1,x2,x3,x4);
x11=x1;
x22=x2;
x33=x3;
x44=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 -1.75000 1.13636 -2.10000

3 5.75455 -2.19230 2.34242 -0.59116

4 4.91894 -2.38134 2.88942 -0.75989

5 4.73209 -1.97242 2.63454 -0.97969

6 4.95677 -1.90700 2.42931 -1.08876

7 5.06396 -1.97358 2.43902 -1.02241

8 5.03118 -2.02283 2.50449 -0.98294

9 4.98964 -2.01307 2.51886 -0.98781

10 4.98810 -1.99727 2.50398 -1.00192

11 4.99971 -1.99531 2.49556 -1.00400

12 5.00341 -1.99954 2.49761 -1.00058

13 5.00103 -2.00123 2.50064 -0.99898

14 4.99929 -2.00048 2.50088 -0.99955

15 4.99950 -1.99977 2.50006 -1.00017

16 5.00007 -1.99980 2.49976 -1.00018

17 5.00017 -2.00001 2.49992 -1.00000

18 5.00003 -2.00006 2.50005 -0.99995

19 4.99996 -2.00001 2.50004 -0.99999

20 4.99998 -1.99999 2.50000 -1.00001

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

%Linear regression of y=a1x+a0


clc;
close all;
x=[10 20 30 40 50 50 70 80];
y=[25 70 380 550 610 1220 830 1450];
linreg(x,y);
function[a1,a0]=linreg(x,y)
n=length(x);
sx=sum(x);
sy=sum(y);
sxx=sum(x.*x);
sxy=sum(x.*y);
a1=(n.*sxy-sx.*sy)./(n.*sxx-sx.^2);
a0=(sy-a1.*sx)./n;
xx=linspace(min(x),max(x),100);
yy=a1.*xx+a0;
plot(x,y,'ko',xx,yy,'r-')
grid;
syms x;
y=a1.*x+a0
y = (12159*x)/638 - 61220/319
end

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 The following data is given:


Computational Lab manual Prepared by Belaineh Esk Dec-24-2022

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

%Lagrange interpolation polynoial


clc;
x=[1 2 4 5 7];
y=[52 5 -5 -40 10];
x0=3;
LagrangeINT(x,y,x0);
function Yint = LagrangeINT(x,y,Xint)
n = length(x);
for i =1:n
L(i)=1; Output
for j=1:n
Yint =6.0000
if j~=i
L(i)= L(i)*(Xint-x(j))/(x(i)-x(j));
end
end
end
Yint = sum(y.*L)
end

Exercise The following data is given:

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,:));

% use the finite divided differences to interpolate


xt = 1;
yint = b(1,1);
for j = 1:n-1
xt = xt*(xx-x(j));
yint = yint+b(1,j+1)*xt;
end
N=length(x)-1;
a = b(1,:);
m = a(N+1); %Begin
for k = N:-1:1
m = [m a(k)] - [0 m*x(k)]; %n(x)*(x - x(k - 1))+a_k - 1
end
disp('coffients of x in decreasing order of power x')
m
xx = (-2:0.02: 4);
yy = polyval(m,xx);
plot(xx,yy,'b-',x,y,'*')
end
Computational Lab manual Prepared by Belaineh Esk Dec-24-2022

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

Newton Coefficents are

-6 6 -2 1 0

coffients of x in decreasing order of power x

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

Experiment 3 Splines and Piecewise Interpolation


✓ Linear Splines
✓ Quadratic Splines
✓ Cubic Splines
Linear Splines

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

T = [-40 0 20 50 100 150 200 250 300 400 500];


density = [1.52 1.29 1.2 1.09 .946 .935 .746 .675 .616 .525 .457];
clc
T = [-40 0 20 50 100 150 200 250 300 400 500];
density = [1.52 1.29 1.2 1.09 .946 .935 .746 .675 .616 .525 .457];
TableLookBin(T,density,350)% binary search
TableLook(T,density,350)% sequential search
function yi = TableLookBin(x, y, xx)
n = length(x);
if xx < x(1) | xx > x(n)
error('Interpolation outside range')
end
% binary search
iL = 1; iU = n;
while (1)
if iU - iL <= 1, break, end
iM = fix((iL + iU) / 2);
if x(iM) < xx
iL = iM;
else
iU = iM;
end
end
% linear interpolation
yi = y(iL) + (y(iL+1)-y(iL))/(x(iL+1)-x(iL))*(xx - x(iL));
end
function yi = TableLook(x, y, xx)
n = length(x);
if xx < x(1) | xx > x(n)
error('Interpolation outside range')
end
% sequential search
i = 1;
while(1)
if xx <= x(i + 1), break, end
i = i + 1;
end
% linear interpolation
yi = y(i) + (y(i+1)-y(i))/(x(i+1)-x(i))*(xx-x(i));
end

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 =

-1.3333 0 -0.6667 3.0000

4.6667 -4.0000 2.3333 1.0000

-5.5000 10.0000 -7.5000 4.0000

0.3333 -6.5000 7.1667 1.0000

1.8333 -5.5000 1.6667 2.0000

ans =

1.7500

>>

% Clamped cubic spline interpolation


% Find the approximate value of f(1.5) from

% Also f'(0)=0, f'(3)=0


close all;
clc;

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

fprintf('Enter f''(x(0)) and f''(x(n)) on separate lines\n');


fp0 = input(' ');
fpn = input(' ');

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

xl(n+1) = h(n) * (2.0 - xu(n));


xz(n+1) = (xa(n+1) - h(n) * xz(n)) / xl(n+1);
c = zeros(1,n+1);
b = zeros(1,n+1);
d = zeros(1,n+1);
c(n+1) = xz(n+1);

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

fprintf('The numbers x(0), ..., x(n) are:\n');

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

Enter f'(x(0)) and f'(x(n)) on separate lines

The numbers x(0), ..., x(n) are:

0.0000 1.0000 2.0000 3.0000 4.0000 5.0000

The coefficients of the spline on the subintervals are:

a(i) b(i) c(i) d(i)

3.00000000 0.00000000 -6.70334928 4.70334928

1.00000000 0.70334928 7.40669856 -5.11004785

4.00000000 0.18660287 -7.92344498 4.73684211

1.00000000 -1.44976077 6.28708134 -3.83732057

2.00000000 -0.38755981 -5.22488038 3.61244019

>>

Exercise use built in libraries to do the problems given above


xd=[0 1 2 3 4 5];
yd=[3 1 4 1 2 0];
x=0:0.1:5;
yi=interp1(xd,yd,x,'spline');
plot(xd,yd,'*',x,yi,'-');

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

0.238357741479329 0.239133348440858 0.239133626798017 0 0

0.238939642510587 0.239133609521007 0.239133626926350 0.239133626928387 0

0.239085130007943 0.239133625840395 0.239133626928355 0.239133626928386 0.239133626928386

>>
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

clc; for k=1:n-1


clear all; xk=x0+k.*h;
close all; dd(k)= f(xk);
x0=1; % sm=sm+dd(k);
xn=2.5; end
n=8; sm=2*sum(dd);
h=(xn-x0)/n; % sm=2.*sm;
f=inline(' exp((-x.^2))+log(x.^2)','x'); smm=(f(x0)+f(xn)+sm).*h./2;
sm=0; smm
dd=zeros(n-1,1);
Computational Lab4 Prepared By Belaineh Esk Jan -10-2023

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

Using Romberg Method


3 2
𝐼 = ∫2 (𝑒 −𝑥 + sin(𝑥 2 ) + ln (𝑥 2 + 𝑥 + 3))𝑑𝑥
clc;
format long;
f=inline('exp(-x^2)+sin(x^2)+log(x^2+x+3)','x');
a=2;b=3;
h=b-a;
p=input('stages of etrapolation');
R=zeros(p,1);
M=zeros(p,p);
R(1,1)=(f(a)+f(b))*h/2;
for k=2:p
sm=0;
for j=1:2^(k-2)
x=a+(2*j-1)*h/(2^(k-1));
sm=sm+f(x);
end
R(k,1)=R(k-1,1)./2+sm.*h./(2.^(k-1));
end
A=R;
for k=1:p
for m=k+1:p
M(m,k)=(4.^(k).*R(m,1)-R(m-1,1))/(4^(k)-1);
end
R(:,1)=M(:,k);
end
% [A M]
D=[A M];
D(:,1:p)

output
stages of extrapolation 6

ans =

2.289514908532539 0 0 0 0 0

2.361059693355689 2.384907954963406 0 0 0 0

2.417378345156140 2.436151229089624 2.439567447364705 0 0 0

2.429235723515060 2.433188182968033 2.432990646559927 2.432886252896359 0 0

2.432076513466268 2.433023443450004 2.433012460815469 2.433012807073494 2.433013303364385 0

2.432779294016157 2.433013554199454 2.433012894916084 2.433012901806570 2.433012902178072 2.433012901785905

>>

You might also like