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

Iterative method

Uploaded by

222901001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Iterative method

Uploaded by

222901001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Iterative method:

clc;
clear all;
close all;
f=@(x)((2*x^2+1)/4);
xold=-1.8;
iter=0
error=2
while error>=1
iter=iter+1
xold
xnew=feval(f,xold)
error=abs((xnew-xold)/xnew)*100
xold=xnew;
end
disp('here%error<1')
root=xnew
Aitken Method:
clc;
clear all;
close all;
format short
format compact
f=@(x)((log10(x)+7)/2);
x1=0.34;
iteration=0;
accuracy=2;
while accuracy>=1
iteration=iteration+1
x1
x2=f(x1)
x3=feval(f,x2)
deltax2=x3-x2
deltasquarex1=x3-2*x2+x1
a=x3-((deltax2^2)/deltasquarex1)
accuracy=abs(a-x3)
x1=a;
end
disp('here,accuracy<1')
root=a

Bisection Method
clc;
clear all;
close all;
format short;
format compact;
xup=5;
xlow=-5;
xmidold=(xup+xlow)/2;
f=@(x)(x^3+3*x+5);
iteration=0;
difference=2;
while difference>=1
iteration=iteration+1
xup
xlow
xmidold
disp('f(xlow)=');f(xlow)
disp('f(xmidold)=');f(xmidold)

if f(xlow)*f(xmidold)>0
disp('here,f(xlow)*f(xmidold>0')
xlow=xmidold
elseif f(xlow)*f(xmidold)<0
disp('here,f(xlow)*f(xmidold)<0')
xup=xmidold
end
xmidnew=(xup+xlow)/2
difference=(xup-xlow)/2

if difference>=1
disp('here,difference>1')
xmidold=xmidnew
end
end
disp('here,difference<1')
root=xmidnew

Scant Method:
clc;
clear all;
close all;
x1=5;
x2=-5;
f=@(x)(x^5+x+1)
iter=0;
dif=2;
while dif>=1
x1
x2
f(x1)
f(x2)
x3=x2-((f(x2)*(x1-x2))/(f(x1)-f(x2)));
dif=abs(x3-x2)
if dif>=1
x1=x2
x2=x3
end
end
root=x3

Regula Falsi
clc;
clear all;
close all;
format short;
format compact
x1=7;
x2=10;
f=@(x)(3*x^3-7*x^2-3*x+2*x);
iter=0;
dif=2;
while dif>=1
iter=iter+1
x1
x2
disp('f(x1)=');f(x1)
disp('f(x2)=');f(x2)
x=x1-((f(x1)*(x2-x1))/(f(x2)-f(x1)));
if abs(f(x1))<abs(f(x2))
x2=x1, x1=x
elseif abs(f(x1))>abs(f(x2))
x1=x2, x2=x
end
dif=abs(x1-x2)
end
disp('here,difference<1')
root=x

Newton Raphson:
clc;
clear all;
close all;
syms x
format short;
format compact;
f=x^3-3*x-5
df=diff(f)
x1=2;
iter=0;
diff=2;
while diff>=1
iter=iter+1
x1
disp('df(x1)=');subs(f,x,x1)
disp('df(x1=');subs(df,x,x1)
x2=x1-(subs(f,x,x1)/subs(df,x,x1))
diff=abs(x2-x1)
x1=x2;
end
disp('here,difference<1')
root=x2

Differentiation forward difference


clc;
clear all;
close all;
xi=[];fi=[];
i=[2,4,8,12]
for j=1:4
xi=[xi,i(j)/2];
fi=[fi,(xi(j))^2+abs(xi(j))];
end
Delta_f1=fi(2)-fi(1)
Delta_f2=fi(3)-fi(2)
Delta_f3=fi(4)-fi(3)
Delta_Square_f1=Delta_f2-Delta_f1
Delta_Square_f2=Delta_f3-Delta_f2
Delta_Cube_f1=Delta_Square_f2-Delta_Square_f1
x=4.12
h=1;
k=(x-xi(1))/h
clear k;
syms k
f1=fi(1);
f2=k*Delta_f1;
f3=((k*(k-1))/factorial(2))*Delta_Square_f1;
f4=((k*(k-1)*(k-2))/factorial(3))*Delta_Cube_f1;
fx=f1+f2+f3+f4;
df=diff(fx)
diff_NGFDIF=(1/h)*subs(df,k,(x-xi(1))/h)

You might also like