MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   Mathematica论坛 (https://www.labfans.com/bbs/forumdisplay.php?f=34)
-   -   谁能帮我找找code的毛病 (https://www.labfans.com/bbs/showthread.php?t=9689)

liu6tot 2009-09-17 07:20

谁能帮我找找code的毛病
 
本人写了一拉格朗日插值的程序,通过输入插值点的数值,完成对函数Ln(x)的模拟,可是发现在Mathematica中的运行结果比真实的情况差别很大,请坛有们帮忙看看。
xi = Input["xi="]
yi = Input["yi="]
n = Length[xi] - 1;
p = Sum[yi[[
i]]*(Product[(x - xi[[i]])/(xi[[i]] - xi[[j]]), {j, 1, i - 1}]*
Product[(x - xi[[i]])/(xi[[i]] - xi[[j]]), {j, i + 1,
n + 1}]), {i, 1, n + 1}];
lag[x_] = Expand[%]

输入数据结果为:
{0.4, 0.5, 0.6, 0.7, 0.8}
{-0.916291,-0.693147,-0.510826,-0.356675,-0.223144}

可是生成的Lagrange多项式和实际的要求差很多:
1.56762 - 1.68125 x - 1.4322 x^2 - 1.63133 x^3 - 2.12625 x^4
比较精确的函数应该为:
-2.6487620000000094` + 6.985559166666734` x -
9.010387500000176` x^2 + 6.786583333333535` x^3 -
2.126250000000086` x^4

请坛子里的各位老大帮帮忙啊?


所有时间均为北京时间。现在的时间是 09:02

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.