如果你构建并运行代码,你会发现它不能解决书中的问题(问题的解决和C语言的程序设计).It给出了2个方程,并希望在curve.And下寻找近似区域添加调用陷阱,n的值为2,4,8,16,32,64,128。
我的代码的输出是负数和-nan。方程式包括:
g(x) = x^2sinx (a = 0, b = 3.14159)
h(x) = sqrt(4-pow(x.2)) ( a =-2, b=2);代码是:
#include <stdio.h>
#include <math.h>
void trap(double a,double b, int n, double *areag, double *areah);
double g(double x);
double h(double x);
int main(void)
{
double areag = 0.0, areah = 0.0;
double a1 = 0, b1 = 10;
int n;
for(n=2;n<=128;n*=2){
trap(a1, b1, n, &areag, &areah);
printf("%f %f\n", areag, areah);
}
return(0);
}
double g(double x){
return(pow(x,2)*sin(x));
}
double h(double x){
return(sqrt(4-pow(x,2)));
}
void trap(double a,double b, int n, double *areag, double *areah){
int i, l;
*areag = (b-a)/2*n * ( g(a) + g(b));
for(i = 1; i<=n-1;i++)
*areag += 2*g(i);
*areah = (b-a)/2*n * ( h(a) + h(b));
for(l=1;l<=n-1;l++)
*areah += 2*h(i);
}发布于 2011-04-24 02:48:24
我不确定它的目的是什么,因为你没有解释它是如何工作的,但这部分是负数的平方根:
sqrt(4-pow(x,2))啊,现在我明白了,这就是你想要集成的函数。问题是,您需要将集成范围划分为小块,而不是在更大范围内集成。试一试
*areah += 2*h( (double) i / n);https://stackoverflow.com/questions/5766161
复制相似问题