0% found this document useful (0 votes)
37 views12 pages

421MA2176 Lab 5 and 6

The document contains the solutions to 6 assignment questions regarding numerical integration techniques like Trapezoidal rule, Simpson's rule, and Gauss Legendre formula. For each technique, the value of pi is calculated numerically for different intervals and the error is computed. It is observed that the error decreases with increasing intervals. Gauss Seidel method for solving systems of linear equations converges faster than Gauss Jacobi method. Gauss Legendre formula using 3 points is more accurate than the 2 point formula.

Uploaded by

Dipankar Das
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)
37 views12 pages

421MA2176 Lab 5 and 6

The document contains the solutions to 6 assignment questions regarding numerical integration techniques like Trapezoidal rule, Simpson's rule, and Gauss Legendre formula. For each technique, the value of pi is calculated numerically for different intervals and the error is computed. It is observed that the error decreases with increasing intervals. Gauss Seidel method for solving systems of linear equations converges faster than Gauss Jacobi method. Gauss Legendre formula using 3 points is more accurate than the 2 point formula.

Uploaded by

Dipankar Das
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/ 12

ASSINGMENT – 5 AND 6

NILOY KUMAR DUTTA 421MA2176

Q(1).
%Gauss Jacobbi method for solving system of linear equations %
A=[4 1 -1 1;1 4 -1 -1;-1 -1 5 1;1 -1 1 3];
B=[-2;-1;0;1];
P=input('Enter the initial roots = ');
n=50;
e=0.001;
N=length(B);
X=zeros(N,1);
for j=1:n
for i=1:N
X(i)=(B(i)/A(i,i))-(A(i,[1:i-1,i+1:N])*P([1:i-1,i+1:N]))/A(i,i);
end
if abs(X-P)<e
break
end
P=X;
end
fprintf('Iteration no = %d\n',j);
fprintf('The solution of the system of equation is =\n ');
disp(X);

Output-

Enter the initial roots = [0;0;0;0]

Iteration no = 12

The solution of the system of equation is =


-0.7521

0.0403

-0.2803

0.6901

%Gauss Jacobbi method for solving system of linear equations %


A=[4 1 -1 1;1 4 -1 -1;-1 -1 5 1;1 -1 1 3];
B=[-2;-1;0;1];
P=input('Enter the initial roots = ');
n=input('Enter the number of iterations = ');
N=length(B);
X=zeros(N,1);
for j=1:n
for i=1:N
X(i)=(B(i)/A(i,i))-(A(i,[1:i-1,i+1:N])*P([1:i-1,i+1:N]))/A(i,i);
end
k=abs(X-P);
P=X;
end
fprintf('The solution of the system of equation is =\n ');
disp(X);
fprintf('The error is =\n');
disp(k);

Output-

Enter the initial roots = [0;0;0;0]

Enter the number of iterations = 10

The solution of the system of equation is =

-0.7497

0.0389

-0.2794

0.6871

The error is =

0.0019

0.0027

0.0021

0.0019

Enter the initial roots = [0;0;0;0]

Enter the number of iterations = 20

The solution of the system of equation is =

-0.7534

0.0411

-0.2808

0.6918

The error is =

1.0e-04 *
0.1443

0.1333

0.0971

0.1684

Table-

After 10 Iterations After 20 Iterations

0.0019 0.00001443

Error 0.0027 0.00001333

0.0021 0.00000971

0.0019 0.00001684

Observation-

If we increase the no of iterations then we get more accurate solution of a system of equation

By using Gauss Jacobi method.

Q(2).
%Gauss Seidel method for solving system of linear equations %
A=[4 1 -1 1;1 4 -1 -1;-1 -1 5 1;1 -1 1 3];
B=[-2;-1;0;1];
P=input('Enter the initial roots = ');
n=50;
e=0.001;
N=length(B);
X=zeros(N,1);
Y=zeros(N,1);
for j=1:n
for i=1:N
X(i)=(B(i)/A(i,i))-(A(i,[1:i-1,i+1:N])*P([1:i-1,i+1:N]))/A(i,i);
P(i)=X(i);
end
if abs(Y-X)<e
break
end
Y=X;
end
fprintf('Iteration no = %d\n',j);
fprintf('The solution of the system of equation is =\n ');
disp(X);

Output-

Enter the initial roots = [0;0;0;0]

Iteration no = 8

The solution of the system of equation is =

-0.7532

0.0410

-0.2807

0.6916

%Gauss Seidel method for solving system of linear equations %


A=[4 1 -1 1;1 4 -1 -1;-1 -1 5 1;1 -1 1 3];
B=[-2;-1;0;1];
P=input('Enter the initial roots = ');
n=input('Enter the number of iterations = ');
N=length(B);
X=zeros(N,1);
Y=zeros(N,1);
for j=1:n
for i=1:N
X(i)=(B(i)/A(i,i))-(A(i,[1:i-1,i+1:N])*P([1:i-1,i+1:N]))/A(i,i);
P(i)=X(i);
end
k=abs(Y-X);
Y=X;
end
fprintf('The solution of the system of equation is =\n ');
disp(Y);
fprintf('The error is =\n');
disp(k);

Output-

Enter the initial roots = [0;0;0;0]

Enter the number of iterations = 10

The solution of the system of equation is =

-0.7534
0.0411

-0.2808

0.6918

The error is =

1.0e-04 *

0.5689

0.1956

0.2685

0.3444

Enter the initial roots = [0;0;0;0]

Enter the number of iterations = 20

The solution of the system of equation is =

-0.7534

0.0411

-0.2808

0.6918

The error is =

1.0e-08 *

0.1822

0.0626

0.0860

0.1103

Table-

After 10th Iterations After 20th Iterations


0.00005689 0.1822e-08

0.00001956 0.0626e-08

Error 0.00002685 0.0860e-08

0.00003444 0.1103e-08

Observation-

Here error is inversely proportional to the no. of Iterations, Hence if we increase the no of
iterations then we get more accurate solution of a system of equation by using Gauss seidel method.

Comparison of convergence speed-

Gauss Seidel method (took 8 iterations to get accuracy) converges faster than Gauss Jacobi
method(took 12 iterations to get accuracy).

Q(3).

(a) Trapezoidal Rule-

f=@(x)(1/(1+(x.^2)));
a=0;
b=1;
n=input('The number of intervals =');
h=(b-a)/n;
m=0;
for i=1:n-1
m=m+f(a+(i*h));
end
s=(h/2)*(f(a)+f(b)+2*m);
k=4*s;
fprintf('The required value of pi using Trapezoidal rule = %4.8f',k);
e=abs(pi-k);
fprintf('\n The error is = %4.8f',e);

Output-

The number of intervals =6

The required value of pi using Trapezoidal rule = 3.13696307


The error is = 0.00462959gut

The number of intervals =12

The required value of pi using Trapezoidal rule = 3.14043525

The error is = 0.00115741

The number of intervals =24

The required value of pi using Trapezoidal rule = 3.14130330

The error is = 0.00028935

(b) Simpson 1/3 Rule-

f=@(x)(1/(1+(x.^2)));
a=0;
b=1;
n=input('The number of intervals=');
h=(b-a)/n;
m1=0;
m2=0;
for i=1:2:n-1
t=a+(i*h);
m1=m1+f(t);
end
for i=2:2:n-2
t=a+(i*h);
m2=m2+f(t);
end
s=(h/3)*(f(a)+f(b)+(4*m1)+(2*m2));
k=4*s;
fprintf('The required value of pi using Simpson 1/3 rule = %4.8f\n',k);
e=abs(pi-k);
disp('The error is =');
disp(e);

Output-

The number of intervals=6

The required value of pi using Simpson 1/3 rule = 3.14159178

The error is =

8.7265e-07

chick

The number of intervals=12

The required value of pi using Simpson 1/3 rule = 3.14159264


The error is =

1.3284e-08

The number of intervals=24

The required value of pi using Simpson 1/3 rule = 3.14159265

The error is =

2.0765e-10

(c) Simpson 3/8 Rule-

f=@(x)(1/(1+(x.^2)));
a=0;
b=1;
n=input('The number of intervals=');
h=(b-a)/n;
m1=0;
m2=0;
m3=0;
for i=1:3:n-1
t=a+(i*h);
m1=m1+f(t);
end
for i=2:3:n-2
t=a+(i*h);
m2=m2+f(t);
end
for i=3:3:n-3
t=a+(i*h);
m3=m3+f(t);
end
s=3*(h/8)*(f(a)+f(b)+(3*((m1)+(m2)))+(2*(m3)));
k=4*s;
fprintf('The required value of pi using Simpson 3/8 rule = %4.8f\n',k);
e=abs(pi-k);
disp('The error is =');
disp(e);

Output-

The number of intervals=6

The required value of pi using Simpson 3/8 rule = 2.69896050

The error is =

0.4426
The number of intervals=12

The required value of pi using Simpson 3/8 rule = 2.93781901

The error is =

0.2038

The number of intervals=24

The required value of pi using Simpson 3/8 rule = 3.04385510

The error is =

0.0977

Observation-

In the above three rules we see that error is inversely proportional to the no. of intervals.

Thus we can get the value of pi more accurate , if the no. of intervals become more larger.

Q(4).

Simpson 1/3 Rule-


f=@(x)(1/(1+(x.^2)));
a=0;
b=1;
n=2;
k=0;
while (abs(pi-k)>=5*(10.^-6))
h=(b-a)/n;
m1=0;
m2=0;
for i=1:2:n-1
t=a+(i*h);
m1=m1+f(t);
end
for i=2:2:n-2
t=a+(i*h);
m2=m2+f(t);
end
s=(h/3)*(f(a)+f(b)+(4*m1)+(2*m2));
k=4*s;
n=n+2;
end
fprintf('The required value of the integral by Simpson 1/3 rule =
%4.8f\n',s);

Output-
The required value of the integral by Simpson 1/3 rule = 0.78539795

Q(5).

Simpsons 1/3 rule-


f=@(x)((sqrt(1-(x.^2)))-x);
a=0;
b=1/sqrt(2);
n=2;
k=0;
while (abs(pi-k)>=5*(10.^-6))
h=(b-a)/n;
m1=0;
m2=0;
for i=1:2:n-1
t=a+(i*h);
m1=m1+f(t);
end
for i=2:2:n-2
t=a+(i*h);
m2=m2+f(t);
end
s=(h/3)*(f(a)+f(b)+(4*m1)+(2*m2));
k=8*s;
n=n+2;
end
fprintf('The required value of the integral by Simpson 1/3 rule =
%4.8f\n',s);

Output-

The required value of the integral by Simpson 1/3 rule = 0.39269866

Q(6).

(a) For qs no. 3-

% Gauss Legendre formula for neumerical integration for 2 points & 3 points %
f=@(x)(1/(1+(x.^2)));
a=0;
b=1;
F=@(t)f(((b-a)*t+(b+a))/2);
n=input('Enter the number of nodes:');
if n==2
w1=1;w2=1;
x1=1/sqrt(3);x2=-1/sqrt(3);
G2=((b-a)/2)*(w1*F(x1)+w2*F(x2));
k1=abs((pi/4)-G2);
fprintf('The value of the integration using gauss 2 point formula is =
%4.9f\n',G2);
fprintf('The error is = %4.8f',k1);
elseif n==3
w1=5/9;w2=8/9;w3=5/9;
x1=sqrt(3/5);x2=0;x3=-sqrt(3/5);
G3=((b-a)/2)*(w1*F(x1)+w2*F(x2)+w3*F(x3));
k2=abs((pi/4)-G3);
fprintf('The value of the integration using gauss 3 point formula is
:%4.9f\n',G3);
fprintf('The error is = %4.8f',k2);
end

Output-

Enter the number of nodes:2

The value of the integration using gauss 2 point formula is = 0.786885246

The error is = 0.00148708

Enter the number of nodes:3

The value of the integration using gauss 3 point formula is :0.785267035

The error is = 0.00013113

(b) For qs no. 5-

% Gauss Legendre formula for neumerical integration for 2 points & 3 points %
f=@(x)((sqrt(1-(x.^2)))-x);
a=0;
b=1/sqrt(2);
F=@(t)f(((b-a)*t+(b+a))/2);
n=input('Enter the number of nodes:');
if n==2
w1=1;w2=1;
x1=1/sqrt(3);x2=-1/sqrt(3);
G2=((b-a)/2)*(w1*F(x1)+w2*F(x2));
k1=abs((pi/8)-G2);
fprintf('The value of the integration using gauss 2 point formula is
:%4.9f\n',G2);
fprintf('The error is = %4.8f',k1);
elseif n==3
w1=5/9;w2=8/9;w3=5/9;
x1=sqrt(3/5);x2=0;x3=-sqrt(3/5);
G3=((b-a)/2)*(w1*F(x1)+w2*F(x2)+w3*F(x3));
k2=abs((pi/8)-G3);
fprintf('The value of the integration using gauss 3 point formula is
:%4.9f\n',G3);
fprintf('The error is = %4.8f',k2);
end

Output-

Enter the number of nodes:2


The value of the integration using gauss 2 point formula

is :0.393053552The error is = 0.00035447

Enter the number of nodes:3

The value of the integration using gauss 3 point formula

is :0.392714456The error is = 0.00001537

Observation-

The error is less in 3 points Gauss Legendry formula as compared to 2 points


Gauss legendryformula.hence we get more accurate value of integration in 3 points
Gauss legendry formula.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy