You are on page 1of 5

'Bisection Method'

clc,clear all
disp('Bisection method')
i=0;
x1=1;
x2=2;
fx3=1;
tol=0.00l;
disp('
i
x1
x2
x3
dx1
fx2
fx3
')
disp('___________________________________________________________________________')
fx1=x1^3+3*x1^2-3*x1-3;
fx2=x2^3+3*x2^2-3*x2-3;
if (fxl*fx2>0)
disp(' ther is no root')
else
while (abs(fx3)>tol)
fx1=x1^3+3*x1^2-3*x1-3;
fx2=x2^3+3*x2^2-3*x2-3;
x3=(x1+x2)/2;
fx3=x3^3+3*x3^2-3*x3-3;
fprintf('%9.6f%13.6f%13.6f%13.6f%13.6f%13.6f%13.6f\n',i,x1,x2,x3
,fx1,fx2,fx3)
if (fxl*fx3>0)
xl=x3;
else
x2=x3;
end
i=i+1;
end
end

'newten rapsen method'


clc clear all
disp('newten rapsen method')
i=0;
disp('
i
x1
x2
x3
dx1
fx2
fx3
')
disp('___________________________________________________________________________')
x1=1;
x2=x1;
tol=0.001;
fx2=1;
k=-1;
fx1=x1^3+3*x1^2-3*x1-3;
if(fx1==0 && dfx1==0)
disp('ther is no root')
else
while (abs(x2-k)>tol)
fx1=x1^3+3*x1^2-3*x1-3;
dfx1=3*x1^2+6*x1-3;
x2=xl-fxl/dfx1;
k=(x1);
xl=x2;
fx2=x2^3+3*x2^2-3*x2-3;
fprintf('%9.6f%13.6f%13.6f%13.6f%13.6f\n',i,x1,x2,fx1,fx2)
x1=x2;
i=i+1;
end
end
x2

'fixed point method'


clc clear all
disp(fixed point method')
i=0;
x1=1;
x2=x1;
fx3=1;
tol=0.001;
y=2;
disp('
i
x1
x2
')
disp('__________________________________________________________')
while(abs(y)>tol)
x1=x2;
x2=(x1-2)^1/2;
fprintf('%9.6f%13.6f%13.6f\n',i,x1,x2)
end
disp('the root')
x2
y

'gauss_elemination'
function x=gauss_elemination(a,b)
if size(b,2)>1;
b=b';
end
n=length(b);
for j=1:n;
for i=j+1:n;
if a(i,j)~=0
m=a(i,j)/a(j,j);
a(I,j+1:n)=a(i,j+1:n)-m*a(j,j+1:n);
b(i)=b(i)-m*b(j);
end
end
end
for j=n:-1:1
b(j)=(b(j)-a(j,j+1:n)*b(j+1:n))/a(j,j);
end
x=b

'gauss_jordan'
Function x=gauss_jordan(a,b)
If size(b,2)>1;
e=b';
end
n=length(b);
for r=1:n-1
for i=r+1:n
if a(I,r)~=0
i=a(i,r)/a(r,r)
a(i,r:n)=a(i,r:n)-i*a(r,r:n)
b(i)=b(i)-i*b(r)
end
end
end
for r=2:n
for i=r-1:-1:1
if a(i,r)~=0
i=a(i,r)/a(r,r)
a(i,r:n)=a(i,r:n)-i*a(r,r:n)
a(:,:)
b(i)=b(i)-i*b(r)
end
end
end
a
c=[a,b]
for i=1:n
c(i,i:n+1)=c(i,i:n+1)/c(i,i);
end
c(1,4)
for i=1:n;
x(i)=c(I,n+1);
end

'jaccobi'
clc
a=[8 1 -1 8;2 -7 1 -4;2 1 9 12];
tol=0.001;
i=0;
x5=-1;
x4=-1;
x9=-1;
x10=-1;
disp('
i
x1
x2
x3
')
disp('___________________________________________________________')
x1=0;
x2=0;
x3=0;
while((abs(x5)>=tol)||(abs(h)>=tol)||(abs(f)>=tol))
x6=(-a(1,2)*x2-a(1,3)*x3+a(1,4))/a(1,1);
x7=(-a(2,1)*x1-a(2,3)*x3+a(2,4))/a(2,2);
x8=(-a(3,1)*x1-a(1,2)*x2+a(3,4))/a(3,3);
x5=x1-x4;
h=x2-x9;
f=x3-x10;
fprintf('%3.0f%13.6f%13.6f%13.6f\n',i,x1,x2,x3)
x4=x1;
x9=x2;
x10=x3;
i=i-1;
x1=x6;
x2=x7;
x3=x8;
end

'second method'
clc,clear all
disp('second method')
i=0;
x1=1;
x2=2;
fx3=1;
tol=0.00l;
disp('
i
x1
x2
x3
dx1
fx2
fx3
')
disp('_______________________________________________________________________________')
fx1=x1^3+3*x1^2-3*x1-3;
fx2=x2^3+3*x2^2-3*x2-3;
while (abs(fx3)>tol)
fx1=x1^3+3*x1^2-3*x1-3;
fx2=x2^3+3*x2^2-3*x2-3;
x3=x2-(fx2*(x2-x1)/(fx2-fx1));
fx3=x3^3+3*x3^2-3*x3-3;
fprintf('%9.6f%13.6f%13.6f%13.6f%13.6f%13.6f%13.6f\n',i,x1,x2,x3
,fx1,fx2,fx3)
if (abs(fx1)>abs(fx3))
x1=x2;
end
i=i+1;

'siedel'
clc
a=[8 1 -1 8;2 7 -1 -4;2 1 9 12];
tol=0.001;
i=0;
x5=-1;
x4=-1;
disp('
i
x1
x2
x3
')
disp('___________________________________________________________')
x1=0;
x2=0;
x3=0;
while((abs(x5)>=tol))
x1=(-a(1,2)*x2-a(1,3)*x3+a(1,4))/a(1,1);
x2=(-a(2,1)*x1-a(2,3)*x3+a(2,4))/a(2,2);
x3=(-a(3,1)*x1-a(1,2)*x2+a(3,4))/a(3,3);
x5=x1-x4;
fprintf('%3.0f%13.6f%13.6f%13.6f\n',i,x1,x2,x3)
x4=x1;
i=j+1;
end

You might also like