Академический Документы
Профессиональный Документы
Культура Документы
GAUSS ELIMINATION
functionx=gauss_elimination(A, b)
disp("performed by zahid’)
[m,n]=size(A);
ifm~=nthen
disp("A is not a square matrix")
else
fork=1:n-1
fori=k+1:n
ifA(k,k)==0then
disp('error=null diagonal element');
else
r=A(i,k)/A(k,k);
b(i)=b(i)-r*b(k);
forj=1:n,
A(i,j)=A(i,j)-r*A(k,j);
end
end
end
end
end
disp("A")
endfunction
OUTPUT-
--> A=[1 1 1;4 3 -1;3 5 3]
A =
1. 1. 1.
4. 3. -1.
3. 5. 3.
-->gauss_elimination(A,b)
performed by zahid
1. 1. 1.
0. -1. -5.
0. 0. -1 0.
5. BACK SUBSTITUTION
function[x]=gauss_elimination(A, b)
disp("performed by zahid")
[m,n]=size(A);
ifm~=nthen
disp("A is not a square matrix")
else
fork=1:n-1
fori=k+1:n
ifA(k,k)==0then
disp('error=null diagonal element');
else
r=A(i,k)/A(k,k);
b(i)=b(i)-r*b(k);
forj=1:n,;
A(i,j)=A(i,j)-r*A(k,j);
end
end
end
end
end
disp(A,"A")
fori=n:-1:1
s=0;
x(i)=0;
forj=1:n
s=s+A(i,j)*x(j);
end
x(i)=(b(i)-s)/A(i,i);
end
endfunction
OUTPUT-
-->gauss_elimination(A,b)
performed by zahid
A
1. 1. 1.
0. -1. -5.
0. 0. -10.
ans =
1.
0.5
-0.5
6.GAUSS-SIEDEL METHOD
Source code-
clc
clear
function[x]=gauss1(A, b, I)
disp('performed by zahid’)
[n,m]=size(A)
ifn~=m,error('A is not a square matrix');else
printf('%10s %10s %13s %12s\n','iteration','x(1)','x(2)','x(3)')
fori=1:n
x(i)=0
end
fork=1:I
fori=1:n
s=0;
forj=1:n
if(j~=i)
s=s+A(i,j)*x(j);
x(i)=(b(i)-s)/A(i,i);
end
end
end
fori=1:n
printf('%14.5f',x(i))
end
printf('\n')
end
end
endfunction
Output-
12.
12.
12.
--> A=[10,1,1;1,10,1;1,1,10]
A =
10. 1. 1.
1. 10. 1.
1. 1. 10.
--> I=10
I =
10.
-->gauss1(A,b,I)
performed by zahid
iteration x(1) x(2) x(3)
1.20000 1.08000 0.97200
0.99480 1.00332 1.00019
0.99965 1.00002 1.00003
1.00000 1.00000 1.00000
1.00000 1.00000 1.00000
1.00000 1.00000 1.00000
1.00000 1.00000 1.00000
1.00000 1.00000 1.00000
1.00000 1.00000 1.00000
1.00000 1.00000 1.00000
ans =
1.
1.
EXPERIMENT NO-7
GAUSS SIEDAL METHOD
SOURCE CODE:-
clc
clear
function[x]=gauss1(A, b, I)
[n,m]=size(A);
ifn~=mthen;error('A is not a square matrix');else
printf('%10s %10s %13s %12s\n','iteration','x(1)','x(2)','x(3)')
fori=1:n
x(i)=0
end
fork=1:I
fori=1:n
s=0;
forj=1:n
if(j~=i)
s=s+A(i,j)*x(j);
x(i)=(b(i)-s)/A(i,i);
end
end
end
printf('%5i\t',k)
fori=1:n
printf('%14.5f',x(i))
end
printf('\n')
end
end
endfunction
OUTPUT
EXPERIMENT NO:8(POWER METHOD)
SOURCE CODE:
clc
clear
A=input('enter the matrix A')
x0=input('inter the initial vector x0')
err=input('enter the permissible error')
n=input('input the number of iterations')
m=0;
fori=1:n
x1=A*x0;
m=max(abs(x1));
x2=x1/m;
disp([i,m])
disp(x2)
ifabs(x2-x0)<err
disp('we get the desired result')
break;
end
x0=x2
end
OUTPUT:
1. 2.
1.
-0.5
0.
2. 2.5
1.
-0.8
0.2
3. 2.8
1.
-1.
0.4285714
4. 3.4285714
0.875
-1.
0.5416667
5. 3.4166667
0.804878
-1.
0.6097561
6. 3.4146341
0.7642857
-1.
0.65
7. 3.4142857
0.7405858
-1.
0.6736402
8. 3.4142259
0.7267157
-1.
0.6875
9. 3.4142157
0.718593
-1.
0.695621
10. 3.4142139
0.7138352
-1.
0.7003785
EXPERIMENT -10
TRAPEZOIDAL SIMPSON FORMULA
Source code:-
clc
clear
sum=0
deff('d=f(x)','d=x^3')
n=input('The number of subinterval')
a=input('Enter the lower limit')
b=input('Enter the upper limit')
sum=f(a)+f(b)
h=(b-a)/n
fori=1:n-1
sum=sum+2*f(a+i*h)
end
I=h*(sum)/2
disp(I,'Integral is=')
OUTPUT
EXPERIMENT-11
ODE by Euler Method
SOURCE CODE :
clc
clear
deff('d=f(x,y)','d=x+y')
n=input('Enter the number of steps')
x0=input('Enter the initial value of x')
y0=input('Enter the value of y')
xn=input('Enter the value of x at which y is required')
h=(xn-x0)/n
printf('%5s,%10s \n','i','y1')
fori=1:n
y1=y0+h*f(x0,y0)
x1=x0+h
printf('%5i,%10.5f\n',i,y1)
y0=y1;
x0=x1;
end
disp(y1,'y1=')
disp(“Performed by zahid”)
OUTPUT:
Enter the number of steps5
Enter the initial value of x4
Enter the value of y8
Enter the value of x at which y is required5
i, y1
1, 10.40000
2, 13.32000
3, 16.86400
4, 21.15680
5, 26.34816
y1=
26.34816
12.RK METHOD
CODE:
clc
clear
disp('RK method By zahid')
function[]=RK4(x0, y0, xn, n)
h=(xn-x0)/n
y(1)=y0;
x(1)=x0;
fori=1:n
k1=h*f(x(i),y(i));
k2=h*f(x(i)+h/2,y(i)+k1/2);
k3=h*f(x(i)+h/2,y(i)+k2/2);
k4=h*f(x(i)+h,y(i)+k3);
k=(k1+2*k2+2*k3+k4)/6;
y(i+1)=y(i)+k
x(i+1)=x(i)+h;
disp([i,x(i+1),y(i+1)])
end
v=[x',y'];
plot(x,y)
endfunction
functiondydx=f(x, y)
dydx=y+exp(x)
endfunction
Output:
RK method By zahid
-->x0=0;y0=0;xn=1;n=5;
--> RK4(x0,y0,xn,n)
1. 0.2 0.2442753
2. 0.4 0.5967163
3. 0.6 1.0932452
4. 0.8 1.7803882
5. 1. 2.7182108
GRAPH: