Solving Gauss-Legendre Algorithm in C++

  • Context: C/C++ 
  • Thread starter Thread starter ggeo1
  • Start date Start date
  • Tags Tags
    C++
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 5K views
ggeo1
Messages
61
Reaction score
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

Last edited:
Physics news on Phys.org
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.
 
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.
 
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.
 
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.