DAS  3.1.6 - 18/09/2017
Macros | Functions
Interpolation.c File Reference
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include "Marq.h"
#include "nrutil.h"
+ Include dependency graph for Interpolation.c:

Go to the source code of this file.

Macros

#define FREERETURN   {free_vector(d,1,n);free_vector(c,1,n);return;}
 
#define PI   3.1415926
 
#define TINY   1.0e-25
 

Functions

void polint (float xa[], float ya[], int n, float x, float *y, float *dy)
 
void ratint (float xa[], float ya[], int n, float x, float *y, float *dy)
 
void spline (float x[], float y[], int n, float yp1, float ypn, float y2[])
 
void splint (float xa[], float ya[], float y2a[], int n, float x, float *y)
 
int testpolintfull (void)
 
int testpolintlambda (void)
 
int testpolintshort (void)
 
int testsplint (void)
 
int testsplintrc (void)
 

Macro Definition Documentation

§ FREERETURN

#define FREERETURN   {free_vector(d,1,n);free_vector(c,1,n);return;}

Definition at line 14 of file Interpolation.c.

Referenced by ratint().

§ PI

#define PI   3.1415926

Definition at line 15 of file Interpolation.c.

Referenced by testpolintfull(), testpolintshort(), and testsplint().

§ TINY

#define TINY   1.0e-25

Definition at line 13 of file Interpolation.c.

Referenced by ratint().

Function Documentation

§ polint()

void polint ( float  xa[],
float  ya[],
int  n,
float  x,
float *  y,
float *  dy 
)

Definition at line 28 of file Interpolation.c.

References free_vector(), nrerror(), and vector().

Referenced by testpolintfull(), testpolintlambda(), and testpolintshort().

29 {
30  int i,m,ns=1;
31  float den,dif,dift,ho,hp,w;
32  float *c,*d;
33  dif=fabs(x-xa[1]);
34  c=vector(1,n);
35  d=vector(1,n);
36  for (i=1;i<=n;i++)
37  { //Here we .nd the index ns of the closest table entry,
38  if((dift=fabs(x-xa[i])) < dif)
39  {
40  ns=i;
41  dif=dift;
42  }
43  c[i]=ya[i]; //and initialize the tableau of c’s and d’s.
44  d[i]=ya[i];
45  }
46  *y=ya[ns--]; //This is the initial approximation to y.
47  for (m=1;m<n;m++)
48  { //For each column of the tableau,
49  for(i=1;i<=n-m;i++)
50  { //we loop over the current c’s and d’s and update them.
51  ho=xa[i]-x;
52  hp=xa[i+m]-x;
53  w=c[i+1]-d[i];
54  if((den=ho-hp) == 0.0) //This error can occur only if two input
55  //xa’s are (to within roundo.) identical.
56  nrerror("Error in routine polint");
57  den=w/den;
58  d[i]=hp*den; //Here the c’s and d’s are updated.
59  c[i]=ho*den;
60  }
61  *y += (*dy=(2*ns < (n-m) ? c[ns+1] : d[ns--]));
62  /*
63  After each column in the tableau is completed, we decide which correction,
64  c or d, we want to add to our accumulating value of y,
65  i.e., which path to take through the tableau forking up or down.
66  We do this in such a way as to take the most straight
67  line route through the tableau to its apex, updating ns accordingly ù to keep track of where we are. This route keeps the partial approximations centered (insofar as possible) on the target x. The last dy added is thus the error indication. */ } free_vector(d,1,n); free_vector(c,1,n); }
68  to keep track of where we are.
69  This route keeps the partial approximations centered (insofar as possible)
70  on the target x. The last dy added is thus the error indication.
71  */
72  }
73  free_vector(d,1,n);
74  free_vector(c,1,n);
75 }
float * vector()
void free_vector()
void nrerror()
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

§ ratint()

void ratint ( float  xa[],
float  ya[],
int  n,
float  x,
float *  y,
float *  dy 
)

Definition at line 82 of file Interpolation.c.

References FREERETURN, nrerror(), TINY, and vector().

84 {
85  int m,i,ns=1;
86  float w,t,hh,h,dd,*c,*d;
87  c=vector(1,n);
88  d=vector(1,n);
89  hh=fabs(x-xa[1]);
90  for (i=1;i<=n;i++)
91  {
92  h=fabs(x-xa[i]);
93  if (h == 0.0)
94  {
95  *y=ya[i];
96  *dy=0.0;
98  }
99  else if(h < hh)
100  {
101  ns=i;
102  hh=h;
103  }
104  c[i]=ya[i];
105  d[i]=ya[i] + TINY; //The TINY part is needed to prevent a rare zero-over-zero condition.
106  }
107  *y=ya[ns--];
108  for (m=1;m<n;m++)
109  {
110  for (i=1;i<=n-m;i++)
111  {
112  w=c[i+1]-d[i];
113  h=xa[i+m]-x; //h will never be zero, since this was
114  //tested in the initializing loop.
115  t=(xa[i]-x)*d[i]/h;
116  dd=t-c[i+1];
117  if (dd == 0.0)
118  nrerror("Error in routine ratint");
119  //This error condition indicates that the interpolating function has a pole at the
120  //requested value of x.
121  dd=w/dd;
122  d[i]=c[i+1]*dd;
123  c[i]=t*dd;
124  }
125  *y += (*dy=(2*ns < (n-m) ? c[ns+1] : d[ns--]));
126  }
127  FREERETURN
128 }
#define TINY
Definition: Interpolation.c:13
float * vector()
#define FREERETURN
Definition: Interpolation.c:14
void nrerror()
+ Here is the call graph for this function:

§ spline()

void spline ( float  x[],
float  y[],
int  n,
float  yp1,
float  ypn,
float  y2[] 
)

Definition at line 138 of file Interpolation.c.

References free_vector(), p, and vector().

Referenced by testsplint(), and testsplintrc().

139 {
140  int i,k;
141  float p,qn,sig,un,*u;
142  u=vector(1,n-1);
143  if (yp1 > 0.99e30) //The lower boundary condition is set either to be “natural”
144  y2[1]=u[1]=0.0;
145  else
146  { //or else to have a specified first derivative.
147  y2[1] = -0.5;
148  u[1]=(3.0/(x[2]-x[1]))*((y[2]-y[1])/(x[2]-x[1])-yp1);
149  }
150  for (i=2;i<=n-1;i++)
151  { //This is the decomposition loop of the tridiagonal algorithm.
152  // y2 and u are used for temporary storage of the decomposed factors.
153  sig=(x[i]-x[i-1])/(x[i+1]-x[i-1]);
154  p=sig*y2[i-1]+2.0;
155  y2[i]=(sig-1.0)/p;
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;
158  }
159  if (ypn > 0.99e30) //The upper boundary condition is set either to be natural
160  qn=un=0.0;
161  else
162  { //or else to have a specified first derivative.
163  qn=0.5;
164  un=(3.0/(x[n]-x[n-1]))*(ypn-(y[n]-y[n-1])/(x[n]-x[n-1]));
165  }
166  y2[n]=(un-qn*u[n-1])/(qn*y2[n-1]+1.0);
167  for (k=n-1;k>=1;k--) //This is the backsubstitution loop of the tridiagonal algorithm.
168  y2[k]=y2[k]*y2[k+1]+u[k];
169 
170  free_vector(u,1,n-1);
171 }
float * vector()
static double p
Definition: SOLPOS.C:131
void free_vector()
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

§ splint()

void splint ( float  xa[],
float  ya[],
float  y2a[],
int  n,
float  x,
float *  y 
)

Definition at line 183 of file Interpolation.c.

References nrerror().

Referenced by testsplint(), and testsplintrc().

184 {
185  void nrerror(char error_text[]);
186  int klo,khi,k;
187  float h,b,a;
188  /*
189  We will find the right place in the table by means of
190  bisection. This is optimal if sequential calls to this
191  routine are at random values of x. If sequential calls
192  are in order, and closely spaced, one would do better
193  to store previous values of klo and khi and test if
194  they remain appropriate on the next call.
195  */
196  klo=1;
197  khi=n;
198  while (khi-klo > 1)
199  {
200  k=(khi+klo) >> 1;
201  if (xa[k] > x)
202  khi=k;
203  else klo=k;
204  } //klo and khi now bracket the input value of x.
205  h=xa[khi]-xa[klo];
206  if (h == 0.0)
207  nrerror("Bad xa input to routine splint"); //The xa’s must be distinct.
208  a=(xa[khi]-x)/h;
209  b=(x-xa[klo])/h; //Cubic spline polynomial is now evaluated.
210  *y=a*ya[klo]+b*ya[khi]+((a*a*a-a)*y2a[klo]+(b*b*b-b)*y2a[khi])*(h*h)/6.0;
211 }
void nrerror()
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

§ testpolintfull()

int testpolintfull ( void  )

Definition at line 308 of file Interpolation.c.

References free_vector(), PI, polint(), and vector().

309 {
310 
311  int i,n,nfunc;
312  float dy,f,x,y,*xa,*ya;
313 
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;
319  xa=vector(1,n);
320  ya=vector(1,n) ;
321  for (nfunc=1;nfunc<=2;nfunc++)
322  {
323  if (nfunc == 1)
324  {
325  printf("\nsine function from 0 to PI\n");
326  for (i=1;i<=n;i++)
327  {
328  xa[i] =i*PI/n;
329  ya[i] =sin(xa[i]);
330 
331  }
332 
333  }
334  else if( nfunc == 2)
335  {
336  printf("\nexponential function from 0 to 1\n");
337  for (i=1;i<=n;i++)
338  {
339  xa[i] =i*1.0/n;
340  ya[i]=exp(xa[i]) ;
341  }
342  }
343  else
344  {
345  break;
346  }
347 
348  printf("\n%9s %13s %16s %13s\n", "x", "f(x)", "interpolated", "error");
349  for (i=1;i<=10;i++)
350  {
351  if (nfunc == 1)
352  {
353  x=(-0.05+i/10.0)*PI;
354  f=sin(x);
355  }
356  else if (nfunc == 2)
357  {
358  x=(-0.05+i/10.0);
359  f=exp(x);
360  }
361 
362  polint(xa,ya,n,x,&y,&dy);
363  printf("%12.6f %12.6f %12.6f %4s %11f\n", x,f,y," ",dy);
364  }
365 
366  printf("\n***********************************\n");
367  printf("press RETURN\n");
368  (void) getchar();
369 
370 
371 
372  }
373 free_vector(ya,1,n);
374 free_vector(xa,1,n);
375 return 0;
376 
377 }
float * vector()
#define PI
Definition: Interpolation.c:15
void polint(float xa[], float ya[], int n, float x, float *y, float *dy)
Definition: Interpolation.c:28
void free_vector()
+ Here is the call graph for this function:

§ testpolintlambda()

int testpolintlambda ( void  )

Definition at line 379 of file Interpolation.c.

References dvector(), nrerror(), and polint().

380 {
381 
382  int i,n, l = 1, linit;
383  float dy,f,x,y;
384  FILE *nf;
385  float *xa,*ya;
386  xa=dvector(1,137);
387  ya=dvector(1,137) ;
388 
389 
390  if((nf = fopen("In.dat", "r")) == NULL)
391  nrerror("In.dat not found\n");
392  while(!feof(nf))
393  {
394  fscanf(nf, "%f%f", &xa[l], &ya[l]);
395  printf("%d %12.6f %12.6f \n", l, xa[l],ya[l]);
396 
397  l++;
398  }
399  fclose(nf);
400  linit = xa[1];
401 
402  nf = fopen("Is.dat", "w");
403 
404  for (i=1; i<l-2;i++)
405  {
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**************");
410 
411  fprintf(nf, "%12.6lf %12.6lf %d %12.6lf \n",xa[i],ya[i], i+linit, y);
412 
413  }
414 
415  fclose(nf);
416 
417 }
double * dvector()
void polint(float xa[], float ya[], int n, float x, float *y, float *dy)
Definition: Interpolation.c:28
void nrerror()
+ Here is the call graph for this function:

§ testpolintshort()

int testpolintshort ( void  )

Definition at line 214 of file Interpolation.c.

References free_vector(), PI, polint(), and vector().

215 {
216 
217  int i,n,nfunc;
218  float dy,f,x,y,*xa,*ya;
219 
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;
225 
226  for (nfunc=1;nfunc<=2;nfunc++)
227  {
228  xa=vector(1,n);
229  ya=vector(1,n) ;
230 
231 
232  if (nfunc == 1)
233  {
234  printf("\nsine function from 0 to PI\n");
235  printf("\n x y \n");
236  for (i=1;i<=n;i++)
237  {
238  xa[i] =i*PI/n;
239  ya[i] =sin(xa[i]);
240  printf("%12.6f %12.6f \n", xa[i],ya[i]);
241 
242  }
243 
244  }
245  else if( nfunc == 2)
246  {
247  printf("\nexponential function from 0 to 1\n");
248  printf("\n x y \n");
249  for (i=1;i<=n;i++)
250  {
251  xa[i] =i*1.0/n;
252  ya[i]=exp(xa[i]) ;
253  printf("%12.6f %12.6f \n", xa[i],ya[i]);
254 
255  }
256  }
257 
258  else
259  {
260  break;
261  }
262 
263  printf("\n%9s %13s %16s %13s\n", "x", "f(x)", "interpolated", "error");
264 // for (i=1;i<=10;i++)
265 // {
266  if (nfunc == 1)
267  {
268  //x=(-0.05+i/10.0)*PI;
269  x=(0.5)*PI;
270  f=sin(x);
271  }
272  else if (nfunc == 2)
273  {
274  //x=(-0.05+i/10.0);
275  x=(0.5);
276  f=exp(x);
277  }
278 /* else if (nfunc == 3)
279  {
280  x=(-0.05+i/10.0);
281  f=x*x ;
282  }
283 */
284  //polint(xa,ya,n,x,&y,&dy);
285  //polint(&xa[4],&ya[4],3,x,&y,&dy);
286  //printf("%12.6f %12.6f %12.6f %4s %11f\n", x,f,y," ",dy);
287 // }
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);
290 
291  free_vector(ya,1,n);
292  free_vector(xa,1,n);
293 
294 
295  printf("\n***********************************\n");
296  printf("press RETURN\n");
297  (void) getchar();
298 
299 
300 
301  }
302 
303 
304 return 0;
305 
306 }
float * vector()
#define PI
Definition: Interpolation.c:15
void polint(float xa[], float ya[], int n, float x, float *y, float *dy)
Definition: Interpolation.c:28
void free_vector()
+ Here is the call graph for this function:

§ testsplint()

int testsplint ( void  )

Definition at line 419 of file Interpolation.c.

References free_vector(), PI, spline(), splint(), and vector().

420 {
421 
422  int i,n,nfunc;
423  float f,x,y,yp1,ypn,*xa,*ya,*y2;
424 
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;
430  xa=vector(1,n);
431  ya=vector(1,n) ;
432  y2=vector(1,n) ;
433  for (nfunc=1;nfunc<=2;nfunc++)
434  {
435  if (nfunc == 1)
436  {
437  printf("\nsine function from 0 to PI\n");
438  for (i=1;i<=n;i++)
439  {
440  xa[i] =i*PI/n;
441  ya[i] =sin(xa[i]);
442 
443  }
444  yp1 = cos(xa[1]);
445  ypn = cos(xa[n]);
446  }
447  else if( nfunc == 2)
448  {
449  printf("\nexponential function from 0 to 1\n");
450  for (i=1;i<=n;i++)
451  {
452  xa[i] =i*1.0/n;
453  ya[i]=exp(xa[i]) ;
454  }
455  yp1 = exp(xa[1]);
456  ypn = exp(xa[n]);
457 
458  }
459  else
460  {
461  break;
462  }
463 
464  spline(xa,ya,n,yp1,ypn,y2);
465 
466  printf("\n%9s %13s %16s %13s\n", "x", "f(x)", "interpolated", "error");
467  for (i=1;i<=10;i++)
468  {
469  if (nfunc == 1)
470  {
471  x=(-0.05+i/10.0)*PI;
472  f=sin(x);
473  }
474  else if (nfunc == 2)
475  {
476  x=(-0.05+i/10.0);
477  f=exp(x);
478  }
479 
480  splint(xa,ya,y2,n,x,&y);
481  printf("%12.6f %12.6f %12.6f \n", x,f,y);
482  }
483 
484  printf("\n***********************************\n");
485  printf("press RETURN\n");
486  (void) getchar();
487 
488 
489 
490  }
491 free_vector(ya,1,n);
492 free_vector(xa,1,n);
493 return 0;
494 
495 }
float * vector()
#define PI
Definition: Interpolation.c:15
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 free_vector()
+ Here is the call graph for this function:

§ testsplintrc()

int testsplintrc ( void  )

Definition at line 497 of file Interpolation.c.

References free_vector(), nrerror(), spline(), splint(), and vector().

498 {
499 
500  int i,n,nfunc, linit, l = 1;
501  float f,x,y,yp1,ypn;
502  float *xa,*ya,*y2;
503  FILE *nf;
504  double a,b;
505 
506  if((nf = fopen("In.dat", "r")) == NULL)
507  nrerror("In.dat not found\n");
508  while(!feof(nf))
509  {
510  fscanf(nf, "%lf%lf", &a, &b);
511  l++;
512  }
513  l--;
514  fclose(nf);
515 
516  xa=vector(1,l);
517  ya=vector(1,l) ;
518  y2=vector(1,l) ;
519 
520 
521  if((nf = fopen("In.dat", "r")) == NULL)
522  nrerror("In.dat not found\n");
523 
524 
525  for (i=1; i<l; i++)
526  {
527  fscanf(nf, "%f%f", &xa[i], &ya[i]);
528  printf("%d %12.6f %12.6f \n", i, xa[i],ya[i]);
529 
530  }
531  printf("**********************************************\n");
532 
533  fclose(nf);
534 
535 /*
536  l=1;
537  while(!feof(nf))
538  {
539  fscanf(nf, "%f%f", &xa[l], &ya[l]);
540  printf("%d %12.6f %12.6f \n", l, xa[l],ya[l]);
541  l++;
542 
543  }
544  printf("**********************************************\n");
545  l--;
546  fclose(nf);
547 */
548  linit = xa[1];
549 
550 
551  yp1 = 1e30;
552  ypn = 1e30;
553 
554 
555  spline(xa,ya,l,yp1,ypn,y2);
556 
557 
558  nf = fopen("Is.dat", "w");
559 
560  for (i=1; i<l;i++)
561  {
562 
563  splint(&xa[i], &ya[i],&y2[i],3,(float) i+linit,&y);
564 
565  //polint(&xa[i], &ya[i],3,(float) i+linit,&y,&dy);
566  printf("\n%d %12.6f %12.6f %d %d %12.6f ", i, xa[i],ya[i],i, i+linit,y);
567  //printf("\n%d %d %12.6f ", i, i+linit,y);
568  //printf("\n**************");
569 
570  fprintf(nf, "%12.6lf %12.6lf %d %12.6lf \n",xa[i],ya[i], i+linit, y);
571 
572  }
573 
574  fclose(nf);
575 
576 free_vector(ya,1,n);
577 free_vector(xa,1,n);
578 return 0;
579 
580 }
float * vector()
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 free_vector()
void nrerror()
+ Here is the call graph for this function:
______________________________________________________________________________________
Generated on Mon Sep 18 2017 11:46:31 for DAS - Rel. 3.1.6 - 18/09/2017.