- #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) )
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...
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: