0

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

MarcoB
  • 67,153
  • 18
  • 91
  • 189
idmj
  • 11
  • 5
  • 1
    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. – Michael E2 May 05 '20 at 12:43
  • 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 – idmj May 05 '20 at 12:45
  • Like For... If... a program like that i call it a cicle – idmj May 05 '20 at 12:47
  • Don't understand... sorry what is that link? – idmj May 05 '20 at 12:48
  • I believe that's called a "loop" in English CS jargon, as in a "for-loop" or "while-loop" and so forth. – Michael E2 May 05 '20 at 12:49
  • Yes! A loop sorry. It is that i need to do to calculate the lagrange operators and put them in a lagrange interpolation – idmj May 05 '20 at 12:50
  • the last link is to my publication – idmj May 05 '20 at 12:59
  • sure, thank you for your help i'm worried about this... – idmj May 05 '20 at 13:01
  • @idmj please also remember to show some own attempts. At the moment the question sounds a bit like "please write a lagrange interpolation algorithm for me". – Max1 May 05 '20 at 13:02
  • i never used a loop for, so i don't know how to do. I don't want that someone do it, just explain to me if it is possible and normally how creat a for loop – idmj May 05 '20 at 13:04
  • 1
    Related: https://mathematica.stackexchange.com/questions/219130/plotting-basis-order-function/219159#219159 (A Fold loop is given at the end.) – Michael E2 May 05 '20 at 13:04
  • Thank you very much, i will see it with atenction!! Thank you!! – idmj May 05 '20 at 13:05
  • 2
    Are you required to write a For loop? It's rarely done in Mathematica as explained here. – Michael E2 May 05 '20 at 13:07
  • What i need is a lagrange interpolation but with a loop because i know that for 2/3 points i can do this L0[x_] := ((x - x1)(x - x2))/((x0 - x1)(x0 - x2)) to find lagrange operators for lagrange interpolation but for 90 point i need a loop – idmj May 05 '20 at 13:10
  • @idmj If you open the mathematica help (f1 key) and search for For you will find some examples which might be helpful. In general the mathemtica help is a great place to learn about mathematica features. – Max1 May 05 '20 at 13:12
  • 1
    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.) – Michael E2 May 05 '20 at 13:28
  • i can make the interpolation excluding points with the same x no? I will make what i say, can i send it after to you to see? thank you – idmj May 05 '20 at 13:55

1 Answers1

3

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
mgamer
  • 5,593
  • 18
  • 26