1. 程式人生 > >【原始碼】牛頓法求解實值函式的根

【原始碼】牛頓法求解實值函式的根

示例:[ x, ex ] = newton( ‘exp(x)+x’, ‘exp(x)+1’, 0, 0.5*10^-5, 10 )

f:輸入函式

df:輸入函式的導數

x0:函式根的初值估計

tol:誤差容忍度

namx:求解最大迭代次數

x:求解輸出的近似根

ex:誤差估計

MATLAB完整原始碼:

function [ x, ex ] = newton( f, df, x0, tol, nmax )

%

% NEWTON Newton’s Method

% Newton’s method for finding successively better approximations to the

% zeroes of a real-valued function.

%

% Input:

% f - input funtion

% df - derived input function

% x0 - inicial aproximation

% tol - tolerance

% nmax - maximum number of iterations

%

% Output:

% x - aproximation to root

% ex - error estimate

%

% Example:

% [ x, ex ] = newton( ‘exp(x)+x’, ‘exp(x)+1’, 0, 0.5*10^-5, 10 )

%

% Author: Tashi Ravach

% Version: 1.0

% Date: 16/04/2007

%

if nargin == 3

    tol = 1e-4;

    nmax = 1e1;

elseif nargin == 4

    nmax = 1e1;

elseif nargin ~= 5

    error('newton: invalid input parameters');

end



f = inline(f);

df = inline(df);

x(1) = x0 - (f(x0)/df(x0));

ex(1) = abs(x(1)-x0);

k = 2;

while (ex(k-1) >= tol) && (k <= nmax)

    x(k) = x(k-1) - (f(x(k-1))/df(x(k-1)));

    ex(k) = abs(x(k)-x(k-1));

    k = k+1;

end

end

關於牛頓法的MATLAB原始碼及PPT下載地址:

http://page5.dfpan.com/fs/blc4j2621129a165927/

更多精彩文章請關注微訊號:在這裡插入圖片描述