%% Question 7 Solution
load ENGR20005_Workshop5p6.mat
%%
% Bisection
error = abs(data_bisection-3);
ei1 = error(2:end);
ei = error(1:end-1);
plot(ei,ei1,’.’,’MarkerSize’,16)
polyfit(log(ei),log(ei1),1)
power_regression(ei(1:end),ei1(1:end),[0.6,1.2]’)
fit(ei(1:end)’,ei1(1:end)’,’a*x^b’)
%%
% False Position
error = abs(data_false_position-3);
ei1 = error(2:end);
ei = error(1:end-1);
plot(ei,ei1,’.’,’MarkerSize’,16)
polyfit(log(ei),log(ei1),1)
power_regression(ei(1:end),ei1(1:end),[0.4,1.1]’)
fit(ei(1:end)’,ei1(1:end)’,’a*x^b’)
%%
% Fixed Point
error = abs(data_fixed_point_iteration-3);
ei1 = error(2:end);
ei = error(1:end-1);
plot(ei,ei1,’.’,’MarkerSize’,16)
polyfit(log(ei),log(ei1),1)
power_regression(ei(1:end),ei1(1:end),[0.7,1.1]’)
fit(ei(1:end)’,ei1(1:end)’,’a*x^b’)
%%
% Secant
error = abs(data_secant-3);
ei1 = error(2:end);
ei = error(1:end-1);
plot(ei,ei1,’.’,’MarkerSize’,16)
polyfit(log(ei),log(ei1),1)
[param,out] = power_regression(ei(3:end),ei1(3:end),[0.5,1.6]’)
fit(ei(3:end)’,ei1(3:end)’,’a*x^b’)
%%
% Newton-Raphson
error = abs(data_newton_raphson-3);
ei1 = error(2:end);
ei = error(1:end-1);
plot(ei(2:end),ei1(2:end),’.’,’MarkerSize’,16)
hold on
plot(linspace(1e-10,0.2,100),0.22198699406178.*linspace(0,0.2,100).^(1.97834562468368))
plot(linspace(0,0.2,100),0.05545.*linspace(0,0.2,100).^(1.208))
polyfit(log10(ei),log10(ei1),1)
power_regression(ei(2:end),ei1(2:end),[0.2,1]’)
hold off
fit(ei(2:end)’,ei1(2:end)’,’a*x^b’)
%%
function [a,out] = power_regression(x,y,a0)
tol = 10^-16;
a = a0;
d = [sum(2*a(1)*x.^(2*a(2))-2*(x.^a(2)).*y);
sum(2*a(1)^2*log(x).*(x.^(2*a(2)))-2*a(1)*log(x).*x.^(a(2)).*y)];
i = 1;
iter_max=500;
while max(d)>tol && i
out = 0;
else
out = 1;
end
end