Simplify code by using loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <vector>
using namespace std;
int main()
{
	
	float x[4] = { 3,5,8,2 };
    float y[4] = { 5,-1.5,0.367,0.2};
	float z[4] = { 5,2,3,4 };
    int size = sizeof(x) / sizeof(x[0]);
    cout << "\n***************************" << endl;
    cout << "Newton's Form: " << endl;
    cout << y[0];
    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[0] - x[1])) * (z[0]) << "(x" << -x[1] << ")"
            << (1 / (x[1] - x[0])) * (z[1]) << "(x" << -x[0] << ")" << endl;
    }
    else if (size == 3)
    {
        cout << "\n\nLagrange Polynomial Form: " << endl;
        cout << (1 / ((x[0] - x[1]) * (x[0] - x[2])) * (z[0])) << "(x" << -x[1] << ")" << "(x" << -x[2] << ")"
            << (1 / ((x[1] - x[0]) * (x[1] - x[2])) * (z[1])) << "(x" << -x[0] << ")" << "(x" << -x[2] << ")"
            << (1 / ((x[2] - x[0]) * (x[2] - x[1])) * (z[2])) << "(x" << -x[2] << ")" << "(x" << -x[0] << ")";

    }
    else if (size == 4)
    {
        cout << "\n\nLagrange Polynomial Form: " << endl;
        cout << (1 / ((x[0] - x[1]) * (x[0] - x[2]) * (x[0] - x[3])) * (z[0])) << "(x" << -x[1] << ")" << "(x" << -x[2] << ")" << "(x" << -x[3] << ")"
            << (1 / ((x[1] - x[0]) * (x[1] - x[2]) * (x[1] - x[3])) * (z[1])) << "(x" << -x[0] << ")" << "(x" << -x[2] << ")" << "(x" << -x[3] << ")"
            << (1 / ((x[2] - x[0]) * (x[2] - x[1]) * (x[2] - x[3])) * (z[2])) << "(x" << -x[0] << ")" << "(x" << -x[1] << ")" << "(x" << -x[3] << ")"
            << (1 / ((x[3] - x[0]) * (x[3] - x[1]) * (x[3] - x[2])) * (z[3])) << "(x" << -x[0] << ")" << "(x" << -x[1] << ")" << "(x" << -x[2] << ")";

    }
    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,
You might like to peruse your mate @allennguyen04's recent thread here:
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?

1
2
3
4
5
6
7
8
9
10
11
12
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!
Topic archived. No new replies allowed.