Lagrange Approximation for y(x)=cos(π x) Using 5 Points

In summary: The code for doing this is shown in the second block of code. The result of the calculation is shown in the third block of code.
  • #1
ggeo1
63
0
Hello, i have done the following code in c++ which gives me a result but :

1) I compute the product "prod*=(y-x)/(x[k]-x)" which has variable "y" inside ,but the result i take is just a number.(i want the result to be for example in this form "p(x)=1.02*x^3+2*x^2..."
I can't understand how the program manipulates the variable y which does hasn't a value!

2) I don't know if i have it right..

The exercise is :
Find Lagrange's polynomial approximation for y(x)=cos(π x), x ∈−1,1 using 5 points
(x = -1, -0.5, 0, 0.5, and 1).


My code :

Code:
using namespace std;

const double pi=3.1415;

// my function
double f(double x){

return (cos(pi*x));
}

//function to compute lagrange polynomial
double lagrange_polynomial(int N){
//N = degree of polynomial

double *x;//hold the values
x=new double [N+1];

int i,k;
double y;

//these points are given
x[0]=-1.0;
x[1]=-0.5;
x[2]=0;
x[3]=0.5;
x[4]=1.0;

//computations for finding lagrange polynomial
double sum=0;
for (k=0;k<=N;k++){
    double prod=1.0;
        for (i=0;i<=N;i++){
            if (i==k) prod=1.0;
            else if (i!=k){
            prod*=(y-x[i])/(x[k]-x[i]);

                      }
              }
sum+=prod*f(x[k]);
}
return sum;
}

int main()
{

    double deg,result;

    cout <<"Give the degree of the polynomial :"<<endl;
    cin >>deg;

    result=lagrange_polynomial(deg);

    cout <<"The Lagrange approximation is :"<<endl;
    cout <<"p(x) = "<<result;

    return 0;
}
 
Technology news on Phys.org
  • #2
The answer to the first question is that in your code, you are not manipulating the variable y. You are simply calculating the product of a series of terms, each of which involves the variable y. The result of the product will be a single number, but it is still a function of y. To get the polynomial form of the result, you need to expand the product out into the sum of its factors, and then reorganize the terms into powers of y.
 

Related to Lagrange Approximation for y(x)=cos(π x) Using 5 Points

1. What is Lagrange Approximation?

Lagrange Approximation is a mathematical method used to approximate a function using a polynomial. It involves selecting a certain number of points on the function and constructing a polynomial that passes through those points.

2. What is the purpose of using 5 points in Lagrange Approximation for y(x)=cos(π x)?

The purpose of using 5 points is to create a polynomial of degree 4, which is the highest degree that can be achieved with 5 points. This will result in a more accurate approximation of the original function.

3. How is Lagrange Approximation different from other approximation methods?

Lagrange Approximation differs from other methods in that it guarantees the polynomial will pass through the selected points, whereas other methods may only approximate the function in a certain range or at certain points.

4. What is the significance of using y(x)=cos(π x) in Lagrange Approximation?

The significance of using this specific function is that it is a periodic function, meaning it repeats itself at regular intervals. This allows for a more accurate approximation as the polynomial can be tailored to fit the periodic nature of the function.

5. Can Lagrange Approximation be used for any type of function?

Yes, Lagrange Approximation can be used for any type of function. However, the accuracy of the approximation will depend on the number of points selected and the degree of the resulting polynomial.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
816
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
671
  • Programming and Computer Science
Replies
4
Views
716
  • Programming and Computer Science
Replies
22
Views
3K
Back
Top