I am trying to replicate Lagrange Interpolating Polynomials in Mathematica. The wiki for an explanattion of them can be seen here. This is my code (sorry I don't know how to place mathematica code in SO so I have an image):
My problem is that I want a conditional product. if j = m I just want to skip over that product and evaluate the next one. That's why I put the false evaluation as 1. Any suggestions?
-
To put code into stackoverflow, select the code you want and use the Edit menu to copy as "Plain text". You're best only using plain ascii variables and no subscripts, etc... See how I formatted the code in my answer below. – Simon Dec 7 '11 at 4:40
-
@Simon Thanks! I forgot about that – Matthew Kemnetz Dec 7 '11 at 4:43
Add a comment
|
All built-in functions start with a capital letter. So the conditional is If[...]
not if[...]
. Fix that and everything works fine!
With[{k = 5}, Sum[Product[
If[j != m, (x - x[m])/(x[j] - x[m]), 1], {m, 0, k}], {j, 0, k}]]
-
I was JUST coming back here to say that! I can't believe I was making such a novice mistake. Took me 20 min to find it. Thanks anyway Simon. – Matthew Kemnetz Dec 7 '11 at 4:42
-
-
1