Newton method MATLAB 实现
%求解目标函数:f(x) = cos(x) - x^3
%x0 is initial value
%err: stop condition
%output root: the root of the function
function root = NewtonMethod(x0,err)
f_x0 = cos(x0) - x0^3;%f(x0)的函数值
f_x0_diff = -sin(x0)-3*x0^2;%f(x0)的导数
x1 = x0 - f_x0/f_x0_diff;
delta = abs(x1 - x0);
while delta > err
x0 = x1;
f_x0 = cos(x0) - x0^3;
f_x0_diff = -sin(x0)-3*x0^2;
x1 = x0 - f_x0/f_x0_diff;
delta = abs(x1 - x0);
end
root = x1;
作者:Alex Yu
出处:http://www.cnblogs.com/biaoyu/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。