13 #define TINY 1.0e-25 // A small number. 14 #define FREERETURN {free_vector(d,1,n);free_vector(c,1,n);return;} 28 void polint(
float xa[],
float ya[],
int n,
float x,
float *y,
float *dy)
31 float den,dif,dift,ho,hp,w;
38 if((dift=fabs(x-xa[i])) < dif)
54 if((den=ho-hp) == 0.0)
56 nrerror(
"Error in routine polint");
61 *y += (*dy=(2*ns < (n-m) ? c[ns+1] : d[ns--]));
82 void ratint(
float xa[],
float ya[],
int n,
float x,
float *y,
float *dy)
86 float w,t,hh,h,dd,*c,*d;
118 nrerror(
"Error in routine ratint");
125 *y += (*dy=(2*ns < (n-m) ? c[ns+1] : d[ns--]));
138 void spline(
float x[],
float y[],
int n,
float yp1,
float ypn,
float y2[])
141 float p,qn,sig,un,*u;
148 u[1]=(3.0/(x[2]-x[1]))*((y[2]-y[1])/(x[2]-x[1])-yp1);
153 sig=(x[i]-x[i-1])/(x[i+1]-x[i-1]);
156 u[i]=(y[i+1]-y[i])/(x[i+1]-x[i]) - (y[i]-y[i-1])/(x[i]-x[i-1]);
157 u[i]=(6.0*u[i]/(x[i+1]-x[i-1])-sig*u[i-1])/
p;
164 un=(3.0/(x[n]-x[n-1]))*(ypn-(y[n]-y[n-1])/(x[n]-x[n-1]));
166 y2[n]=(un-qn*u[n-1])/(qn*y2[n-1]+1.0);
168 y2[k]=y2[k]*y2[k+1]+u[k];
183 void splint(
float xa[],
float ya[],
float y2a[],
int n,
float x,
float *y)
185 void nrerror(
char error_text[]);
207 nrerror(
"Bad xa input to routine splint");
210 *y=a*ya[klo]+b*ya[khi]+((a*a*a-a)*y2a[klo]+(b*b*b-b)*y2a[khi])*(h*h)/6.0;
218 float dy,f,x,y,*xa,*ya;
220 printf(
"generation of interpolation tables\n");
221 printf(
" ... sin(x) 0<x<PI\n");
222 printf(
" ... exp(x) 0<x<1 \n");
223 printf(
"how many entries go in these tables?\n");
224 if (scanf(
"%d",&n) == EOF)
return 1;
226 for (nfunc=1;nfunc<=2;nfunc++)
234 printf(
"\nsine function from 0 to PI\n");
240 printf(
"%12.6f %12.6f \n", xa[i],ya[i]);
247 printf(
"\nexponential function from 0 to 1\n");
253 printf(
"%12.6f %12.6f \n", xa[i],ya[i]);
263 printf(
"\n%9s %13s %16s %13s\n",
"x",
"f(x)",
"interpolated",
"error");
288 polint(&xa[n/2],&ya[n/2],5,x,&y,&dy);
289 printf(
"%12.6f %12.6f %12.6f %4s %11f\n", x,f,y,
" ",dy);
295 printf(
"\n***********************************\n");
296 printf(
"press RETURN\n");
312 float dy,f,x,y,*xa,*ya;
314 printf(
"generation of interpolation tables\n");
315 printf(
" ... sin(x) 0<x<PI\n");
316 printf(
" ... exp(x) 0<x<1 \n");
317 printf(
"how many entries go in these tables?\n");
318 if (scanf(
"%d",&n) == EOF)
return 1;
321 for (nfunc=1;nfunc<=2;nfunc++)
325 printf(
"\nsine function from 0 to PI\n");
336 printf(
"\nexponential function from 0 to 1\n");
348 printf(
"\n%9s %13s %16s %13s\n",
"x",
"f(x)",
"interpolated",
"error");
363 printf(
"%12.6f %12.6f %12.6f %4s %11f\n", x,f,y,
" ",dy);
366 printf(
"\n***********************************\n");
367 printf(
"press RETURN\n");
382 int i,n, l = 1, linit;
390 if((nf = fopen(
"In.dat",
"r")) == NULL)
394 fscanf(nf,
"%f%f", &xa[l], &ya[l]);
395 printf(
"%d %12.6f %12.6f \n", l, xa[l],ya[l]);
402 nf = fopen(
"Is.dat",
"w");
406 polint(&xa[i], &ya[i],2,(
float) i+linit,&y,&dy);
407 printf(
"\n%d %12.6f %12.6f ", i, xa[i],ya[i]);
408 printf(
"\n%d %d %12.6f %12.6f", i, i+linit,y,dy);
409 printf(
"\n**************");
411 fprintf(nf,
"%12.6lf %12.6lf %d %12.6lf \n",xa[i],ya[i], i+linit, y);
423 float f,x,y,yp1,ypn,*xa,*ya,*y2;
425 printf(
"generation of interpolation tables\n");
426 printf(
" ... sin(x) 0<x<PI\n");
427 printf(
" ... exp(x) 0<x<1 \n");
428 printf(
"how many entries go in these tables?\n");
429 if (scanf(
"%d",&n) == EOF)
return 1;
433 for (nfunc=1;nfunc<=2;nfunc++)
437 printf(
"\nsine function from 0 to PI\n");
449 printf(
"\nexponential function from 0 to 1\n");
464 spline(xa,ya,n,yp1,ypn,y2);
466 printf(
"\n%9s %13s %16s %13s\n",
"x",
"f(x)",
"interpolated",
"error");
481 printf(
"%12.6f %12.6f %12.6f \n", x,f,y);
484 printf(
"\n***********************************\n");
485 printf(
"press RETURN\n");
500 int i,n,nfunc, linit, l = 1;
506 if((nf = fopen(
"In.dat",
"r")) == NULL)
510 fscanf(nf,
"%lf%lf", &a, &b);
521 if((nf = fopen(
"In.dat",
"r")) == NULL)
527 fscanf(nf,
"%f%f", &xa[i], &ya[i]);
528 printf(
"%d %12.6f %12.6f \n", i, xa[i],ya[i]);
531 printf(
"**********************************************\n");
555 spline(xa,ya,l,yp1,ypn,y2);
558 nf = fopen(
"Is.dat",
"w");
563 splint(&xa[i], &ya[i],&y2[i],3,(
float) i+linit,&y);
566 printf(
"\n%d %12.6f %12.6f %d %d %12.6f ", i, xa[i],ya[i],i, i+linit,y);
570 fprintf(nf,
"%12.6lf %12.6lf %d %12.6lf \n",xa[i],ya[i], i+linit, y);
int testpolintshort(void)
void polint(float xa[], float ya[], int n, float x, float *y, float *dy)
void splint(float xa[], float ya[], float y2a[], int n, float x, float *y)
void spline(float x[], float y[], int n, float yp1, float ypn, float y2[])
void ratint(float xa[], float ya[], int n, float x, float *y, float *dy)
int testpolintlambda(void)