首页 > 解决方案 > 幂级数泰勒逮捕标准问题

问题描述

大家好,我创建了 ac 程序来对抗 taylor taylor 余弦和插值系列,但我有一个问题。问题在于函数余弦(使用泰勒方法计算),因为程序总是给我一个度数 3 或 1 。我的老师说我应该使用这个逮捕标准

x^(2k)/(2k!)<t其中 t 在我的代码中是 tau,即公差。真的感谢任何可以帮助我的人!

#include<stdio.h>
#include<math.h>



double read_tolerance();
double put_x();
void cosine(double,double,int &,double,double);
double Li(int,int,double[],double);
double Pn(int,double[],double[],double);
void interpo_Lagrange(int);


int main()
{
    
    int k=1;
    double cos_initial=1,a=1;
   
    double tau=read_tolerance();
    double x=put_x();   
    cosine (cos_initial,a,k,x,tau);
    interpo_Lagrange(k+1);
return 0;
}
    
double read_tolerance() 
{
    double t;
    
        printf("\n choose the value of tolerance: ");
        scanf("%lf", &t);
    
    return t;
}

double put_x()
{
    double x;
    
    printf("\nput value of  x: ");
    scanf("%lf",&x);
    
    return x;
}

void cosine(double coseno,double a,int &k,double x,double tau)
{
     do {
    a *= pow(-1,k)*((x*x) / (2*k));
    coseno += a;
    k=k+2;
    
}while (a> tau);

printf("value of cosine of %lf is %lf\n",x,coseno);
printf("degree with taylor is ' %d\n",k);
return ;
}

double Li(int i, int n, double x[], double X){
    int j;
    double prod=1;
    for(j=0;j<=n;j++){
        if(j!=i)
            prod=prod*(X-x[j])/(x[i]-x[j]);
    }
        return prod;
}

double Pn(int n, double x[], double y[], double X){
    double sum=0;
    int i;
    for(i=0;i<=n;i++){
        sum=sum+Li(i,n,x,X)*y[i];
    }
    return sum;
}


void interpo_Lagrange(int n)
{
     int i;  
    printf("the number of data-points are %d:\n",n);
      
    
    
    double x[n];
    double y[n];
    printf("Enter the x data-points:\n");
    for(i=1;i<=n;i++){
        x[i]=Pi*i/(8);
        printf("%lf\n",x[i]);
    }
     
    printf("Enter the y data-points:\n");
    for(i=1;i<=n;i++){
    
    y[i]=cos(Pi*i/(8));
        printf("%lf\n",y[i]);
    }
     
    double X;  
    printf("Enter the value of x for which you want the interpolated value of y(x):\n");
    scanf("%lf",&X);
    printf("The interpolated value is %lf",Pn(n,x,y,X));
}


标签: c++taylor-series

解决方案


泰勒级数的实现不正确,这里是正确的

do {
    a *= -((x * x) / ((2 * k - 1) * (2 * k)));
    coseno += a;
    k = k + 1;
} while (fabs(a) > tau);

我希望这能解决你的问题


推荐阅读