0% found this document useful (0 votes)
114 views

Experiment-5: AIM:WAP in MATLAB To Find The Root of Following Equations Using Gauss Elimination

The document describes experiments to solve various problems in MATLAB using numerical methods. It includes experiments to find the root of equations using Gauss elimination, Gauss Jordan method and Gauss Seidel method. It also includes experiments to find the inverse of a matrix, fit a parabola to data using least squares, solve differential equations using Runge Kutta method, and find eigenvalues using power method. The document provides the aim, flowchart and code for each experiment.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views

Experiment-5: AIM:WAP in MATLAB To Find The Root of Following Equations Using Gauss Elimination

The document describes experiments to solve various problems in MATLAB using numerical methods. It includes experiments to find the root of equations using Gauss elimination, Gauss Jordan method and Gauss Seidel method. It also includes experiments to find the inverse of a matrix, fit a parabola to data using least squares, solve differential equations using Runge Kutta method, and find eigenvalues using power method. The document provides the aim, flowchart and code for each experiment.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

EXPERIMENT- 5

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');

A=input('enter augumneted matrix(row wise)');

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;

A=input('enter elements of matrix');

[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('the aug matrix is:');

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('the inverse of mtarix is:');

disp(D);
disp('the inverse of the matrix using in built function');

disp(inv(A));

else

disp('singular matrix inverse dont exists');

end

else

disp('not a sqaure matrix');

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');

A=input('enter matrix(row wise)');

aerr=input('allowed error');

maxitr=input('max itertaions allowed');

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)

fprintf('\nthe solution using gauss siedal method is');

for i=1:N

fprintf('x[%d]=%f ',i,x(i));

end

return;

end

end

disp('max itr not sufficient');

end

OUTPUT:
EXPERIMENT- 9
AIM: WAP to fit a parabola y=𝑎+𝑏𝑥+𝑐𝑥2 using method of least square

for following data

X 1 1.5 2 2.5 3 3.5 4


Y 1.1 1.3 1.6 2 2.7 3.4 4.1

FLOWCHART:
CODE:

function [] = fitting()

clear all;

clc;

n=input('no of pair of observation');

augm=zeros(3,4);

augm(1,1)=n;

for i=1:n

fprintf('enter pair no %d :\t',i);

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

fprintf('the required parabola is y=(%f)+(%f)x+(%f)x^2',a,b,c);

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;

X0 = input(‘Enter the lower limit’);

Xn = input(‘Enter the upper limit’);

n = input(‘Enter the subintervals’);

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

fprintf(‘The value of the integral using trapezoidal rule is %f:’ , s);

end

OUTPUT:
2) SIMPSON’S ONE THIRD RULE:

FLOWCHART:
CODE:

Function[] = simp()

Clear all;

Clc;

X0 = input(‘Enter the lower limit’);

Xn = input(‘Enter the upper limit’);

n = input(‘Enter the subintervals’);

h = (xn – x0)/n;

y = inline(‘1/(1+x2)’);

if (rem(n,2) == 0)

s = y(x0) + y(xn) + 4*y(x0 + h);

for I = 3:2:(n-1)

s = s + 2*y(x0 + (i-1)*h) + 4*y(x0 + i*h)

end;

s =(s*h)/3;

fprintf(‘The value of the integral using SIMPSONS ONE THIRD rule is %f:’ , s);

else

fprintf(‘Enter the even no of intervals’);

end;

OUTPUT:
EXPERIMENT – 11

AIM: WAP in MATLAB to solve dy/dx = x + y2 , y(0) = 1 by R-K method of order 4.

FLOWCHART:

CODE:

Function[] = RK()

Clc;

Clear all;

x0 = input(‘Enter the value of x0’);

y0 = input(‘Enter the value of y0’);


xn = input(‘Enter the value of xn’);

h = input(‘Enter the difference’);

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

k = ( k1 + 2*k2 + 2*k3 +k4 )/6;

y = y+k;

fprintf(‘The value of y when x = %f is \n’, x+h,y);

end

end

OUTPUT:
EXPERIMENT – 12

AIM: WAP in MATLAB to find the largest eigen value of A = 2 -1 0

using powe method -1 2 -1

0 -1 2

FLOWCHART:
CODE:

Function[] = p_method()

Clc;

Clear all;

a = input(‘Enter the elemnts of the matrix’);

[n, m] = size(a);

If (n == m)

x = input(‘initial approximation of eigen vector’);

maxitr = input(‘Enter the max no of iterations’);

aerr = input(‘Enter the allowed error’);

e = max(abs(x));

for itr = 1:maxitr

r = x*a;

t = max(abs(r));

r = r/t;

fprintf(‘%d \t %f’, itr,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;

evec = abs(x(i) – r(i));

maxerr = evec

end

x(i) = r(i);

end

if((eval<aerr)&&(maxerr<aerr))

fprintf(‘The largest eigen value is %f \n’,t);


fprintf(‘The eigen vector is : ‘);

disp(r);

[v, b] = eig(a)

Disp(v);

fprintf(‘\n’)

disp(b);

return;

end

end

fprintf(‘No of iterations not sufficient’);

else

fprintf(‘Enter the square matrix’);

end

end

OUTPUT:
EXPERIMENT – 13

AIM:WAP a matlab code to evaluate:


6
∫0 1/(1 + 𝑥 2 ) 𝑑𝑥
Using simposon’s three-eight rule.

FLOWCHART:
CODE:

Function[] = simp()

Clear all;

Clc;

X0 = input(‘Enter the lower limit’);

Xn = input(‘Enter the upper limit’);

n = input(‘Enter the subintervals’);

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

fprintf(‘The value of integral using simpsons three-eight rule is %f’,s);

else

fprintf(‘Enter the subintervals as multiple of 3’);

end

end
OUTPUT:

You might also like