我有一个多变量函数,我想要集成在C(二手梯形规则)

时间:2017-06-18 19:35:09

标签: c numerical-integration

我必须与E集成,将它的下限和上限作为我的log10(E)列的第一个值,我打印每个源和上限作为最后一个值同一列。 我的功能是: fo E ^ -spectralindex exp(-tau1) ,这只不过是我在这里定义的correctionDifflux。 [此功能与E]。在这里,我采取了 v '和' w '作为简单的上限和下限,但我在这里进行的集成并未给出正确的值。 我的代码:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
float ebl(float zz, float ee) // Function ebl assigned for returning ta3 
value to be used later
{



float e1, e2, z1, z2, tau_a, tau_b, ta1, ta2, ta3, tau[500], z[500], e[500];
  int i, j, ii, jj;
float tauM[501][500];
FILE *fp;

 fp=fopen("tau_dominguez11_cta.txt", "r"); // Reading a file for creating a 
 matrix which has redshift and energy values

 for(i=0; i<501; i++)
   {
    for(j=0; j<400; j++)
   {
 fscanf(fp, "%f", &tauM[i][j]);

     }

   }
 fclose(fp);

 for(j=0; j<400; j++)
  {
  if(tauM[0][j]<=zz)
   {jj=j;}
 }

for(i=0; i<501; i++)
{

  if(tauM[i][0]<=ee)
  {ii=i;}
 }


 ta1=(zz-tauM[0][jj])*(tauM[ii][jj+1] -tauM[ii][jj])/(tauM[0][jj+1]-tauM[0]
 [jj]) + tauM[ii][jj]; //across zz

  ta2=(zz-tauM[0][jj])*(tauM[ii+1][jj+1] - tauM[ii+1][jj]) / (tauM[0][jj+1] 
 - tauM[0][jj]) + tauM[ii+1][jj]; //across zz

 ta3= (ta2 - ta1)*(ee-tauM[ii][0]) /(tauM[ii+1][0] - tauM[ii][0]) + ta1; 
 //across ee

 return ta3;
 }



float t(float E) // Defining to carry out the integration (help needed in 
this part) 
{
float fo, spectralindex, tau1;
return log10(fo * pow(E, -1*spectralindex) * exp(-1*tau1));
}

int main()
{
 FILE *fp1, *fp2,*files;
 int j,i,k;
 float fo;
 char source;
 float a,spectralindex,flux,E1,E2,E3,E4,x,logE,E,z,red,tau1;
 double diffflux,correctedDiffflux;
 float v=0.1, w=10, N=100,sum=0, h, p;

 E1=0.1, E2=100;
 E3=0.01, E4=10;
 float minLogE=log10(E3), maxlogE=log10(E4);

  int n=100;


fp1=fopen("actualvalues.txt", "r"); // reading flux and spectral index values for 3 sources




 for(j=0; j<3; j++)
{

 char filename[300];
 sprintf(filename,"source%d.txt",j);


  files=fopen(filename,"w");


   fscanf(fp1, "%s %e %f %f\n", &source,  &flux, &spectralindex,&red);

  a=-1*(spectralindex)+1;

  fo=(flux*a)/(pow(E2, a) - pow(E1, a));


   x=(log10(E4/E3))/n;
   logE=minLogE;
  for(i=0; i<n; i++)

   {


    logE=logE+x;

   E=pow(10,logE);



   diffflux=fo*(pow(E,-1*(spectralindex)));

  tau1=ebl(red,E);
   correctedDiffflux=diffflux*exp(-1*tau1);

  h = (w-v) / N;
   p = t(v);

   for(k=1; k<N; k++)
  {
     p=p+2*t(v+k*h);
   }
   p = p+t(w);
   p = p*(h/2);  // p is the integration variable

     fprintf(files,"%f    %f    %f    
      %f\n",log10(E),log10(diffflux),log10(correctedDiffflux),p);

      }
     }
       fclose(fp1);
      fclose(fp2);
    fclose(files);
      }

1 个答案:

答案 0 :(得分:0)

在此功能中,您评论需要一些帮助,

#include <stdio.h>
#include "lib.h"

void (*foo_original)() = NULL;
void foo_patch()
{    
    puts("Before foo!");
    (*foo_original)();
    puts("Before foo!");
}

int main()
{
    foo_original = &foo;
    foo = &foo_patch;

    // Somewhere else in the code
    foo();
}

三个局部变量未初始化。它们与其他函数中的类似变量没有关联。

如果启用编译器警告,则应通知您。

另外,我建议您停止使用error: non-object type 'void ()' is not assignable 支持float t(float E) // Defining to carry out the integration (help needed in this part) { float fo, spectralindex, tau1; return log10(fo * pow(E, -1*spectralindex) * exp(-1*tau1)); } ,除非有充分理由不这样做。二十世纪的教科书?