Linear fit through loglog plots.

3 次查看(过去 30 天)
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;
break;
end
n = n + 1;
end
figure, plot(0:nfinal - 1,x(1:nfinal),'o-')
title('Newton''s Method')
xlabel('Iterations')
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.

采纳的回答

Kevin Osborn
Kevin Osborn 2021-10-1
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));
scatter(x,y)
hold on
plot(x, alpha*x + log(lambda))
hold off
legend('Data','Fitted line','location','best')
title(['\alpha = ',num2str(alpha),' \lambda = ',num2str(lambda)])

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Biotech and Pharmaceutical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by