作者gfs777 (成人展三万嫖妓)
看板MATLAB
标题[讨论] 牛顿法的求值与绘图问题>"<
时间Tue Nov 15 11:03:36 2011
是这样的>"< 这个问题是老师後天考试前发下来 给大家作为范例参考的题目
如下:
================================================================
Newton's method is an efficient algorithm for finding approximations to the
zeros (or roots) of a real-valued function. The algorithm can be written
as follows
Xn+1 = Xn - [f(Xn) / f'(Xn)]
Newton’s method iteratively approximates the root(s) for function f(x)
until Xn+1 and Xn difference is smaller than real number e
for example |Xn+1 - Xn| <e
(e没给 我自设 1e-4)
write a script that that performs the Newton method to find a root for
y = xn^3 - 5*xn^2 - xn + 5
Plot the Xn on the y-axis with number of iterations on the x-axis.
===========================================================
目前我写成这样:
xn = input('first x?\n');
y = xn^3 - 5*xn^2 - xn + 5;
yplum = 3*xn^2 - 10*xn - 1;
cnt = 1;
e = 1e-4;
while cnt<=10000;
xn1 = xn - y / yplum
if abs(xn1 - xn)<= e
fprintf('converged')
break;
end
xn = xn1
cnt = cnt + 1
end
===========================================================
问一下这样是对的吗?
xn = -1.6062e+005 这答案是对的吗? 我起始x = 50
xn = -2.9412e+005 起始x = 90
xn = -4.9415e+005 起始x= 150
跑几次都不一样><
另外画图要怎麽画? 跪求~感谢><
那时候作业有个神人教我用clf hold figure等
现在我又不会用了....当时画出来的都跟同学不一样><
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.76.121
1F:→ DKer:回圈很有问题 自己想像模拟一下马上就会看出来了 11/15 12:59
2F:→ DKer:你在回圈里没有重新计算xn,y,yplum 跑再多次都一样 11/15 13:00
3F:→ DKer:然後记得每一行结尾要加 ; 11/15 13:00
4F:→ gfs777:感谢大大。目前解答改成 11/15 15:03
xn = input('first x?\n');
cnt = 1;
e = 1e-4;
while cnt<=10000;
y = xn^3 - 5*xn^2 - xn + 5;
yplum = 3*xn^2 - 10*xn - 1;
xn1 = xn - y / yplum
if abs(xn1 - xn)<= e
fprintf('converged')
break;
end
xn = xn1
cnt = cnt + 1
end
预祝大大事事顺利
※ 编辑: gfs777 来自: 140.112.183.66 (11/15 15:04)