Hi everyone. How can I simplify my code of the Lagrange Polynomial by using any other kind of loop instead of using if? Because if the size of an array is bigger, then my code is going to be very very long
there is a pattern to these; from memory (been a while on this topic) its some sort of either summation series or product series, they you should be able to expand in code rather than have a if/else for each one.
I would have to relearn the specifics.... its been over 30 years since I did anything like this.
There are probably better algorithms but how do you solve the thing on paper? Follow those steps, and you should end up expanding something ... that expansion is what you want to code up.
By the time you have got to size=4 you should be able to spot the pattern in Lagrange interpolation.
Loop round, with, on the ith pass and the jth inner loop (counting from 0) including the factor
omitting the factor with i=j.
Write the loops to avoid those enormous long formulae. Also, pre-compute the coefficients.
I have just told you where you are going wrong in constructing the coefficients:
(1) You are setting a new value of p on each pass of the j loop, rather than including it in a product;
(2) After the j loop has finished you still need to multiply by y[i]
And if these coefficients (p) are to be usable they really need to be stored in an array or vector.