
定義資料
data = Transpose@{{-3.0, -2.5, -2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0}, {187, 108.063, 60.0, 33.0625, 19, 11.0625, 4.0, -5.9375, -21.0, -41.9375, -68, -96.9375, -125, -146.938,-156, -143.938, -101, -15.9375, 124}};
檢視資料散佈圖
ListPlot[data, PlotStyle -> PointSize[Large]]

以最小平方法來估計四次函數
nlm = NonlinearModelFit[data, {a, b, c, d, c}.{1, x, x^2, x^3, x^4}, {a, b, c, d, e}, {x}];
Normal@nlm

將最小平方法擬合資料繪出
Plot[Normal[nlm], {x, -3, 6}, Epilog -> {PointSize[Large], Red, Point@Transpose@{data[[All, 1]], Map[nlm, data[[All, 1]]]}, Blue, Point@data}]

以FindMinimum求擬合曲線最小值
FindMinimum[Normal[nlm], {x, 4}]
{-131.961, {x -> 3.84572}}
以x=5為起始值,畫出迭代過程
Needs["Optimization`UnconstrainedProblems`"];
FindMinimumPlot[nlm[x], {x, -5}][[3]]

以x=5為起始值,輸出將迭代過程
First@Last@Reap[FindMinimum[nlm[x], {x, -3}, StepMonitor :> Sow[{x, nlm[x]}]]] // TableForm

列出ConjugateGradient, PrincipalAxis, Newton, QuasiNewton的迭代過程
Labeled[TableForm[First@Last@Reap[FindMinimum[nlm[x], {x, 5}, StepMonitor :> Sow[{x, nlm[x]}], Method -> #]]], #, Top] & /@ {"ConjugateGradient", "PrincipalAxis", "Newton", "QuasiNewton"} // TableForm

以牛頓法求極小值
min = NestList[# - nlm'[#]/nlm''[#] &, 5, 10]
{5, 4.18259, 3.88676, 3.84644, 3.84573, 3.84572, 3.84572, 3.84572, 3.84572, 3.84572, 3.84572}
又以上可以發現Mathematica在執行FindMinimum時,預設以準牛頓法來進行迭代計算
以黃金切割搜尋法求解
lambda = N@((Sqrt[5] - 1)/2);
First@NestWhile[If[nlm[(1-lambda)*#[[1]]+lambda*#[[2]]]>nlm[lambda*#[[1]]+(1-lambda)*#[[2]]],{#[[1]],(1-lambda)*#[[1]]+lambda*#[[2]]},{lambda*#[[1]]+(1-lambda)*#[[2]],#[[2]]}]&,{2,5},Unequal,All]
3.84573
黃金切割搜尋法收斂圖
test=NestWhileList[If[nlm[(1-lambda)*#[[1]]+lambda*#[[2]]]>nlm[lambda*#[[1]]+(1-lambda)*#[[2]]],{#[[1]],(1-lambda)*#[[1]]+lambda*#[[2]]},{lambda*#[[1]]+(1-lambda)*#[[2]],#[[2]]}]&,{2,5},Unequal,All]
Plot[0,{x,0,6},PlotRange->{All,{0,2}},Epilog->{Thickness[0.01],
Table[{RGBColor[Random[],Random[],Random[]],
Line@{{test[[n,1]],n/10},{test[[n,2]],n/10}}},{n,20}]}]

將最小點標示於函數曲線上做圖說明
Plot[Normal[nlm], {x, -4, 8},
PlotRange -> {{-4, 8}, {-200, 250}}, ImageSize -> 500,
Epilog -> {PointSize[Large], Red,
Point@Transpose@{data[[All, 1]], Map[nlm, data[[All, 1]]]},
Blue, Point@data,
Black, PointSize[0.05],
Point[{Last@min, nlm[Last@min]}]
}]

建立圖例函數
LineStylesFromTooltips[plot_Graphics] := Cases[plot, Tooltip[{s__, l_Line}, tt_] :> Grid[{{Graphics[Flatten[{s, Thickness[0.15], Line[{{0, 0}, {1, 0}}]}], ImageSize -> {24, 8}, AspectRatio -> 8/24, ImagePadding -> 0], tt}}], Infinity];
LineLegendFromTooltips[plot_Graphics] := Labeled[plot, Style[Column[LineStylesFromTooltips[plot], Left], "TR", ShowStringCharacters -> False], {{Right, Top}}];
將插值法及最小平方法的曲線擬合資料繪出
LineLegendFromTooltips[Plot[{Tooltip[Interpolation[data, InterpolationOrder -> 4][x], "插值法"], Tooltip[nlm[x], "最小平方法"]}, {x, -3, 6}, PlotStyle -> Thickness[0.01], Epilog -> {PointSize[Large], Red, Point@Transpose@{data[[All, 1]], Map[nlm, data[[All, 1]]]}, Blue, Point@data, Black, PointSize[0.05], Point[{Last@min, nlm[Last@min]}]}]]

Mathematica教學 mathematica 6教學 mathematica基本教學 mathematica mathematica下載 spss教學講義下載spss教學網站 spss教學pdf spss教學網 spssFacebook 我的餐廳 X幣 免費取得方式 秘技教學講義下載 spss教學網站 spss教學下載 spss教學講義 spss教學pdf roc curve spss教學 manova spss教學 spss教學手冊 spss 16教學 automator mac 教學 automator 浮水 box cox spss cwtex linux cwtex ptt cwtex texlive cwtex-linux excel matrix excel 分配圖 excel 畫常態分配圖 excel常態分配 excel常態分配圖 excel畫常態分佈圖 excel畫常態分配圖 google latex 註解 graphicspath latex 路徑 import mathematica integral exp x 2 latex latex 文字和圖片重疊 latex 浮水印 透明度 watermark latex 圖例 latex 圖片文字重疊 latex教學 mac automator mac latex 新增 package mac pdf 浮水印 mac 水印 mac合併pdf mathematica mathematica 8 圖 字 mathematica abs mathematica pso mathematica 字型 mathematica 教學 pdf mathematica 講義 mathematica教學 mathematica講義 norminv vba p-value excel scientific workplace lyx spss string substr spss 幾何平均數 spss 遺漏值 spss教學講義下載 丁吉峰 可愛手寫字體 可愛字體 可愛的字體 如何畫常態分配圖 利用mac automator幫pdf加入浮水印 怎麼合併pdf for mac automator 怎麼畫星星 浮水印 cwtex 留言板 常態分配圖 常態分配圖如何畫 敏感性分析 公式 統計學講義 最小生成樹 華康可愛字體 戴忠淵
讀者回應 ( 12 意見 )
訂閱發佈留言 (Atom)
那請問一下
此題的黃金切割法呢
要怎麼去寫出來