Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement Interpolation Polynomial Algorithm with Mathematica

I have to implement this algorithm in Mathematica:

Algorithm

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)

like image 721
ave4496 Avatar asked Jan 23 '26 23:01

ave4496


1 Answers

You can define a function P like this :

P[x_, 0]  := sy[0] 
P[x_, k_] := P[x, k - 1] + (sy[k] - P[sx[k], k - 1])*
             Product[(x - sx[j])/(sx[k] - sx[j]), {j, 0, k - 1}] // Simplify

Setting values sx and sy as you defined above we get :

In[13]:= P[x, 1] 
Out[13]= 2 (1 + x)

In[14]:= P[x, 2]
Out[14]= 2 (1 + x)

In[15]:= P[x, 3]
Out[15]= 2 + x + x^3
like image 89
Artes Avatar answered Jan 25 '26 13:01

Artes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!