Linear fit through loglog plots.

조회 수: 3 (최근 30일)
Kevin Osborn
Kevin Osborn 2021년 10월 1일
답변: Kevin Osborn 2021년 10월 1일
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.
  댓글 수: 1
Kevin Osborn
Kevin Osborn 2021년 10월 1일
I figured it out actually! Just needed to work on it a bit longer.

댓글을 달려면 로그인하십시오.

채택된 답변

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