gpt4 book ai didi

c - 需要麦克劳林级数和泰勒级数的编程帮助吗?

转载 作者:行者123 更新时间:2023-12-04 06:52:28 25 4
gpt4 key购买 nike

好的,这是我到目前为止所拥有的:

#include <stdio.h>
#include <math.h>
//#define PI 3.14159

int factorial(int n){
if(n <= 1)
return(1);
else
return(n * factorial(n-1));
}

void McLaurin(float pi){
int factorial(int);
float x = 42*pi/180;
int i, val=0, sign;

for(i=1, sign=-1; i<11; i+=2){
sign *= -1; // alternate sign of cos(0) which is 1
val += (sign*(pow(x, i)) / factorial(i));
}

printf("\nMcLaurin of 42 = %d\n", val);
}

void Taylor(float pi){
int factorial(int);
float x;
int i;
float val=0.00, sign;
float a = pi/3;

printf("Enter x in degrees:\n");
scanf("%f", &x);
x=x*pi/180.0;
printf("%f",x);

for(i=0, sign=-1.0; i<2; i++){
if(i%2==1)
sign *= -1.0; // alternate sign of cos(0) which is 1
printf("%f",sign);
if(i%2==1)
val += (sign*sin(a)*(pow(x-a, i)) / factorial(i));
else
val += (sign*cos(a)*(pow(x-a, i)) / factorial(i));
printf("%d",factorial(i));
}

printf("\nTaylor of sin(%g degrees) = %d\n", (x*180.0)/pi, val);
}

main(){
float pi=3.14159;
void McLaurin(float);
void Taylor(float);

McLaurin(pi);
Taylor(pi);
}

这是输出:
McLaurin of 42 = 0
Enter x in degrees:
42
0.733038-1.00000011.0000001
Taylor of sin(42 degrees) = -1073741824

我怀疑这些离谱数字的原因与我混淆了浮点数和整数的事实有关?但我就是想不通……!!也许它是一个数学问题,但它从来都不是我的强项,更不用说用微积分编程了。麦克劳林也失败了,它怎么等于零?卧槽!请帮助纠正我的菜鸟代码。我还是初学者。。。

- - 更新 - -
#include <stdio.h>
#include <math.h>
//#define PI 3.14159

int factorial(int n){
if(n <= 1)
return(1);
else
return(n * factorial(n-1));
}

void McLaurin(float pi){
int factorial(int);
float x = 42*pi/180, val=0;
int i, sign;

for(i=1, sign=-1; i<11; i+=2){
sign *= -1; // alternate sign of cos(0) which is 1
val += (sign*(pow(x, i)) / factorial(i));
}

printf("\nMcLaurin of of sin(%f degrees) = %f\n", (x*180.0)/pi, val);
}

void Taylor(float pi){
int factorial(int);
float x;
int i;
float val=0, sign;
float a = pi/3;

printf("Enter x in degrees:\n");
scanf("%f", &x);
x=x*pi/180.0;
printf("%f",x);

for(i=0, sign=-1.0; i<2; i++){
if(i%2==0)
sign *= -1; // alternate sign of cos(0) which is 1
printf("%f",sign);
if(i%2==0)
val += (sign*sin(a)*(pow(x-a, i)) / factorial(i));
else
val += (sign*cos(a)*(pow(x-a, i)) / factorial(i));
printf("%d",factorial(i));
}

printf("\nTaylor of sin(%f degrees) = %f\n", (x*180.0)/pi, val);
}

main(){
float pi=3.14159;
void McLaurin(float);
void Taylor(float);

McLaurin(pi);
Taylor(pi);
}

给我奇怪的错误。
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: cannot open output file a.exe: Device or resource busy
collect2: ld returned 1 exit status
Can anyone explain what's wrong now?

最佳答案

一些潜在的错误来源:

  • 你的 pi 值没有足够的数字来保证浮点精度;
  • 在您的 Maclaurin 函数中,您将 val 作为一个整数,对其进行除法运算;
  • 在 Taylor 中,您只使用系列中的 2 个术语

  • 我认为 ?

    关于c - 需要麦克劳林级数和泰勒级数的编程帮助吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2923811/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com