Program 1
% MATLAB Program for Bisection Method
function y= bisection_method(f,a,b,tol,maxit)
fa=feval(f,a);
fb=feval(f,b);
if fa*fb>0
fprintf('There is no root b/n these numbers\n')
else
fprintf(' i a b x f(x) error\n')
i=0;
while i<=maxit
i=i+1;
x=(a+b)/2;
fx=feval(f,x);
err=abs(b-a)/2;
fprintf('%2.0f %8.4f %8.4f %8.4f %12.6f %12.6f\n',i,a,b,x,fx,err)
if err<tol
fprintf('The required root is x=%5.4f\n',x)
break
end
if(fa*fx<0)
b=x;fb=fx;
else
a=x;fa=fx;
end
if(i>maxit)
fprintf('Iterations are not sufficient.\n')
end
end
end
1
Program 2
%MATLAB Program for false position method
function y = false_positionm( f, a, b, tol, maxi )
x0 = a;
fa = feval(f,a);
fb = feval(f,b);
if fa*fb>0
fprintf('There is no real root b/n %d and %d\n',a,b)
else
fprintf(' i a b x Error \n')
for i = 1 : maxi
x = (a*fb - b*fa)/( fb - fa );
fx = feval(f,x);
err=abs(x-x0);
if nargout == 0
fprintf ('%3d %4.4f %5.4f %0.4f %3.4f', i, a, b, x, err)
fprintf('\n')
end
if err < tol
fprintf(' The real root is : %f\n',x)
if nargout == 1
y = x;
end
return
elseif fa * fx < 0
b = x;
fb = fx;
else
a = x;
fa = fx;
end
x0 = x;
end
fprintf('Maximum number of iterations not sufficient \n')
if nargout== 1
y = x;
end
end
2
Program 3
% MATLAB Program for Newton-Raphson Method
function y= newton_raphsonmethod(f,df,x0,tol,maxit)
fprintf(' i x f(x) error\n')
i=0;
while i<=maxit
i=i+1;
x=x0-(feval(f,x0)/feval(df,x0));
err=abs(x-x0);
fx=feval(f,x);
fprintf('%3.0f %12.5f %12.5f %12.9f\n',i,x,fx,err)
x0=x;
if err<tol
fprintf('The required root is x =%8.4f\n',x)
break
end
end
if i>maxit
fprintf('Iterations are not sufficient.\n')
end
Instruction:- By using the programs 1, 2 and 3 answer the following
questions and identify the answer and error.
1. Find the real root of the equation 𝑥 3 − 2𝑥 2 + 10𝑥 − 20 = 0 between 1 and 2
correct to four decimal places.
2. Find the root of the equation 3𝑥 − 1 + 𝑠𝑖𝑛𝑥 = 0 between 0 and 1 correct to three
decimal places.
3. Find the real root of the equation log 𝑥 − 𝑐𝑜𝑠𝑥 =0 between 1 and 2 correct to five
decimal places.
4. Find the real root of the equation 2𝑥 + 3𝑐𝑜𝑠𝑥 − 𝑒 𝑥 = 0 between 1 and 2 correct to
five decimal places.
5. Find the real root of the equation (𝑥 − 2)2 − 𝑙𝑛𝑥 =0 between 1 and 2 correct to
five decimal places.
3
Program 4
% MATHLAB Program for Gauss Elimination Method
function y=gauss_eliminationmethod(A,b)
fprintf('The augmented matrix is :\n');
agm=[A b]
n=length(A);
for i=1:n-1
for j=i+1:n
m=A(j,i)/A(i,i);
A(j,:)=A(j,:)-m*A(i,:);
b(j)=b(j)-m*b(i);
end
end
fprintf('The row echolen form of the matrix A is:\n');
A=[A]
fprintf('The row echolen form of the agumented matrix agm is:\n');
agm=[A b]
if rank(A)~=rank(agm)
fprintf('The systm has no real root\n')
elseif rank(A)<n
fprintf('The systm has many real roots\n')
else
x(n)=b(n)/A(n,n);
for i=n-1:-1:1
k=b(i);
for j=i+1:n
k=k-A(i,j)*x(j);
x(i)=k/A(i,i);
end
end
fprintf('The solution vector has components:\n');
for i=1:n
fprintf('x(%d) = %d\n',i,x(i));
end
end
4
Program 5
% MATHLAB Program for Cramer's Method
function y=cramer_method(A,b)
n=length(A);
fprintf('The solution vector has components:\n');
for i=1:n
D=A;
D(:,i)=b;
x(i)=det(D)/det(A);
fprintf('x(%d)=%d\n',i,x(i))
end
Instruction:- By using the programs 4 and 5 solve the following system
of linear equations.
𝑥1 − 𝑥2 + 𝑥3 = 1
1. Solve −3𝑥1 + 2𝑥2 − 3𝑥3 = −6
2𝑥1 − 5𝑥2 + 4𝑥3 = 5
𝑥1 + 3𝑥2 + 6𝑥3 = 2
2. Solve 𝑥1 − 4𝑥2 + 2𝑥3 = 7
3𝑥1 − 𝑥2 + 4𝑥3 = 9
5𝑥1 + 𝑥2 + 𝑥3 + 𝑥4 = 4
𝑥1 + 7𝑥2 + 𝑥3 + 𝑥4 = 12
3. Solve
𝑥1 + 𝑥2 + 6𝑥3 + 𝑥4 = −5
𝑥1 + 𝑥2 + 𝑥3 + 𝑥4 = −6
5
Program 6
%MATLAB Program for Jacobi methood.
function y=jocobi_method(A,b,x0,tol,maxit)
n=length(x0);
fprintf(' k');
for i=1:n
fprintf(' x(%d)',i);
end
fprintf(' \n');
for k=1:maxit
maxerr=0;
for i=1:n
s=0;
for j=1:n
if(j~=i)
s=s+(A(i,j)*x0(j));
end
end
x(i)=(b(i)-s)/A(i,i);
err=abs(x0(i)-x(i));
if err>maxerr
maxerr=err;
end
end
fprintf('%3.0f',k);
for i=1:n
x0(i)=x(i);
fprintf(' %12.4f ',x(i));
end
fprintf('\n')
if maxerr<tol
fprintf('The solutions are:\n');
for i=1:n
fprintf(' x(%d) = %0.4f\n',i,x(i));
end
break
end
end
if maxerr>tol
fprintf('The iterations are not sufficient\n')
end
6
Program 7
%MATLAB Program for Gauss_Seidel Method
function y=gauss_seidelmethod(A,b,x0,tol,maxit)
n=length(x0);
fprintf(' k');
for i=1:n
fprintf(' x(%d)',i);
end
fprintf(' \n');
for k=1:maxit
maxerr=0;
for i=1:n
s=0;
for j=1:n
if(j~=i)
s=s+A(i,j)*x0(j);
end
end
x(i)=(b(i)-s)/A(i,i);
err=abs(x0(i)-x(i));
if err>maxerr
maxerr=err;
end
x0(i)=x(i);
end
fprintf('%3.0f',k);
for i=1:n
fprintf(' %12.4f',x(i));
end
fprintf('\n')
if maxerr<tol
fprintf('The solutions are:\n');
for i=1:n
fprintf(' x(%d) = %0.4f\n',i,x(i));
end
break
end
end
if maxerr>tol
fprintf('The iterations are not sufficient\n')
end
7
Instruction:- Solve the following system of linear equations by using
the programs 6 and 7.
4𝑥1 + 𝑥2 − 𝑥3 = 5
1. Solve −𝑥1 + 3𝑥2 + 𝑥3 = −4 correct to three decimal places.
2𝑥1 + 2𝑥2 + 5𝑥3 = 1
4𝑥1 + 𝑥2 − 𝑥3 + 𝑥4 = −2
𝑥1 + 4𝑥2 − 𝑥3 − 𝑥4 = −1
2. Solve correct to three decimal places.
−𝑥1 − 𝑥2 + 5𝑥3 + 𝑥4 = 0
𝑥1 − 𝑥2 + 𝑥3 + 4𝑥4 = 1
4𝑥1 − 𝑥2 − 𝑥4 = 0
−𝑥2 − 𝑥3 + 4𝑥4 = −2
3. Solve correct to three decimal places.
−𝑥1 − 𝑥2 + 4𝑥3 = 6
−𝑥1 + 4𝑥2 − 𝑥3 − 𝑥4 = 5
4𝑥1 + 𝑥2 + 𝑥3 = 6
2𝑥1 + 𝑥2 + 5𝑥3 = 7
4. Solve correct to three decimal places.
−𝑥1 − 𝑥2 + 𝑥3 + 4𝑥4 = 9
−𝑥1 − 4𝑥2 + 𝑥3 + 𝑥4 = 5
8
Program 8
%MATLAB Program for Newton Forward Interpolation Formula
function z=NFIF(X,Y,x)
n=length(X);
h=X(2)-X(1);
u=(x-X(1))/h;
for i=1:n
A(1,i)=Y(i);
end
for j=2:n
for i=1:n+1-j
A(j,i)=A(j-1,i+1)-A(j-1,i);
end
end
fprintf('The forward difference table is:\n')
fprintf(' X Y');
for i=1:n-1
fprintf(' FD(%d)',i);
end
fprintf(' \n');
for i=1:n
fprintf('%3.1f',X(i));
for j=1:n+1-i
fprintf(' %6.2f',A(j,i));
end
fprintf('\n')
end
p=1;
y=Y(1)*p;
for k=1:n-1
p=p*(u-(k-1))/k;
y=y+p*A(k+1,1);
end
fprintf('The required approximation is y=%5.2f\n',y)
end
9
Program 9
%MATLAB Program for Newton Backward Interpolation Formula
function z=NBIF(X,Y,x)
n=length(X);
h=X(2)-X(1);
u=(x-X(n))/h;
for i=1:n
A(1,i)=Y(i);
end
for j=2:n
for i=j:n
A(j,i)=A(j-1,i)-A(j-1,i-1);
end
end
fprintf('The backward difference table is:\n')
fprintf(' X Y');
for i=1:n-1
fprintf(' BD(%d)',i);
end
fprintf(' \n');
for i=1:n
fprintf('%3.1f',X(i));
for j=1:i
fprintf(' %6.3f',A(j,i));
end
fprintf('\n')
end
p=1;
y=Y(n)*p;
for k=1:n-1
p=p*(u+(k-1))/k;
y=y+p*A(k+1,n);
end
fprintf('The required approximation is y=%5.3f\n',y)
end
10
Program 10
%MATLAB Program for Newton divided difference Method
function z=NDDIF(X,Y,x)
n=length(X);
for i=1:n
DD(1,i)=Y(i);
end
for j=2:n
for i=1:n+1-j
DD(j,i)=(DD(j-1,i+1)-DD(j-1,i))/(X(i+j-1)-X(i));
end
end
fprintf('The divided difference table is:\n')
fprintf(' X Y');
for i=1:n-1
fprintf(' DD(%d)',i);
end
fprintf(' \n');
for i=1:n
fprintf('%2.0f',X(i));
for j=1:n+1-i
fprintf(' %5.2f',DD(j,i));
end
fprintf('\n')
end
p=1;
y=Y(1)*p;
for k=1:n-1
p=p*(x-X(k));
y=y+p*DD(k+1,1);
end
fprintf('The required approximation is y=%5.2f\n',y)
end
11
Program 11
%MATLAB Program for Lagrange Method
function y=lagrange(X,Y,x)
n=length(X);
y=0;
for i=1:n
p=1;
for j=1:n
if j~=i
p=p*(x-X(j))/(X(i)-X(j));
end
end
y=y+p*Y(i);
end
Instruction I:- Approximate using the programs 8, 9, 10 and 11.
1. Given the data
x 4 6 8 10
y 1 3 8 16
Find approximate value of y at x=9.
2. Given the data
x 0.1 0.2 0.3 0.4 0.5
y 1.4 1.56 1.76 2 2.28
Interpolate at x=0.25 and x=0.35.
3. Given the data
x 10 15 20 25 30 35
y 35.3 32.4 292 26.1 23.2 20.5
Find f(12).
Instruction II:- Approximate using the programs 10 and 11.
4. Given the data
X 0 2 3 4 7 8
y 4 26 58 112 466 668
Find f(1), f(5) and f(9).
5. Given the data
X 1 2 3 4 7
y 2 4 8 16 128
Find f(5) .
12
Program 12
%MATLAB Program For Numerical Integration
function I=Integration(ft,a,b,n)
h=(b-a)/n;
fprintf('The function value is :\n')
fprintf(' i x f(x)\n')
for i=1:n+1
x(i)=a+(i-1)*h;
y(i)=feval(ft,x(i));
fprintf('%2.0f %2.1f %5.4f\n',i,x(i),y(i))
end
if mod(n,6)==0
I1=h*(sum(y)-0.5*(y(1)+y(n+1)));
I2=(h/3)*(y(1)+4*sum(y(2:2:n))+2*sum(y(3:2:n))+y(n+1));
I3=(3*h/8)*(y(1)+3*sum(y(2:n))-sum(y(4:3:n))+y(n+1));
fprintf('The numerical integration by Trapezoidal rule is :\n')
fprintf(' I=%5.5f\n',I1)
fprintf('The numerical integration by Simpsos one thrid rule is :\n')
fprintf(' I=%5.5f\n',I2)
fprintf('The numerical integration by Simpson three eigth rule is :\n')
fprintf(' I=%5.5f\n',I3)
elseif mod(n,2)==0
I1=h*(sum(y)-0.5*(y(1)+y(n+1)));
I2=(h/3)*(y(1)+4*sum(y(2:2:n))+2*sum(y(3:2:n))+y(n+1));
fprintf('The numerical integration by Trapezoidal rule is :\n')
fprintf(' I=%5.5f\n',I1)
fprintf('The numerical integration by Simpsos one thrid rule is :\n')
fprintf(' I=%5.5f\n',I2)
fprintf('Integration by Simpsons three eigth rule does not work.\n')
elseif mod(n,3)==0
I1=h*(sum(y)-0.5*(y(1)+y(n+1)));
I3=(3*h/8)*(y(1)+3*sum(y(2:n))-sum(y(4:3:n))+y(n+1));
fprintf('The numerical integration by Trapezoidal rule is :\n')
fprintf(' I=%5.5f\n',I1)
fprintf('The numerical integration by Simpson three eigth rule is :\n')
fprintf(' I=%5.5f\n',I3)
fprintf('Integration by Simpsons one third rule does not work.\n')
13
else
I1=h*(sum(y)-0.5*(y(1)+y(n+1)));
fprintf('The numerical integration by Trapezoidal rule is :\n')
fprintf(' I=%5.5f\n',I1)
fprintf('Integration by Simpsons one third and three eigth rules do not
work.\n')
end
Instruction III:- Solve the following system of linear equations by using
the program 12.
14