| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 435 人关注过本帖
标题:请教为何文件数据输出不对?
取消只看楼主 加入收藏
huaijuliu
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2009-9-17
收藏
 问题点数:0 回复次数:1 
请教为何文件数据输出不对?
新手请教各位前辈。在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);
}



搜索更多相关主题的帖子: 抛物线 
2011-06-19 19:31
huaijuliu
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2009-9-17
收藏
得分:0 
问题解决 感谢关注  分配内存时候出错 以下的maxlevel改为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));
2011-06-20 02:03
快速回复:请教为何文件数据输出不对?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019471 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved