回复 2楼 A13433758072
#include "stdlib.h"
#include "math.h"
#include "stdio.h"
void rkt1(t,y,n,h,k,z,f)
void (*f)();
int n,k;
double t,h,y[],z[];
{
int i,j,l;
double a[4],tt,*b,*d;
b=malloc(n*sizeof(double));
d=malloc(n*sizeof(double));
a[0]=h/2.0;a[1]=a[0];
a[2]=h;a[3]=h;
for (i=0;i<=n-1;i++) z[i*k]=y[i];
for (l=1;l<=k-1;l++)
{
(*f)(t,y,n,d);
for(i=0;i<=n-1;i++) b[i]=y[i];
for(j=0;j<=2;j++)
{
for(i=0;i<=n-1;i++)
{
y[i]=z[i*k+l-1]+a[j]*d[i];
b[i]=b[i]+a[j+1]*d[i]/3.0;
}
tt=t+a[j];
(*f)(tt,y,n,d) ;
}
for (i=0;i<=n-1;i++)
z[i*k+l]=y[i];
t=t+h;
}
free(b);free(d);
}
void rkt1f(t,y,n,d)
int n;
double t,y[],d[];
{
t=t;n=n;
d[0]=y[1];
d[1]=-y[0]+2*y[1]+t*exp(t)-t;
}
main()
{
int i,j;
void rktlf(double,double[],int,double[]);
double t,h,y[2],z[2][101];
FILE * fp,*fp2;
fp=fopen("e1.txt","w");
y[0]=0.0;y[1]=0.0;
t=0.0;h=0.1;
rkt1(t,y,2,h,11,z,rkt1f);
printf ("\n");
for (i=0;i<=10;i++)
{
t=i*h;
printf("t=%5.2f\n",t);
j=0;
printf ("y(%d)=%0.7f\t",i,z[j][i]);
printf("\n");
fprintf(fp,"%10f
%10f\n",t,z[j][i]);
}
printf("finish");
fclose(fp);
getch();
}麻烦再帮看下