⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newton1.m

📁 数值分析, 同济大学教材<<现代数值数学和计算>>数值分析课程所有算法的matlab代码,所有例程均经过测试,私家珍藏
💻 M
字号:
function [x,it,hist]=newton1(x0,f,g,maxit,tol,dnhl) 
%% 
%% find the zero of function f by Newton method 
%% Usage [x,it,hist]=newton(x0,f,g,maxit,tol,dnhl) 
%% f    function 
%% g    gradient 
%% dnhl 1: downhill search 
%%      0: no search, steplength = 1 
%% 
if nargin<6,           dnhl = 1; 
   if nargin<5,         tol = 1e-6; 
      if nargin<4,    maxit = 100; 
         if nargin<3, error('too few input!!'); 
         end; end; end; end; 
x    = x0; 
hist = x; 
fx   = feval(f,x); 
for it = 1:maxit, 
    if norm(fx)<=tol, 
        disp('Newton Iteration successes!!') 
        return; 
    end 
    d = feval(g,x)\fx; 
    if dnhl==1, 
        lambda = 1.0; 
        xn     = x - d; 
        fn     = feval(f,xn); 
        while norm(fn)>=norm(fx), 
            lambda = lambda * 0.85; 
            if lambda<tol, 
                error('too short step!!'); 
            end 
            xn     = x - lambda * d; 
            fn     = feval(f,xn); 
        end 
        x  = xn; 
        fx = fn; 
    else 
        x  = x - d; 
        fo = fx; 
        fx = feval(f,x); 
        if norm(fx)>norm(fo), 
            warning('an uphill direction!!'); 
        end 
    end 
    hist = [hist x]; 
end 
disp('Newton Iteration fails!!'); 
%% end of newton 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -