421MA2176 Lab 5 and 6
421MA2176 Lab 5 and 6
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-
Iteration no = 12
0.0403
-0.2803
0.6901
Output-
-0.7497
0.0389
-0.2794
0.6871
The error is =
0.0019
0.0027
0.0021
0.0019
-0.7534
0.0411
-0.2808
0.6918
The error is =
1.0e-04 *
0.1443
0.1333
0.0971
0.1684
Table-
0.0019 0.00001443
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
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-
Iteration no = 8
-0.7532
0.0410
-0.2807
0.6916
Output-
-0.7534
0.0411
-0.2808
0.6918
The error is =
1.0e-04 *
0.5689
0.1956
0.2685
0.3444
-0.7534
0.0411
-0.2808
0.6918
The error is =
1.0e-08 *
0.1822
0.0626
0.0860
0.1103
Table-
0.00001956 0.0626e-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.
Gauss Seidel method (took 8 iterations to get accuracy) converges faster than Gauss Jacobi
method(took 12 iterations to get accuracy).
Q(3).
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-
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 error is =
8.7265e-07
chick
1.3284e-08
The error is =
2.0765e-10
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 error is =
0.4426
The number of intervals=12
The error is =
0.2038
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).
Output-
The required value of the integral by Simpson 1/3 rule = 0.78539795
Q(5).
Output-
Q(6).
% 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-
% 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-
Observation-