找回密码
 欢迎注册
查看: 886|回复: 20

[讨论] 二元牛顿迭代法,下面哪个是正确的?还是都正确?

[复制链接]
发表于 2024-7-25 09:12:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?欢迎注册

×
本帖最后由 nyy 于 2024-7-25 09:36 编辑

第一种迭代格式见:
https://zhuanlan.zhihu.com/p/580580125

第二种迭代格式见
https://www.docin.com/p-1445113743.html

多元函数的泰勒展开矩阵见
https://blog.csdn.net/red_stone1/article/details/70260070


QQ截图20240725090921.jpg

QQ截图20240725090946.jpg





我似乎感觉两个办法都是正确的!
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2024-7-25 09:22:44 | 显示全部楼层
我看了感觉两种办法都是正确的,但是第一种迭代,似乎更漂亮一些,
第二种迭代比较丑,
但是感觉他们的推理没问题!

那究竟是哪个好呢?
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2024-7-25 09:47:19 | 显示全部楼层
13.5 Solving Two General Equations in Two Variables
One nice feature of Newton's Method (and of Poor Man's Newton as well) is that it can easily be generalized to two or even three dimensions.

That is, suppose we have two standard functions, f and g of two variables, x and y. Each of the equations, f(x, y) = 0, and g(x, y) = 0 will typically be satisfied on curves, just as similar linear equations are satisfied on straight lines. And suppose we seek simultaneous solutions to both equations, which will then be at the intersections, if any, of these curves.

If we could solve either of the equations for x say, in terms of y, we could find a parametric representations of the curve solutions to it (with y as parameter), and use the divide and conquer method of the last section on the other function, cutting the parameter interval in half at each step as in one dimension.

This is a slow and steady method that can be implemented fairly easily. But it assumes we can obtain a parametric representation of one or the other curve.

We can always try Newton's method, which is fairly easy to implement in general.

https://ocw.mit.edu/ans7870/18/1 ... er13/section05.html

这儿也是方法2
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2024-7-25 09:54:38 | 显示全部楼层
表达形式不同而已,矩阵求逆表达式代入应该可以写成同样形式

点评

nyy
你是对的,同一个东西,然后前者的马甲漂亮,后者是脱了马甲  发表于 2024-7-25 11:15
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2024-7-25 10:02:46 | 显示全部楼层
mathe 发表于 2024-7-25 09:54
表达形式不同而已,矩阵求逆表达式代入应该可以写成同样形式

我感觉不是一种表达式呀,你仔细代入了吗?我现在被这个问题困惑了
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2024-7-25 10:58:37 | 显示全部楼层
mathe 发表于 2024-7-25 09:54
表达形式不同而已,矩阵求逆表达式代入应该可以写成同样形式

你是对的!这两个表达式是完全一样的!
我把第一个逆矩阵理解成了,先求行列式,然后再求倒数!
然后我自己码代码,怎么搞都不收敛!丢人丢大了!

应该理解成先求矩阵,然后对矩阵求逆,然后再矩阵乘法

(fx,fy;gx,gy)的逆矩阵是(gy,-fy;-gx,fx)/(fx*gy-gx*fy)我的记忆办法是主颠倒,副变号(主对角线调换一下,副对角线变号,再除以行列式的值)
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2024-7-25 11:06:31 | 显示全部楼层
mathe 发表于 2024-7-25 09:54
表达形式不同而已,矩阵求逆表达式代入应该可以写成同样形式
  1. Clear["Global`*"];
  2. f=x^2-10*x+y^2+8
  3. g=x*y^2+x-10*y+8
  4. jacob=D[{f,g},{{x,y}}]//Simplify (*雅可比矩阵*)
  5. next={x,y}-Inverse[jacob].{f,g} (*雅克比矩阵的逆矩阵*)
  6. {x0,y0}={3.0,3.0}(*初始值*)
  7. FindRoot[{f,g},{{x,x0},{y,y0}}](*软件自身的求解程序求解结果*)
  8. Do[ {x0,y0}=N[(next/.{x->x0,y->y0}),20];
  9.     Print[N[{x0,y0},20]]
  10. ,{k,1,15}]
复制代码


上我自己写的代码。
  1. next={x,y}-Inverse[jacob].{f,g}
复制代码

这行我原本写成了
  1. next={x,y}-{f,g}/Det[jacob]
复制代码

结果迭代了几千次,都不收敛,一般牛顿迭代法搞个十次左右就能收敛了。
这明显收敛,那肯定是错误的!
即使我再傻,我也相信代码肯定有问题!
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2024-7-25 13:10:35 | 显示全部楼层
本帖最后由 Jack315 于 2024-7-25 13:13 编辑

修改了一下代码:
  1. Clear["Global`*"];
  2. precision = 20;
  3. p = precision + 1;
  4. f = x^2 - 10*x + y^2 + 8;
  5. g = x*y^2 + x - 10*y + 8;
  6. {x0, y0} = SetPrecision[{3.0, 3.0}, precision];(*初始值*)
  7. FindRoot[{f, g}, {{x, x0}, {y, y0}}, WorkingPrecision -> precision](*软件自身的求解程序求解结果*)
  8. jacob = D[{f, g}, {{x, y}}] // Simplify;(*雅可比矩阵*)
  9. jacob // MatrixForm
  10. next = {x, y} - Inverse[jacob].{f, g}; (*雅克比矩阵的逆矩阵*)
  11. {err, err0, iter, maxiter} = {1, 10^-precision, 0, 10};
  12. While[err > err0,
  13. iter++;
  14. {x1, y1} = SetPrecision[(next /. {x -> x0, y -> y0}), 20];
  15. xerr = SetPrecision[Abs[x1 - x0], p];
  16. yerr = SetPrecision[Abs[y1 - y0], p];
  17. err = Max[xerr, yerr];
  18. Print[{iter, err, x1, y1}];
  19. If[iter > maxiter - 1,
  20.   Print["超出最大允许的迭代次数,迭代异常终止!"];
  21.   Break[]];
  22. {x0, y0} = {x1, y1}
  23. ]
复制代码
迭代 6 次结束:
x=2.1934394154153081387
y=3.0204664681230335884

7# 的代码也得出正确结果,迭代不应该很多次后还不收敛的。
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2024-7-25 13:28:25 | 显示全部楼层
用下列命令作图可以看到一共有 2 个解:
  1. Plot3D[{0, f, g}, {x, 0, 4}, {y, 0, 4}]
复制代码
将起始点设为 (-1,-1),迭代 7 次得到结果:x=1, y=1。

将起始点设为 (-3,-3),迭代 11 次得到结果:x=1, y=1,
但 FindRoot 报告迭代 100 次后仍未收敛。

点评

可能是选项没设置正确,在这种情况下就跑偏了……不深究了。  发表于 2024-7-31 16:12
nyy
奇葩,你发现了软件的一个bug  发表于 2024-7-31 10:02
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2024-7-25 15:34:23 | 显示全部楼层
Jack315 发表于 2024-7-25 13:28
用下列命令作图可以看到一共有 2 个解:
将起始点设为 (-1,-1),迭代 7 次得到结果:x=1, y=1。

你的意思是说mathematica的findroot函数bug
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
您需要登录后才可以回帖 登录 | 欢迎注册

本版积分规则

小黑屋|手机版|数学研发网 ( 苏ICP备07505100号 )

GMT+8, 2024-10-18 10:44 , Processed in 0.031552 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表