Experiment-5: AIM:WAP in MATLAB To Find The Root of Following Equations Using Gauss Elimination
Experiment-5: AIM:WAP in MATLAB To Find The Root of Following Equations Using Gauss Elimination
AIM:WAP in MATLAB to find the root of following equations using gauss elimination
method:
10x−7y+3z+5w=6
-6x+8y−1z−4w=5
3x+y+4z+11w=2
5x−9y−2z+4w=7
FLOWCHART:
CODE:
function [] = gausselimination()
clc;
clear all;
close all;
N=input('enter no of equation');
A=input('enter augmented matrix(row wise)');
for j=1:1:N
for i=j+1:1:N
t=A(i,j)/A(j,j);
for k=1:1:N+1
A(i,k)=A(i,k)-t*A(j,k);
end
end
end
disp(A);
for i=N:-1:1
s=0;
for j=i+1:N
s=s+A(i,j)*x(j);
end
x(i)=(A(i,N+1)-s)/A(i,i);
end
disp(x);
end
OUTPUT:
EXPERIMENT- 6
AIM:WAP in MATLAB to find the root of following equations using gauss Jordan method:
10𝑥−7𝑦+3𝑧+5𝑤=6
-6𝑥+8𝑦−1𝑧−4𝑤=5
3𝑥+𝑦+4𝑧+11𝑤=2
5𝑥−9𝑦−2𝑧+4𝑤=7
FLOWCHART:
CODE:
function [] = gaussjordan()
clc;
clear all;
close all;
N=input('enter no of equation');
for j=1:1:N
for i=1:1:N
if(i~=j)
t=A(i,j)/A(j,j);
for k=1:1:N+1
A(i,k)=A(i,k)-t*A(j,k);
end
end
end
end
disp(A);
for i=1:N
x(i)=A(i,N+1)/A(i,i);
end
disp(x);
end
OUTPUT:-
EXPERIMENT- 7
AIM:WAP in matlab to find a inverse of the matrix :
2 2 3
2 1 1
1 3 5
FLOWCHART:
CODE:
function [] = inverse_jordan()
clc;
clear all;
[r,C]=size(A);
if(r==C)
if(det(A)~=0)
N=r;
B=eye(r);
C=[A B];
for j=1:N
for i=1:N
if(i~=j)
t=C(i,j)/C(j,j);
for k=1:2*N
C(i,k)=C(i,k)-t*C(j,k);
end
end
end
end
disp(C);
for i=1:N
for j=N+1:2*N
C(i,j)=C(i,j)/C(i,i);
end
end
D=C(1:N,N+1:2*N);
disp(D);
disp('the inverse of the matrix using in built function');
disp(inv(A));
else
end
else
end
end
OUTPUT:
EXPERIMENT- 8
AIM: WAP in MATLAB to solve system of linear equations using Gauss Siedal method:
20x+y-2Z=17
3x+20y-z=-18
2x-3Y+20z=25
FLOWCHART:
CODE:
function [] = gauss_seidal()
clc;
clear all;
N=input('enter no equations');
aerr=input('allowed error');
x=zeros(1,N);
fprintf('itr no \t');
for i=1:N
fprintf('x[%d]\t',i);
end
for itr=1:maxitr
maxerr=0;
for i=1:N
s=0;
for j=1:N
if(j~=i)
s=s+A(i,j)*x(j);
end
end
t=(A(i,N+1)-s)/A(i,i);
err=abs(t-x(i));
if(err>maxerr)
maxerr=err;
end
x(i)=t;
end
fprintf('\n %d \t',itr);
for i=1:N
fprintf('%f \t',x(i));
end
if(maxerr<aerr)
for i=1:N
fprintf('x[%d]=%f ',i,x(i));
end
return;
end
end
end
OUTPUT:
EXPERIMENT- 9
AIM: WAP to fit a parabola y=𝑎+𝑏𝑥+𝑐𝑥2 using method of least square
FLOWCHART:
CODE:
function [] = fitting()
clear all;
clc;
augm=zeros(3,4);
augm(1,1)=n;
for i=1:n
x=input(' ');
xsq=x(1)*x(1);
augm(1,2)=augm(1,2)+x(1);
augm(1,3)=augm(1,3)+xsq;
augm(1,4)=augm(1,4)+x(2);
augm(2,3)=augm(2,3)+xsq*x(1);
augm(2,4)=augm(2,4)+x(1)*x(2);
augm(3,3)=augm(3,3)+xsq*xsq;
augm(3,4)=augm(3,4)+xsq*x(2);
end
augm(2,1)=augm(1,2);
augm(2,2)=augm(1,3);
augm(3,1)=augm(2,2);
augm(3,2)=augm(2,3);
for j=1:1:3
for i=1:1:3
if(i~=j)
t=augm(i,j)/augm(j,j);
for k=1:1:4
augm(i,k)=augm(i,k)-t*augm(j,k);
end
end
end
end
a=augm(1,4)/augm(1,1);
b=augm(2,4)/augm(2,2);
c=augm(3,4)/augm(3,3);
end
OUTPUT:
EXPERIMENT – 10
AIM:WAP a matlab code to evaluate:
6
∫0 1/(1 + 𝑥 2 ) 𝑑𝑥
FLOWCHART:
1) Trapezoidal rule
CODE:
Function[] = trap()
Clc;
Clear all;
h = (xn – x0)/n;
y = inline(‘1/(1+x2)’);
s = y(x0) + y(xn);
for I = 1:(n-1)
s = s+2*y(x0 + i*h);
end
s = (s*h)/2
end
OUTPUT:
2) SIMPSON’S ONE THIRD RULE:
FLOWCHART:
CODE:
Function[] = simp()
Clear all;
Clc;
h = (xn – x0)/n;
y = inline(‘1/(1+x2)’);
if (rem(n,2) == 0)
for I = 3:2:(n-1)
end;
s =(s*h)/3;
fprintf(‘The value of the integral using SIMPSONS ONE THIRD rule is %f:’ , s);
else
end;
OUTPUT:
EXPERIMENT – 11
FLOWCHART:
CODE:
Function[] = RK()
Clc;
Clear all;
df = inline(‘x + y*y’);
x = x0;
y = y0;
for x = x0:h:(xn – h)
k1 = h*df(x,y);
k2 = h*df(x+h/2, y+k1/2);
k3 = h*df(x+h/2, y+k2/2);
k4 = h*df(x+h, y+k3);
y = y+k;
end
end
OUTPUT:
EXPERIMENT – 12
0 -1 2
FLOWCHART:
CODE:
Function[] = p_method()
Clc;
Clear all;
[n, m] = size(a);
If (n == m)
e = max(abs(x));
r = x*a;
t = max(abs(r));
r = r/t;
for i = 1:n
fprintf(‘%d \t’,x(i));
end
fprintf(‘\n’)
eval = abs(t-e);
e = t;
maxerr = 0;
for i = i:n;
maxerr = evec
end
x(i) = r(i);
end
if((eval<aerr)&&(maxerr<aerr))
disp(r);
[v, b] = eig(a)
Disp(v);
fprintf(‘\n’)
disp(b);
return;
end
end
else
end
end
OUTPUT:
EXPERIMENT – 13
FLOWCHART:
CODE:
Function[] = simp()
Clear all;
Clc;
h = (xn – x0)/n;
y = inline(‘1/(1+x2)’);
a = mod(n,3);
if(a == 0)
s = y(x0) + y(xn);
for i = 1:(n-1)
b = mod(i,3);
if(b == 0);
s = s + 2*y(x0 + i*h);
else
s = s+3*y(x0 + i*h);
end
end
s = s*((3*h)/8);
else
end
end
OUTPUT: