C++ 中的高斯图例 [英] gauss-legendre in c++

查看:34
本文介绍了C++ 中的高斯图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据以下算法创建 gauss-legendre 代码:

i am trying to create gauss-legendre code according to the following algorithm:

对于 n 个点

也就是说,它创建了一个 2n 方程组(如果我们要求对 2n-1 阶多项式准确,

That is,it is created a 2n equation system (if we demand to be accurate for polynominals of order 2n-1 ,

ti 是 n 阶勒让德多项式的根.勒让德多项式给出:

ti are roots of the legendre polynominals of order n.The legendre poynominals are given :

和wi:

我的代码是:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <cmath>


using namespace std;

const double pi=3.14;


//my function with limits (-1,1)
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 [n];


        while(n<10){

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


        //legendre coef
        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);


        }

        //weights w
        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]));
        }


        //sums  w*t
        for (int i=1;i<=10;i++){
            sum1=0.0; //for k=1,3,5,2n-1
            for (int k=1;k<=2*n-1;k+=2){
                sum1+=w[i]*(pow(t[i],k));
            }
                sum1=0;
                sum2=0.0;//for k=0,2,4,2n-2
                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(result-exact)>eps) {
        result=legendre(n);
        cout <<"\nFor n = "<<n<<",error = "<<fabs(error-exact)<<",value = "<<result;

    n++;
    }

    return 0;
}

我的问题是:

1) 编译器给我 :error: invalidoperands of type ‘double*’ 和 ‘double’ to binary ‘operator*’ --> at result=w*f(*t);

1) The compiler gives me :error: invalid operands of types ‘double*’ and ‘double’ to binary ‘operator*’ --> at result=w*f(*t);

2) 我不确定我是否把整件事都做对了.我的意思是,如果我将所有事情结合在一起并且我是否正确实施了算法.

2) I am not sure if i have done the whole thing right.I mean ,if i combined all the things together and if i implemented right the algorithm.

推荐答案

我不知道算法但你的代码是错误的.
第一:

I do not know the algorithm but your code is wrong.
First :

        while(n<10)
        {
         L[0]=1;
         L[1]=x;
        //legendre coef
        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);
        }

你必须改变n的值(递增、递减等),否则就是死循环.

You must change the value of n (increment, decrement, etc.) otherwise this is an endless loop.

第二:

//weights w
    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]));
    }

w 是一个指针.如果你想重置它,使用 memset(w,0,sizeof(double)*n); 不要让它等于 0.

w is a pointer. If you want to reset it, use memset(w,0,sizeof(double)*n); Do not make it equal to 0.

最后:

result=w*f(*t);

由于您使用 wt 指针作为数组,您必须提供某种索引,例如 result=w[ind] * f(t[ind]);.在这里,您只是将指针 w 的值相乘,而不是 w 指向的值(w 的值是 0 从代码中顺便说一下) 用 t 指向的 double 数组的第一个值.

Since you are using the w and t pointers as arrays, you have to provide some sort of index like result=w[ind] * f(t[ind]);. Here you are simply multiplying the value of pointer w, not the value that is pointed by w (the value of w is 0 form your code by the way) with the first value of the double array pointed by t.

此外,我无法了解您的代码与问题中的公式之间的任何关系.所以我谦虚的建议是不要为此使用 C 或 C++.如果必须,则不要使用指针,因为您似乎不熟悉它们.您可以轻松使用 std::vector 而不是指针.

Also I could not get any relation between your code and the formulas in the question. So my humble advise is do not use C or C++ for this. If you must, then do not use pointers, because it seems you are not familiar with them. You can easily have std::vector instead of pointers.

这篇关于C++ 中的高斯图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆