function [xnew, y] = NM (fun, x0, tol, max)
syms x
xnew = zeros(max, 1);
y = zeros(max, 1);
xold = zeros(max, 1);
xold(1)=x0;
fp=inline(diff(fun(x)));
for i = 1 : max
xnew(i) = xold(i)-(fun(xold(i))/fp(xold(i)));
y(i) = feval(fun, xnew(i));
if (abs(xnew(i) - xold(i)) < tol)
fprintf('NM method has converged\n');
break;
end
if y(i) == 0
fprintf('Exact solution found\n');
break;
end
iter = i+1;
end
if (iter > max)
fprintf('Zero not found to desired tolerance within the maximum number of iterations\n');
end
% Output results
k = 1:iter;
fprintf(' iter xold xnew y\n');
disp([k' xold(1:iter) xnew(1:iter) y(1:iter)]);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/12704.html
標籤:數據庫相關
上一篇:原民師養老補貼管理系統開發設計
