0

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): mathematica error 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?

2
  • 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, 2011 at 4:40
  • @Simon Thanks! I forgot about that Dec 7, 2011 at 4:43

1 Answer 1

4

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}]]

enter image description here

3
  • 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. Dec 7, 2011 at 4:42
  • @Matthew: Brain farts like this happen sometimes...
    – Simon
    Dec 7, 2011 at 4:46
  • 1
    Some of us are more gassy than others... (Matthew, I don't mean you.)
    – Mr.Wizard
    Dec 7, 2011 at 6:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.