Secant method MATLAB 实现

%求解目标函数:f(x) = 4*x^3-16*x^2+17*x-4
%x1, x2 are two initial values
%err: stop condition
%output root: the root of the function
function root = secant (x1,x2,err)
f_x1 = 4*x1^3-16*x1^2+17*x1-4;%f(x1)的函数值
f_x2 = 4*x2^3-16*x2^2+17*x2-4;%f(x2)的函数值
delta = abs(x2 - x1);

while delta > err
x3 = x2 - f_x2 * (x2 - x1)/(f_x2 - f_x1);
x1 = x2
x2 = x3
f_x1 = 4*x1^3-16*x1^2+17*x1-4;
f_x2 = 4*x2^3-16*x2^2+17*x2-4;
delta = abs(x2 - x1);

end
root = x2;

posted on 2011-12-03 18:43  Alex Yu  阅读(3831)  评论(0编辑  收藏  举报

导航