
f[x_]:=x*Exp[x]-x^2
Plot[f[x],{x,-3,3}]

FindRoot[f[x],{x,2}]
(*利用NestList將牛頓法計算過程列出*)
sol1=N@{#,f[#]}&/@NestList[N[#-f[#]/f'[#]]&,2,10]
Plot[f[x],{x,-2.1,2.1},PlotStyle->Thickness[0.005],
Epilog->{Black,Thickness[0.005],Line[sol1],Brown,
PointSize[0.025],Point[sol1],Red,PointSize[0.025/2],
Point[sol1]}]

(*利用FindRoot函數中EvaluationMonitor選項將牛頓法計算過程列出*)
sol2=Reap[FindRoot[f[x],{x,2},EvaluationMonitor:>Sow[{x,f[x]}]]][[2]]
Plot[f[x],{x,-2.1,2.1},
Epilog->{Black,Thickness[0.005],Line[sol2],Brown,
PointSize[0.025],Point[sol2],Red,PointSize[0.025/2],
Point[sol2]}]

讀者回應 ( 0 意見 )
訂閱發佈留言 (Atom)
發佈留言
Please leave your name and tell me what you thought about this site. Comments, suggestions and views are welcomed.
如果這篇文章對你有幫助,那請留個訊息給我~