问题描述:
Matlab 里编写的牛顿迭代法,不知道什么出错了,请大家帮帮忙
function newton(f,a)
g=diff(f);
for n=1:20
x=a;
fa=eval(f);
ga=eval(g);
a=a-fa/ga;
X=a;
format long
disp(X)%output 'approximate value of a'
end
当我用x^3+x^2+5=0来检查的时候,答案是正确的;当我用cos(x)-x来检查的时候,输出的东西非常奇怪
» newton('cos(x)-x',10)
-13.77099420154660
1.993511775153038e+002
-2.020292634287075e+004
3.159234177166328e+004
1.005958011583579e+004
1.671119202654307e+003
-4.338983151322527e+002
2.360027467552869e+002
-1.424684491875079e+002
-67.31957649476091
-33.34425589894090
4.884120479595355e+002
-8.719276647547400e+004
4.703381773171524e+005
-1.806200566117338e+006
-9.030749196921680e+005
-4.156637151853491e+005
-1.182332523540095e+005
1.468631133479570e+005
-9.370856705585728e+003
不知道哪里出错了,请大家帮帮忙把,答案应该是0.7左右
function newton(f,a)
g=diff(f);
for n=1:20
x=a;
fa=eval(f);
ga=eval(g);
a=a-fa/ga;
X=a;
format long
disp(X)%output 'approximate value of a'
end
当我用x^3+x^2+5=0来检查的时候,答案是正确的;当我用cos(x)-x来检查的时候,输出的东西非常奇怪
» newton('cos(x)-x',10)
-13.77099420154660
1.993511775153038e+002
-2.020292634287075e+004
3.159234177166328e+004
1.005958011583579e+004
1.671119202654307e+003
-4.338983151322527e+002
2.360027467552869e+002
-1.424684491875079e+002
-67.31957649476091
-33.34425589894090
4.884120479595355e+002
-8.719276647547400e+004
4.703381773171524e+005
-1.806200566117338e+006
-9.030749196921680e+005
-4.156637151853491e+005
-1.182332523540095e+005
1.468631133479570e+005
-9.370856705585728e+003
不知道哪里出错了,请大家帮帮忙把,答案应该是0.7左右
问题解答:
我来补答展开全文阅读