% main.m
result = zeros(6,6);
iteration = zeros(6,6);
format compact % This shortens the output.
format long % This prints more decimal places.
i = 1;
enable = zeros(6,1);
enable(1,1)=1;
enable(2,1)=1;
enable(3,1)=1;
enable(4,1)=1;
enable(5,1)=1;
enable(6,1)=1;
leftbound = 0;
rightbound = 8;
step = 0.1;
fcleftbound = 18;
fcrightbound =fcleftbound;
for fc=fcleftbound:fcrightbound
x=leftbound:step:rightbound
for x0=leftbound:step:rightbound
fcp = 1000+fc;
esp = 1e-5;
%x0 = 3;
localvalue1 = [];
localvalue2 = [];
localvalue3 = [];
localvalue4 = [];
localvalue5 = [];
localvalue6 = [];
if enable(1,1)==1
[result(1,i), iteration(1,i),localvalue1] = Steffensen(fc,x0,esp);
end
if enable(2,1)==1
[result(2,i), iteration(2,i),localvalue2] = newton(x0,fc,fcp,esp);
end
if enable(3,1)==1
[result(3,i), iteration(3,i),localvalue3] = secant(x0,x0-0.0001,fc,esp);
end
if enable(4,1)==1
[result(4,i), iteration(4,i),localvalue4] = FixedPointIter(fc,x0,esp);
end
if enable(5,1)==1
[result(5,i), iteration(5,i),localvalue5] = Steffensen_diff(fc,fcp,x0,esp);
end
if enable(6,1)==1
[result(6,i), iteration(6,i),localvalue6] = Aitken(fc,x0,esp);
end
i=i+1;
%fc
%x0
%esp
%result
%iteration
%disp(['----------------------------------'])
end
%x=0:1:20;
switch(fc)
case 9
functionstr = ['sqrt(10/(x+4))-x'];
case 12
functionstr=['(2-exp(x)+x^2)/3-x '];
case 13
functionstr=['0.5*(sin(x)+cos(x))-x'];
case 14
functionstr=['sqrt(exp(x)/3)-x'];
case 15
functionstr=['5\^(-x)-x'];
case 16
%ex 7
functionstr=['x\^3-x-1'];
case 17
%ex 8
functionstr = ['x-2\^(-x)'];
case 5
functionstr = ['0.01*(p^3-3*p^2+3*p-1)'];
otherwise
functionstr=[''];
end
figure;
title(['f(x)= ' functionstr ' - initial guess - iteration times graph']);hold on;
xlabel(['initial guess number']);ylabel(['iteration times']);
if enable(1,1)==1
h=plot(x,iteration(1,:),'r-');set(h,'LineWidth',3);hold on;
end
if enable(2,1)==1
h=plot(x,iteration(2,:),'b-');set(h,'LineWidth',2);hold on;
end
if enable(3,1)==1
h=plot(x,iteration(3,:),'g-');set(h,'LineWidth',2);hold on;
end
if enable(4,1)==1
h=plot(x,iteration(4,:),'c-');set(h,'LineWidth',2);hold on;
end
if enable(6,1)==1
h=plot(x,iteration(6,:),'m-');set(h,'LineWidth',2);hold on;
end
if enable(5,1)==1
h=plot(x,iteration(5,:),'ko-');set(h,'LineWidth',1);hold on;
end
legend('Steffensen-g(x)=x+f(x)','newton','secant','FixedPointIterator','Aitken method','Steffensen-Newton',-1);
figure;
title(['f(x)= ' functionstr ' initial guess - result graph']);hold on;
xlabel(['initial guess number']);ylabel(['result']);
if enable(1,1)==1
h=plot(x,result(1,:),'r-');set(h,'LineWidth',3);hold on;
end
if enable(2,1)==1
h=plot(x,result(2,:),'b-');set(h,'LineWidth',2);hold on;
end
if enable(3,1)==1
h=plot(x,result(3,:),'g-');set(h,'LineWidth',2);hold on;
end
if enable(4,1)==1
h=plot(x,result(4,:),'c-');set(h,'LineWidth',2);hold on;
end
if enable(6,1)==1
h=plot(x,result(6,:),'m-');set(h,'LineWidth',2);hold on;
end
if enable(5,1)==1
h=plot(x,result(5,:),'ko-');set(h,'LineWidth',1);hold on;
end
legend('Steffensen-g(x)=x+f(x)','newton','secant','FixedPointIterator','Aitken method','Steffensen-Newton',-1);
%legend('Steffensen-g(x)=x+f(x)','newton','secant','FixedPointIterator','Aitken method',-1);
end