You are on page 1of 7

Roll No .

02

GAUSS SEIDAL (SIMULTANEOUS EQUATIONS)

clc,
n=input('enter the no. of unknows:');
a=input('enter the coefficient matrix:');
m=input('enter the no. of iteration:');
for i=1:n
x(i)=0;
end;
for k=1:m
for i=1:n
p=0;
for j=1:n
if i~=j
p=p+a(i,j)*x(j);
end;
end;
x(i)=(a(i,n+1)-p)/a(i,i);
fprintf('for %d iteration value of x(%d)=%f\n',k,i,x(i));
end;
end;

OUTPUT

enter the no. of unknows:3

enter the coefficient matrix:[83 11 -4 95 ; 7 52 13 104 ; 3 8 29 71]

enter the no. of iteration:5

for 1 iteration value of x(1)=1.144578

for 1 iteration value of x(2)=1.845922

for 1 iteration value of x(3)=1.820651

for 2 iteration value of x(1)=0.987680

for 2 iteration value of x(2)=1.411880

for 2 iteration value of x(3)=1.956618

for 3 iteration value of x(1)=1.051756

for 3 iteration value of x(2)=1.369263


Roll No .02

for 3 iteration value of x(3)=1.961746

for 4 iteration value of x(1)=1.057652

for 4 iteration value of x(2)=1.367187

for 4 iteration value of x(3)=1.961708

for 5 iteration value of x(1)=1.057925

for 5 iteration value of x(2)=1.367160

for 5 iteration value of x(3)=1.961688

>>
Roll No .02

NEWTON FORWARD DIFFERENCE INTERPOLATION


clc;
x=input('the diff values of x:');
y=input('the coressponding values of y:');
a=input('the values of x:');
n=input('the no of values:');
h=x(2)-x(1);
p=(a-x(1))/h;
m=n;
for i=2:n
m=m-1;
for j=1:m
y(i,j)=y(i-1,j+1)-y(i-1,j);
end;
end;
sum=y(1,1);
for i=1:n-1
pr=1;
for j=1:i
pr=pr*(p-j+1);
end;
sum=sum+(pr*y(i+1,1))/factorial(i);
end;
fprintf('the solution is=%f',sum);

OUTPUT

the diff values of x:[2 3 4 5 6 7 8 9]

thecoressponding values of y:[19 48 99 178 291 444 643 894]

the values of x:3.5

the no of values:8

the solution is=70.375000>>


Roll No .02

NEWTON RAPHSON METHOD


clc;
f=input('The given funct:');
fd=input('the derivative of given funct:');
a=input('the value of a:');
n=input('the no of iteration:');
d=1;
while(d>0)
b=input('the value of b:');
d=f(a)*f(b);
end;
x=input('the value of xinput btw a and b:');
for i=1:n
x=x-(f(x)/fd(x));
end;
fprintf('the integration of given function=%f',x);

OUTPUT

The given funct:@(x)(sin(x)-(x)*cos(x))

the derivative of given funct:@(x)((x)*sin(x))

the value of a:-1

the no of iteration:3

the value of b:-4.712

the value of xinput btw a and b:4.5

the integration of given function=4.493409>>


Roll No .02

>> % root of (linear)

>> p=[1 0 -5 3];

>> r=root (p)

r=

-2.4909

1.8342

0.6566

>> % root of equation (transcendal)

>> syms x

>> p=’3*x-cos (x)-1’;

>> r= solve (p)

r=

.60710164810312263122482837345049

>> % simpson’s method for integration

>> fun=’4*x+2’;

>> l=quad (fun , 1, 4)

l=

36

>> % Trapezoidal rule for integration

>> x=1:0.5:4;

>> l=trapz (x, 4*X+2)

l=

36
Roll No .02

>> % Interpolation

>> x=[50 51 52 53 54];

>> y=[39.1961 39.7984 40.3942 40.9843 41.5687]

>> interp1 (x, y, 50.5)

Ans=

39.4971

>> % solving simultaneous equations

>> a=[3 -0.1 -0.2; 0.1 7 -0.3; 0.3 -0.2 10];

>> b=[7.85; -19.3; 71.4];

>> linsolve (a,b)

ans =

3.0000

-2.5000

7.0000

>> % least square / curve fitting

>> x= [-2 -1 0 1 2];

>> y=[15 1 1 3 19];

>> polyfit (x, y 3)

ans=

0.0 4.4286 1.0000 -1.0571


Roll No .02

>> % Runge –kutta 4 for order

>> x=% save a .m file with name funrk4 and syntax as below

Function dy= f(x, y);

Dy=((y^2)-(x^2))/((y^2)+(x^2));

>> [x, y]=ode45(‘funrk4’,[0:0.2:0.4], 1)

X=

0.2000

0.4000

Y=

1.0000

1.1960

1.3753

You might also like