Clear Filters
Clear Filters

Linear fit through loglog plots.

2 views (last 30 days)
With the iteration data below find the best linear fit in the formula above and determine the slope, α (order of convergence), and the value of λ. How would I go about coding this in MatLab? I think I need to use loglog plots, but I'm not exactly sure how to go about this. I have used Newton's Method of approximation as shown below.
f = @(x) 2*exp(-2*x) + 4*sin(x) - 2*cos(2*x);
fp = @(x) 4*(-exp(-2*x) + sin(2*x) + cos(x));
x0 = 0.3;
N = 10;
tol = 1E-6;
x(1) = x0;
n = 2;
nfinal = N + 1;
while (n <= N + 1)
fe = f(x(n - 1));
fpe = fp(x(n - 1));
x(n) = x(n - 1) - fe/fpe;
if (abs(fe) <= tol)
nfinal = n;
n = n + 1;
figure, plot(0:nfinal - 1,x(1:nfinal),'o-')
title('Newton''s Method')
ylabel('Root Approximation')
table([1:length(x)]', x', 'VariableNames', {'Iteration' 'Root Approximation'})
x(n) = x(n - 1) - fe/fpe;
fprintf('Iteraion Root Approximation Tolerance Level\n')
fprintf('%3d: %20g %20g\n', n, x(n), abs(fe));
Thank you in advance for your help.
  1 Comment
Kevin Osborn
Kevin Osborn on 1 Oct 2021
I figured it out actually! Just needed to work on it a bit longer.

Sign in to comment.

Accepted Answer

Kevin Osborn
Kevin Osborn on 1 Oct 2021
This is how I plotted it, before cleaning up the graph to make it more pretty and what not.
y = log(abs(x(3:end) - x(2:end-1)));
x = log(abs(x(2:end-1) - x(1:end-2)));
p = polyfit(x,y,1);
alpha = p(1);
lambda = exp(p(2));
hold on
plot(x, alpha*x + log(lambda))
hold off
legend('Data','Fitted line','location','best')
title(['\alpha = ',num2str(alpha),' \lambda = ',num2str(lambda)])

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!