使用 Mathematica 实现插值多项式算法
我必须在 Mathematica 中实现这个算法:
我的问题是我不太理解 Mathematica 语法,因为有那里没有很多有用的例子。我做了什么:(
(* Input: 4 Points*)
Array[sx, 4, 0];
Array[sy, 4, 0];
sx[0] = -1;
sy[0] = 0;
sx[1] = 0;
sy[1] = 2;
sx[2] = 1;
sy[2] = 4;
sx[3] = 3;
sy[3] = 32;
P[x,0]:=sy[0];
P[x, k_] :=
P[x, k - 1] + (sy[k] - P[sx[k], k - 1])*
Sum[(x - sx[j])/sx[k] - sx[j], {j, 0, x}];
我尝试实现几何平均值,但失败了,因为我什至无法计算总和。)
如何正确实现递归? (几何平均值)
I have to implement this algorithm in Mathematica:
My problem is that I don't really understand the Mathematica syntax because there aren't a lot of useful examples out there. What I have done:
(* Input: 4 Points*)
Array[sx, 4, 0];
Array[sy, 4, 0];
sx[0] = -1;
sy[0] = 0;
sx[1] = 0;
sy[1] = 2;
sx[2] = 1;
sy[2] = 4;
sx[3] = 3;
sy[3] = 32;
P[x,0]:=sy[0];
P[x, k_] :=
P[x, k - 1] + (sy[k] - P[sx[k], k - 1])*
Sum[(x - sx[j])/sx[k] - sx[j], {j, 0, x}];
(I tried to implement the geometric mean but I failed because I can't even calculate the Sum.)
How can I implement the recursion correctly? (an the geometric mean)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以像这样定义一个函数 P :
设置值
sx
和sy
如您上面定义的那样,我们得到:You can define a function P like this :
Setting values
sx
andsy
as you defined above we get :顺便说一句,Mathematica 有一个内置函数
InterpolatingPolynomial
。假设 pts 是您想要查找多项式的点列表,则 p[x, k] 可以写为对于原始帖子中的示例,您将得到
Just as an aside, Mathematica has a built-in function
InterpolatingPolynomial
. Supposepts
is the list of points for which you want to find the polynomial, thenp[x, k]
could be written asFor the example in the original post you would get