- 注册时间
- 2015-8-20
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 1429
- 在线时间
- 小时
|
发表于 2023-9-11 07:11:21
|
显示全部楼层
【拟合迭代过程】
函数定义:
- f0[x_] := Hypergeometric2F1[-(1/2), -(1/2), 1, x^2]
- f1[x_, {a_, b_, c_}] := a + b x + c x^2
复制代码
初次拟合:
- f1Param = Solve[Table[
- Table[SeriesCoefficient[f0[x], {x, 0.5, n}], {n, 0, 2}] ==
- Table[SeriesCoefficient[f1[x, {a, b, c}], {x, 0.5, n}], {n, 0, 2}]
- , {i, 3}], {a, b, c}][[1]]
复制代码
修正拟合使定义域两端误差为零:
- f1k = ((f0[1] - f0[0])/(f1[1, {a, b, c}] - f1[0, {a, b, c}])) /. f1Param
- f1b = Mean[{f0[0] - f1k f1[0, {a, b, c}], f0[0] - f1k f1[0, {a, b, c}]}] /. f1Param
- f1[x_] := Table[x^(i - 1), {i, 3}].Table[f1Param[[i]][[2]], {i, 3}] f1k + f1b //Simplify
复制代码
误差函数:
- err1[x_] := f0[x] - f1[x]
- Plot[err1[x], {x, 0, 1},
- PlotStyle -> Red,
- Filling -> Axis,
- GridLines -> Automatic,
- Frame -> True,
- PlotLabel -> "f1(x) 的拟合误差"]
复制代码
误差函数的三个零点:
- sol = FindRoot[err1[x], {x, 0.5}]
- x0s = {0, sol[[1]][[2]], 1}
- Plot[err1[x], {x, 0, 1},
- PlotStyle -> Red,
- Filling -> Axis,
- Epilog -> {PointSize[0.03], Table[Point[{x0s[[i]], 0}], {i, Length[x0s]}]},
- GridLines -> Automatic,
- Frame -> True,
- PlotLabel -> "误差函数 err1(x) 的零点"]
复制代码
零点间的极值点:
- xms = {x /. FindRoot[D[err1[x], x], {x, Mean[x0s[[1 ;; 2]]]}],
- x /. FindRoot[D[err1[x], x], {x, Mean[x0s[[2 ;; 3]]]}]}
- yms = err1[x] /. x -> xms
- Plot[err1[x], {x, 0, 1},
- PlotStyle -> Red,
- Filling -> Axis,
- Epilog -> {PointSize[0.03],
- Table[Point[{xms[[i]], yms[[i]]}], {i, Length[xms]}]},
- GridLines -> Automatic,
- Frame -> True,
- PlotLabel -> "误差函数 err1(x) 的极值点"]
复制代码
拟合左侧的峰:
- f2[x_, {a_, b_, c_}] := a + b x + c x^2
- f2Param = Solve[Table[
- Table[SeriesCoefficient[err1[x], {x, xms[[1]], n}], {n, 0, 2}] ==
- Table[SeriesCoefficient[f2[x, {a, b, c}], {x, xms[[1]], n}], {n, 0, 2}]
- , {i, 3}], {a, b, c}][[1]]
- Plot[{err1[x], f2[x, {a, b, c}] /. f2Param}, {x, 0, 1},
- Filling -> {1 -> {2}},
- GridLines -> Automatic,
- PlotLegends -> "Expressions",
- Frame -> True,
- PlotLabel -> "拟合函数 f2(x)"]
复制代码
窗函数:
- fwin[x_, {m_, s_}] := E^-((x - m)/s)^2
- (*计算窗函数参数 s 的公式,w 是相邻零点的宽度*)
- winS[w_] := w/3
复制代码
接下来是通过加窗函数逐个拟合误差函数的极值点……
代码改乱了,上个图看看加窗拟合的效果:
|
|