TransWikia.com

How to put conditions on indices of product

Mathematica Asked by TheStrangeQuark on March 30, 2021

I’m trying to solve for the Lagrangian form of the interpolation polynomial. Right now, I’m just trying to solve for the $l_i(x)$ values which are equal to $l_i(x)=prod_{jneq i, j=1}^{n}{frac{x-x_j}{x_i-x_j}} $. Where $n$ is the length of the list of x values. My code right now is:

LagrangeL[i_, xList_] := 
 Product[(x - xList[j])/(xList[i] - xList[j]) Boole[i != j], {j, 1, 
   Length[xList]}]

This is giving me Indeterminate for LagrangeL[1, {1, 2}] and I’m not sure why. Is there a way to make the condition of $j neq i$ in the product index?

One Answer

Two problems: your xList is a List, so index into it with [[ ]] rather than [ ], and you need to keep the denominator from evaluating.

LagrangeL[i_, xList_] := Product[If[i != j, 
   (x - xList[[j]])/(xList[[i]] - xList[[j]]), 1], {j, 1, Length[xList]}] 

2 - x

Correct answer by bill s on March 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP