0
$\begingroup$

I have the following table with 90 elements, and I want to write a loop that gives me the Lagrange operators. I want to construct an interpolation with Lagrange interpolation and show the loop to calculate the Lagrange operators.

{{230.5, 622.}, {231.5, 622.}, {230.5, 613.}, {234., 605.}, {235., 594.}, {236., 584.}, {239.5, 574.}, {238., 564.}, {239.5, 549.}, {239.5, 538.}, {244., 531.}, {242.5, 516.}, {236., 510.}, {235., 501.}, {230.5, 491.}, {235., 490.}, {236., 481.}, {238., 469.}, {242.5, 459.}, {245., 449.}, {241.5, 443.}, {247., 433.}, {254., 424.}, {255., 417.}, {264., 408.}, {266., 399.}, {267., 401.}, {266., 395.}, {268.5, 389.}, {278.5, 383.}, {279.5, 370.}, {288.5, 356.}, {297.5, 340.}, {301.5, 331.}, {305., 327.}, {310.5, 324.}, {319.5, 315.}, {325., 306.}, {337.5, 296.}, {345., 292.}, {349.5, 276.}, {353., 258.}, {358.5, 245.}, {359.5, 235.}, {362., 232.}, {364., 224.}, {372., 208.}, {373., 195.}, {386.5, 185.}, {396.5, 183.}, {408.5, 176.}, {418.5, 171.}, {429., 162.}, {440., 156.}, {447.5, 154.}, {459., 144.}, {461., 144.}, {463.5, 140.}, {471., 142.}, {484.5, 134.}, {491., 129.}, {495.5, 123.}, {501., 125.}, {507., 121.}, {517., 110.}, {528., 108.}, {537., 103.}, {543.5, 99.}, {554.5, 97.}, {558., 91.}, {562.5,88.}, {571.5, 87.}, {578., 82.}, {587., 74.}, {597., 68.}, {605., 68.}, {607., 63.}, {611.5, 67.}, {617., 60.}, {622.5, 59.}, {627., 59.}, {640.5, 58.}, {647., 54.}, {650.5, 49.}, {652.5, 49.}, {657., 52.}, {667., 53.}, {676., 51.}, {681.5, 44.}, {683., 40.}};

Thank you

$\endgroup$
17
  • 1
    $\begingroup$ I'm not sure what a "cicle" is. Is there another term? In any case, InterpolatingPolynomial will do it, if used right. Someone asked this same question (maybe different data) a couple weeks ago or so. You can search the site for interpolation and Lagrange. $\endgroup$
    – Michael E2
    Commented May 5, 2020 at 12:43
  • $\begingroup$ I can't use predefined commands. What i want to do is creat i For cicle to calculate the Lagrange opertores like: For i=.... like this because i have 90 points and with a cicle for this will be very easy $\endgroup$
    – idmj
    Commented May 5, 2020 at 12:45
  • 1
    $\begingroup$ Related: mathematica.stackexchange.com/questions/219130/… (A Fold loop is given at the end.) $\endgroup$
    – Michael E2
    Commented May 5, 2020 at 13:04
  • 2
    $\begingroup$ Are you required to write a For loop? It's rarely done in Mathematica as explained here. $\endgroup$
    – Michael E2
    Commented May 5, 2020 at 13:07
  • 1
    $\begingroup$ Then perhaps the function L[i, xj, x] in the linked Q&A will work for you, with xj = data[[All, 1]] being the x-coordinates of the data in your question. Best of luck! (Oh, let me just say that polynomial interpolation of 90 points usually has numerical problems. Also the x-coordinate 230.5 is repeated the data above; I assume it's a typo, otherwise you won't be able to do Lagrange interpolation, which requires distinct x-coordinates.) $\endgroup$
    – Michael E2
    Commented May 5, 2020 at 13:28

1 Answer 1

3
$\begingroup$

You can define a function that does the job (here an older one from me used in lectures 2015), this goes over the Lagrange base polynomials. Nominators and denominators fo the base-polynomials are calculated and used to build ab the interpolation polynomial. So the function delivers all the Lagrange base-polynomials.

lagrangeInterpolation[values_, var_] := 
 Module[{xvalues = values\[Transpose][[1, All]], 
   yvalues = values\[Transpose][[2, All]], 
   subs, 
   complements,
   nominators,
   denominators},
  subs = Subsets[xvalues, {Length@xvalues - 1}];
  complements = Flatten[Complement[xvalues, #] & /@ subs];
  nominators = Times @@ (# - x) & /@ subs;
  denominators = 
   Table[Times @@ (subs[[i]] - complements[[i]]), {i, 1, Length@subs}];
  Reverse[nominators/denominators].yvalues
  ]

then with

xvalues = Range@7

and (just an example)

yvalues = {3, 6, 5, 7, 4, 3, 3}

data = {xvalues, yvalues}\[Transpose]

then

lagrangeInterpolation[data, x]

results in

1/240 (1-x) (2-x) (3-x) (4-x) (5-x) (6-x)+1/12 (1-x) (2-x) (3-x) (4-x) (7-x) (6-x)-7/36 (1-x) (2-x) (3-x) (5-x) (7-x) (6-x)+5/48 (1-x) (2-x) (4-x) (5-x) (7-x) (6-x)-1/20 (1-x) (3-x) (4-x) (5-x) (7-x) (6-x)+1/240 (2-x) (3-x) (4-x) (5-x) (7-x) (6-x)-1/40 (1-x) (2-x) (3-x) (4-x) (5-x) (7-x)

and

p = Expand @ lagrangeInterpolation[data, x] 

delivers:

-109 + (7543 x)/30 - (74401 x^2)/360 + (3973 x^3)/48 - (
 2485 x^4)/144 + (431 x^5)/240 - (53 x^6)/720
$\endgroup$
2

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