C/C++ Solving Gauss-Legendre Algorithm in C++

  • Thread starter Thread starter ggeo1
  • Start date Start date
  • Tags Tags
    C++
AI Thread Summary
The discussion focuses on troubleshooting a C++ implementation of the Gauss-Legendre algorithm. Key issues include improper array initialization, where a double variable is incorrectly used to define the size of an array, leading to compilation errors. Additionally, there is confusion regarding the connection between computed values, specifically how to integrate the arrays for weights and nodes. The user is advised to dereference the pointer correctly when passing it to the function, as the current implementation leads to type mismatch errors. Overall, the user seeks clarification on algorithm implementation and proper C++ syntax to resolve these issues.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
5
Views
3K
Replies
35
Views
4K
Replies
39
Views
4K
Replies
22
Views
3K
Replies
17
Views
2K
Replies
1
Views
1K
Back
Top