gpt4 book ai didi

c - C中梯形数值积分的实现

转载 作者:太空宇宙 更新时间:2023-11-04 02:02:38 24 4
gpt4 key购买 nike

我正在尝试使用以下公式使用梯形近似来实现数值积分: formel

我的问题是我不知道如何正确地实现它。为了测试,我写了一个文件,其中 22050 double 值都等于 2,例如:

....................
value =2.0;
for ( index = 0 ; index < 22050;index++){
fwrite(&value,sizeof(double),1,inp2);
}

为了让问题简单化,假设我想要每 100 个样本的整数值:

 X Area                integral value 
0-100 should be 200
100-200 should be 200
..... ...........
22000-22050 should be 100

为此,我编写了一个应该执行此操作的程序,但得到的结果是 4387950 for 100 samples 这是我的代码:

..............................
// opening the files

double* inputData= NULL;
unsigned int N = 100;
double h= 0.0;
unsigned int index= 0;
FILE* inputFile=NULL;
double value =0.0;
int i =0,j=0;


inputFile = fopen("sinusD","rb");
outputFile=fopen("Trapez","wb+");
if( inputFile==NULL || outputFile==NULL){
printf("Couldn't open the files \n");
return -1;
}
inputData = (double*) malloc(sizeof(double)*N);

h=22050/2;
while((i = fread(inputData,sizeof(double),N,inputFile))==N){
value += inputData[0] +inputData[N];
for(index=1;index<N;index++){
value+=(2*inputData[index]);
}
value *=h;
fprintf(outputFile,"%lf ",value);
value =0;

}
if(i!=0){
value = 0;
i=-i;
printf("i value %i\n", i);
fseek(inputFile,i*sizeof(double),SEEK_END);
fread(inputData,sizeof(double),i,inputFile);
for(index=0;index<-i;index++){
printf("index %d\n",index);
value += inputData[0] +inputData[i];
value+=(2*inputData[index]);
}
value *=h;
fprintf(outputFile,"%lf ",value);
value =0;
}
fclose(inputFile);
fclose(outputFile);
free(inputData);
return 0;}

知道怎么做吗?

更新

while((i = fread(inputData,sizeof(double),N,inputFile))==N){
value = (inputData[0] + inputData[N])/2.0;
for(index=1;index<N;index++){
value+=inputData[index];
}
value *=h;
fprintf(outputFile,"%lf ",value);
printf(" value %lf\n",value);
value =0;

}

我得到 199.000 作为每个段的结果。

最佳答案

为什么您不从简单的事情开始。假设您有以下数据 {1,2,3,4,5,6,7,8,9,10} 并假设 h = 1。这个很简单,

#include <stdio.h>

#define SIZE 10

int main()
{
double a[SIZE] = {1,2,3,4,5,6,7,8,9,10}, sum = 0.0, trapz;
int h = 1;
int i = 0;

for ( i; i < SIZE; ++i){
if ( i == 0 || i == SIZE-1 ) // for the first and last elements
sum += a[i]/2;
else
sum += a[i]; // the rest of data
}
trapz = sum*h; // the result

printf("Result: %f \n", trapz);
return 0;
}

这是结果

Result: 49.500000

用 Matlab 仔细检查你的工作:

Y = [1 2 3 4 5 6 7 8 9 10];
Q = trapz(Y)

Q =

49.5000

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$

编辑:对于您在评论中的问题:这是 matlab 代码:

X = 0:pi/100:pi; % --> h = pi/100
Y = sin(X); % get values as many as the size of X
Q = trapz(X,Y);
Q =
1.9998

现在要在 C 中实现相同的场景,请执行以下操作

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

#define SIZE 101

#define PI 3.14159265358

int main()
{
double X[SIZE], Y[SIZE], incr = 0.0, h = PI/100.0, sum = 0.0, trapz;
int i = 0, k = 0, j = 0;

// Generate samples
for ( i; i < SIZE; ++i)
{
X[i] = incr;
incr += h;
}

// Generate the function Y = sin(X)
for ( k; k < SIZE; ++k)
{
Y[k] = sin(X[k]);
}

// Compute the integral of sin(X) using Trapezoidal numerical integration method
for ( j; j < SIZE; ++j){
if ( j == 0 || j == SIZE-1 ) // for the first and last elements
sum += Y[j]/2;
else
sum += Y[j]; // the rest of data
}
trapz = sum * h; // compute the integral

printf("Result: %f \n", trapz);

return 0;
}

结果是

Result: 1.999836

关于c - C中梯形数值积分的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25124569/

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