c++ - 幂级数泰勒逮捕标准问题
问题描述
大家好,我创建了 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));
}
解决方案
泰勒级数的实现不正确,这里是正确的
do {
a *= -((x * x) / ((2 * k - 1) * (2 * k)));
coseno += a;
k = k + 1;
} while (fabs(a) > tau);
我希望这能解决你的问题
推荐阅读
- javascript - 在 JS 或 Jquery 中单击页面刷新时没有重置时间的启动计时器?
- swift - 在 iOS 的 flutter_inappwebview 中禁用 Cookie
- excel - 下拉菜单保持可视
- php - 计算PHP中另一个数组中一个数组中单词的出现频率
- python - 如何将 csv 文件转换为列表以便进行数字排序
- python - 在 Python 中安装 PyQt5 给出 SyntaxError: invalid syntax
- javascript - 如何在asp mvc的cshtml视图中调用js函数?
- java - 队列大小超出了 Spring Boot 中使用的 ThreadPoolTaskExecutor 中配置的最大队列容量
- python - 将数据库从 sqllite 切换到 postgre django 时出错
- c# - 如何使用默认和自己的策略在 ASP.net Core WebAPI 中启用 CORS