Newton's Method Convergence Plot

1 次查看(过去 30 天)
Zyruss Edjan
Zyruss Edjan 2019-10-17
回答: Sid Singh 2019-10-21
Im currently trying to creat a convergence plot for my Newton's Method code. But I keep on getting a function error message that says Index exceeds the numbers of elements. I'm not really sure how to fix it.
function x_root = newtonze(x_est, tol, max_iter)
w = 20*10^3; L = 4; E = 70*10^9; I = 52.9*10^(-6);
for i = 1:max_iter
Xi = x_est - ((w*(7*L^4 - 30*L^2*x_est^2 + 15*x_est^4))/(360*E*I*L))/(-(w*x_est*(L^2 - x_est^2))/(6*E*I*L));
if abs((Xi-x_est)/x_est) < tol
x_root = Xi;
break
end
x_est=Xi;
end
x_root=x_est;
hold on
% Plot evolution of the solution
figure('Color','White')
plot(0:max_iter-1,x_root(1:max_iter),'o-')
title('Newton''s Method Solution: $f(x) = x + x^{\frac{4}{3}}$','FontSize',20,'Interpreter','latex')
xlabel('Iteration','FontSize',16)
ylabel('$x$','FontSize',16,'Interpreter','latex')
end

回答(1 个)

Sid Singh
Sid Singh 2019-10-21
Hi, you are trying to plot a scalar value "x_root" against a time series which is why you are getting the error. Create an array
x_root = zeros(1, max_iter);
for i = 1:max_iter
Xi = x_est - ((w*(7*L^4 - 30*L^2*x_est^2 + 15*x_est^4))/(360*E*I*L))/(-(w*x_est*(L^2 - x_est^2))/(6*E*I*L));
if abs((Xi-x_est)/x_est) < tol
x_root(i) = Xi;
break
end
x_est=Xi;
end
and store values in that array. You would get zeros in the plot where this
if abs((Xi-x_est)/x_est) < tol
condition is not being met.

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by