Solving Gauss-Legendre Algorithm in C++

  • Context: C/C++ 
  • Thread starter Thread starter ggeo1
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around the implementation of the Gauss-Legendre algorithm in C++. Participants are addressing issues related to the code structure, variable initialization, and function arguments within the context of numerical integration.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant expresses uncertainty about how to connect various components of the algorithm, specifically regarding the computation of L[i+1], w[i], sum1, and sum2.
  • Another participant points out that the variable "t" is a pointer to a double, while the function f() expects a double, suggesting the need to dereference t.
  • A participant warns that the code will crash due to the allocation of the array w with a length of n (which is 1), while iterating beyond that length in the loop.
  • Multiple participants highlight errors related to the declaration of the array t, noting that it cannot be initialized with a double variable and that x is uninitialized.
  • Concerns are raised about the expression "result=w*f(*t)" where the types of operands are incompatible, indicating a misunderstanding of how to use the variable w.

Areas of Agreement / Disagreement

Participants generally agree on the presence of errors in the code, but there is no consensus on the overall implementation of the algorithm or the specific corrections needed.

Contextual Notes

Limitations include uninitialized variables, incorrect array declarations, and potential out-of-bounds access in loops. The specific algorithm being implemented is not fully detailed, which may contribute to the confusion.

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:
Technology 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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 39 ·
2
Replies
39
Views
5K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
2K