急!!!!!哪位大哥会matlab 帮忙写个程序,用牛顿迭代法求方程X^3-3*x-1=0在2附近的解
急!!!!!哪位大哥会matlab 帮忙写个程序,用牛顿迭代法求方程X^3-3*x-1=0在2附近的解
日期:2017-11-23 17:42:17 人气:2
syms x
x0=2;
f=x^3-3*x-1;
eps=1e-6;
maxcnt=1000;
fx=diff(f,x);
x1=x0;
cnt=1;
while cnt<=maxcnt
x2=x1-subs(f/fx,x,x1);
if abs(x1-x2)<eps
break;
end
[cnt,x1,x2] %迭代次数,迭代前,迭代后
x1=x2;
cnt=cnt+1;
end
subs(f,x,x2)