请教为何文件数据输出不对?
新手请教各位前辈。在init_f中试图输出两个向量f and hrhs,这两个向量都随着x的变动而变动,x从-4.5逐渐递增至1.5。两个文件ff1.dat and fhrhs1.dat的第一列均为x,第二列为对应的f and hrhs的值。由定义可知,f恒为零,而hrhs为抛物线曲线形状,但为什么fhrhs1.dat的第二列输出的hrhs的数据不对?请教各位。程序代码:
#include <stdio.h> #include <stdlib.h> #include <math.h> /* FMG Solver of the EHL circular contact */ #define pi 3.1415926535897931 typedef struct { double hx; int ii; double *p, *f; double *hfi, *hrhs; double *w; double *K; double *pjac; /*old pressure for use in Jacobi relaxation*/ double rg; double Hm; } Level; typedef struct { int nx0; int maxlevel; double xa,xb; double h0; /*global constant and work unit*/ Level *Lk; } Stack; void initialize(Stack *U, int nx0, int maxl, double xa, double xb, double h0) { /* initialize values in datastructure */ double hx; Level *L; int i,ii; U->xa=xa; U->xb=xb; U->maxlevel=maxl; U->h0=h0; U->Lk=(Level *)calloc(maxl+1,sizeof(Level)); hx=(xb-xa)/nx0; ii=nx0; for (i=1; i<=maxl; i++) { L=U->Lk+i; L->hx=hx; L->ii=ii; L->p =(double *)calloc(U->maxlevel+1,sizeof(double)); L->w =(double *)calloc(U->maxlevel+1,sizeof(double)); L->f =(double *)calloc(U->maxlevel+1,sizeof(double)); L->pjac =(double *)calloc(U->maxlevel+1,sizeof(double)); L->hfi =(double *)calloc(U->maxlevel+1,sizeof(double)); L->hrhs=(double *)calloc(U->maxlevel+1,sizeof(double)); L->K =(double *)calloc(U->maxlevel+1,sizeof(double)); printf("\n level: %2d ii=%4d, hx=%f",i,ii,hx); hx*=0.5; ii*=2; } } /********* SINGLE GRID ROUTINES *****/ void init_f(Stack *U, int lev) { int i; Level *L; double x; L=U->Lk+lev; for (i=0; i<=L->ii; i++) { x=U->xa+i*L->hx; L->hrhs[i]=0.5*x*x; L->f[i]=0.0; } printf("\n xa and hx are %f and %f",U->xa,L->hx); FILE *ff1,*fhrhs1; ff1=fopen("f1.dat","w"); fhrhs1=fopen("hrhs1.dat","w"); for (i=0; i<=L->ii; i++) { x=U->xa+i*L->hx; fprintf(ff1,"%f %f\n",x,L->f[i]); fprintf(fhrhs1,"%f %f\n",x,L->hrhs[i]); } fprintf (ff1, "\n"); fprintf (fhrhs1, "\n"); fclose(ff1); fclose(fhrhs1); L->rg=-pi/2.0; } /********** MAIN PROGRAM **********/ void main() { Stack U; int i; initialize(&U,128,1,-4.5,1.5,-0.5);//initialize(Stack *U, int nx0, int maxl, double xa, double xb, double h0) init_f(&U,1); }