Solving Gauss-Legendre Algorithm in C++

  • C/C++
  • Thread starter ggeo1
  • Start date
  • Tags
    C++
In summary, the code in the file attached to the message is not working and the compiler gives errors.
  • #1
ggeo1
63
0
Hello , i have the algorithm attached and i am trying to execute it in c++.

My code until now is : ( i have created the function f with limits (-1,1) )
Code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <cmath>

using namespace std;

const double pi=3.14;

double f(double x){
double y;
y=(pi/4.0)*(log((pi*(x+1.0))/4.0 +1.0));
return y;

}

double legendre (int n){

    double *L,*w,*t;
    double x,sum1,sum2,result;
    L=new double [n];
    w=new double [n];
    t=new double [x];


        while(n<10){

         L[0]=1;
         L[1]=x;



        for (int i=1;i<=10;i++){
        L[i+1]=((2.0*i+1.0)*x*L[i] - i*L[i-1])/(i+1.0);


        }

        w=0;
        for (int i=1;i<=10;i++){
        w[i]+=(2.0*(1.0-x*x))/(i*i*(L[i-1]*L[i-1]));
        }
      
        for (int i=1;i<=10;i++){
            sum1=0.0;
            for (int k=1;k<=2*n-1;k+=2){
                sum1+=w[i]*(pow(t[i],k));
            }
                sum1=0;
                sum2=0.0;
                for(int k=0;k<=2*n-2;k+=2){
                    sum2+=w[i]*(pow(t[i],k));
                }
                sum2=2.0/n;
        }

    }

    result=w*f(t);

    return result;

}

int main()
{
    double eps=1e-8;//accuracy
    double exact=0.8565899396;//exact solution for the integral
    double error=1.0;
    double result;

    int n=1;//initial point

    while (fabs(error-exact)>eps) {
        result=legendre(n);
        cout <<"\nFor n = "<<n<<",error = "<<fabs(error-exact)<<",value = "<<result;

    n++;
    }

    return 0;
}

I have the following problems:

(first of all i am not sure if am doing it right..)

1) I am not sure how to connect the whole thing.I am computing L[i+1],w and then sum1 and sum2 but i can't figure how to connect them.
2) In the point i am writing result=w*f(t) ,the compiler gives me an error.I can't insert a pointer (t here) in a function as argument?

Any help is appreciated...
 

Attachments

  • Untitled 1.doc
    34 KB · Views: 201
Last edited:
Technology news on Phys.org
  • #2
For #2: "t" is a pointer to a double, and the function f() wants a double as an argument. So most likely you want to de-reference t: result=w*f(*t);

For #1: I don't know what you mean by "connecting the whole thing", because I'm not familiar with the function you are computing.
 
  • #3
This will crash...

You set n = 1...

You call legendre(n)...

Then inside legendre(int) you say w=new double [n];

In other words n = 1 and w has a length of 1.

But then you iterate i from 1 to 10 and say :
sum1+=w*(pow(t,k));
This will crash or at least badly misbehave unless w has a length of at least 11.

Maybe it would help if you posted the specific algorithm you are trying to implement.
 
  • #4
Hello ,
i have the algorithm attached.

the compiler gives me the following errors:
where i have "t=new double [x];" it gives -> error: expression in new-declarator must have integral or enumeration type|


and where " result=w*f(*t); " ->error: invalid operands of types ‘double*’ and ‘double’ to binary ‘operator*’|

(note :i changed result=w*f(t) to result = w*f(*t)

And as i said , i don't know if i implemented the algorithm right.

Thanks for helping.
 
  • #5
ggeo1 said:
Hello ,
i have the algorithm attached.

the compiler gives me the following errors:
where i have "t=new double [x];" it gives -> error: expression in new-declarator must have integral or enumeration type|
You can't do this, which is what the compiler error message is saying. There are several things wrong here.
1. x is a local variable of type double. When you declare an array, the number must be an integral type (int, long, char, short, etc.) or a value of an enum.
2. Even if you could declare an array with a double or float number of elements, x is an uninitialized local variable.

ggeo1 said:
and where " result=w*f(*t); " ->error: invalid operands of types ‘double*’ and ‘double’ to binary ‘operator*’|

(note :i changed result=w*f(t) to result = w*f(*t)

And as i said , i don't know if i implemented the algorithm right.

Thanks for helping.
 

What is the Gauss-Legendre algorithm?

The Gauss-Legendre algorithm is a numerical method used to approximate the value of a definite integral. It is based on the Gauss-Legendre quadrature rule, which involves breaking down the integral into a sum of simpler integrals and approximating each one using a polynomial.

Why is the Gauss-Legendre algorithm important?

The Gauss-Legendre algorithm is important because it provides a highly accurate approximation of definite integrals, even for functions that are difficult to integrate analytically. It is also efficient and easy to implement in programming languages like C++.

How does the Gauss-Legendre algorithm work?

The algorithm works by first defining a set of points and weights that correspond to the roots of certain polynomials. These points and weights are then used to approximate the integral by evaluating the function at each point and multiplying by the corresponding weight. The results are then summed to get the final approximation.

What are the advantages of using C++ for implementing the Gauss-Legendre algorithm?

C++ is a high-performance language that allows for efficient and accurate computation, making it well-suited for implementing the Gauss-Legendre algorithm. Additionally, C++ has a wide range of libraries and tools that can be utilized to further optimize the algorithm and improve its performance.

Are there any limitations to the Gauss-Legendre algorithm in C++?

While the Gauss-Legendre algorithm is generally accurate and efficient, it may not perform well for certain types of integrals, such as those with singularities or oscillatory behavior. In these cases, alternative numerical methods may be more suitable.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
3K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
1
Views
646
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
2
Replies
39
Views
3K
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
944
  • Programming and Computer Science
Replies
11
Views
1K
Back
Top