📄 newtnd.m
字号:
function [x, ithist, iflag] = newtnd( f, x, tolf, tolx, maxit )%% function [x, ithist, iflag] = newtnd( f, x, tolf, tolx, maxit )%% fdnewtnd attempts to compute a root of F using Newton's method%% Input parameters:% f name of a matlab function that evaluates % f and its derivative.% x initial iterate% tolf stopping tolerance (optional. Default tolf = 1.e-7)% Newton's method stops if ||F(x)||_2 < tolf% tolx stopping tolerance (optional. Default tolx = 1.e-7)% Newton's method stops if ||s||_2 < tolx, % where s = -F'(x)^{-1} F(x) is the Newton step.% maxit maximum number of iterations (optional. Default maxit = 100)%%% Output parameters:% x approximation of the solution. % ithist array with the iteration history% The i-th row of ithist contains [it, x, F, s, t]% ifag return flag% iflag = 0 ||F(x)||_2 <= tolf % iflag = 1 iteration terminated because maximum number of % iterations was reached. ||F(x)||_2 > tolf % iflag = 2 iteration terminated because no step size was found%% Matthias Heinkenschloss% Department of Computational and Applied Mathematics% Rice University% March 11, 2004%% set tolerances if necessaryif( nargin <= 3 ) tolf = 1.e-7; tolx = 1.e-7; maxit = 100; endif( nargin <= 4 ) tolx = 1.e-7; maxit = 100; endif( nargin <= 5 ) maxit = 100; end it = 0;iflag = 0;[F,Jac] = feval(f, x);normF = norm(F); s = tolx*ones(size(x));t = 1;while( it < maxit & norm(t*s) > tolx & normF > tolf ) s = - (Jac\F); t = 1; xtmp = x+s; F = feval(f, xtmp); while ( norm(F)^2 > (1-t*2.e-4)*normF^2 & norm(t*s) > tolx ) t = t/2; xtmp = x+t*s; F = feval(f, xtmp); end ithist(it+1,:) = [it, norm(x), normF, norm(t*s), t]; x = xtmp; it = it+1; [F,Jac] = feval(f, x); normF = norm(F);end% check why the Newton's method truncated and set iflagif( norm(F) > tolf ) % Newton's method truncated because the maximum number of iterations % was reached iflag = 1; returnelseif( norm(t*s) > tolx & t < 1 ) iflag = 2; returnelse % Newton's method truncated because norm(F) <= tolf % print info for last iteration ithist(it+1,:) = [it, norm(x), norm(F),0,0];end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -