用牛顿迭代法求方程x^3-3x-1=0在x0=2附近的根。 要求:给出程序和运行结果;计算结果保留4位有效数字

是用matlab 写程序的 不是直接用roots命令

syms x
f=x^3-3*x-1;
df=diff(f,x);

eps=1e-5;
x0=2;
cnt=0;
MAXCNT=200; %最大循环次数
while cnt<MAXCNT %防止无限循环
x1=x0-subs(f,x,x0)/subs(df,x,x0); %去掉分号可以看到迭代过程.
if (abs(x1-x0)<eps)
break;
end
x0=x1;
cnt=cnt+1;
end
if cnt==MAXCNT
disp '不收敛'
else
vpa(x1,8)
end

参考资料:http://zhidao.baidu.com/question/81067389.html?fr=ala0

温馨提示:答案为网友推荐,仅供参考
相似回答