### Simplify code by using loop ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include #include using namespace std; int main() { float x = { 3,5,8,2 }; float y = { 5,-1.5,0.367,0.2}; float z = { 5,2,3,4 }; int size = sizeof(x) / sizeof(x); cout << "\n***************************" << endl; cout << "Newton's Form: " << endl; cout << y; for (int i = 1; i < size; i++) { cout << showpos << y[i]; for (int j = 0; j < i; j++) { cout << "(x" << showpos << -x[j] << ")"; } } if (size == 2) { cout << "\n\nLagrange Polynomial Form: " << endl; cout << (1 / (x - x)) * (z) << "(x" << -x << ")" << (1 / (x - x)) * (z) << "(x" << -x << ")" << endl; } else if (size == 3) { cout << "\n\nLagrange Polynomial Form: " << endl; cout << (1 / ((x - x) * (x - x)) * (z)) << "(x" << -x << ")" << "(x" << -x << ")" << (1 / ((x - x) * (x - x)) * (z)) << "(x" << -x << ")" << "(x" << -x << ")" << (1 / ((x - x) * (x - x)) * (z)) << "(x" << -x << ")" << "(x" << -x << ")"; } else if (size == 4) { cout << "\n\nLagrange Polynomial Form: " << endl; cout << (1 / ((x - x) * (x - x) * (x - x)) * (z)) << "(x" << -x << ")" << "(x" << -x << ")" << "(x" << -x << ")" << (1 / ((x - x) * (x - x) * (x - x)) * (z)) << "(x" << -x << ")" << "(x" << -x << ")" << "(x" << -x << ")" << (1 / ((x - x) * (x - x) * (x - x)) * (z)) << "(x" << -x << ")" << "(x" << -x << ")" << "(x" << -x << ")" << (1 / ((x - x) * (x - x) * (x - x)) * (z)) << "(x" << -x << ")" << "(x" << -x << ")" << "(x" << -x << ")"; } system("pause"); }``````

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
Last edited on 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. @joinnin how can I do it? Can you help me please? 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. @thanhquan1704,
http://www.cplusplus.com/forum/general/273639/#msg1180471

He/she was using Newton's difference formula to get the interpolating polynomial rather than Lagrange interpolation, but there would be quite a lot of commonality in the approach. @lastchance Can you help me in the Lagrange Polynomial part, too? I'm trying to figure out base on the link you send to me but I can't find out in the Lagrange Polynomial part. @thanhquan1704,
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
(x-x[j])/(x[i]-x[j])
omitting the factor with i=j.

Write the loops to avoid those enormous long formulae. Also, pre-compute the coefficients. @lastchance that's what I'm still sticking. Can you help me in the for loops that void the enormous long formulae, please?

 ``123456789101112`` ``````for (int i = 0; i < size; i++) { int p = 1; for (int j = 0; j < size; j++) { if (i != j) { p = 1/(x[i] - x[j]); } } cout << p << endl; }``````

I'm trying to find the coefficient but it went wrong!
Last edited on You are not creating a product of terms.You are setting a new value of p each time.

Also, your final coefficient for each term needs to be multiplied by y[i] after the inner loop. I really don't know to get out of it. It took me a lot of time, can you help me to write this part, please? :( @thanhquan1704,
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.
Last edited on I did it. Thank you for your help!