DAS  3.1.6 - 18/09/2017
Interpolation.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <conio.h>
3 #include <math.h>
4 #include "Marq.h"
5 #include "nrutil.h"
6 //#include "driver.h"
7 /*
8 INTERPOLATION routines
9 
10  Danbo 12/02/04
11 
12 */
13 #define TINY 1.0e-25 // A small number.
14 #define FREERETURN {free_vector(d,1,n);free_vector(c,1,n);return;}
15 #define PI 3.1415926
16 
17 /*
18 Here is a routine for polynomial interpolation or extrapolation from N input
19 points. Note that the input arrays are assumed to be unit-offset. If you have
20 zero-offset arrays, remember to subtract 1 (see §1.2):
21 */
22 
23 /*Given arrays xa[1..n] and ya[1..n], and given a value x, this routine
24  returns a value y, and an error estimate dy.
25  If P(x) is the polynomial of degree N - 1 such that P(xai) = yai, i =
26  1, . . . , n, then the returned value y = P(x).
27 */
28 void polint(float xa[], float ya[], int n, float x, float *y, float *dy)
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); } /*Given arrays xa[1..n] and ya[1..n], and given a value of x, this routine returns a value of y and an accuracy estimate dy. The value returned is that of the diagonal rational function, evaluated at x, which passes through the n points (xai, yai), i = 1...n. */ void ratint(float xa[], float ya[], int n, float x, float *y, float *dy) { int m,i,ns=1; float w,t,hh,h,dd,*c,*d; c=vector(1,n); d=vector(1,n); hh=fabs(x-xa[1]); for (i=1;i<=n;i++) { h=fabs(x-xa[i]); if (h == 0.0) { *y=ya[i]; *dy=0.0; FREERETURN } else if(h < hh) { ns=i; hh=h; } c[i]=ya[i]; d[i]=ya[i] + TINY; //The TINY part is needed to prevent a rare zero-over-zero condition. } *y=ya[ns--]; for (m=1;m<n;m++) { for (i=1;i<=n-m;i++) { w=c[i+1]-d[i]; h=xa[i+m]-x; //h will never be zero, since this was //tested in the initializing loop. t=(xa[i]-x)*d[i]/h; dd=t-c[i+1]; if (dd == 0.0) nrerror("Error in routine ratint"); //This error condition indicates that the interpolating function has a pole at the //requested value of x. dd=w/dd; d[i]=c[i+1]*dd; c[i]=t*dd; } *y += (*dy=(2*ns < (n-m) ? c[ns+1] : d[ns--])); } FREERETURN } /*Given arrays x[1..n] and y[1..n] containing a tabulated function, i.e., yi = f(xi), with x1 < x2 < .. . < xN, and given values yp1 and ypn for the first derivative of the interpolating function at points 1 and n, respectively, this routine returns an array y2[1..n] that contains the second derivatives of the interpolating function at the tabulated points xi. If yp1 and/or ypn are equal to 1 × 1030 or larger, the routine is signaled to set the corresponding boundary condition for a natural spline, with zero second derivative on that boundary. */ void spline(float x[], float y[], int n, float yp1, float ypn, float y2[]) { int i,k; float p,qn,sig,un,*u; u=vector(1,n-1); if (yp1 > 0.99e30) //The lower boundary condition is set either to be “natural” y2[1]=u[1]=0.0; else { //or else to have a specified first derivative. y2[1] = -0.5; u[1]=(3.0/(x[2]-x[1]))*((y[2]-y[1])/(x[2]-x[1])-yp1); } for (i=2;i<=n-1;i++) { //This is the decomposition loop of the tridiagonal algorithm. // y2 and u are used for temporary storage of the decomposed factors. sig=(x[i]-x[i-1])/(x[i+1]-x[i-1]); p=sig*y2[i-1]+2.0; y2[i]=(sig-1.0)/p; u[i]=(y[i+1]-y[i])/(x[i+1]-x[i]) - (y[i]-y[i-1])/(x[i]-x[i-1]); u[i]=(6.0*u[i]/(x[i+1]-x[i-1])-sig*u[i-1])/p; } if (ypn > 0.99e30) //The upper boundary condition is set either to be natural qn=un=0.0; else { //or else to have a specified first derivative. qn=0.5; un=(3.0/(x[n]-x[n-1]))*(ypn-(y[n]-y[n-1])/(x[n]-x[n-1])); } y2[n]=(un-qn*u[n-1])/(qn*y2[n-1]+1.0); for (k=n-1;k>=1;k--) //This is the backsubstitution loop of the tridiagonal algorithm. y2[k]=y2[k]*y2[k+1]+u[k]; free_vector(u,1,n-1); } /* It is important to understand that the program spline is called only once to process an entire tabulated function in arrays xi and yi. Once this has been done, values of the interpolated function for any value of x are obtained by calls (as many as desired) to a separate routine splint (for “spline interpolation”): */ /*Given the arrays xa[1..n] and ya[1..n], which tabulate a function (with the xai’s in order), and given the array y2a[1..n], which is the output from spline above, and given a value of x, this routine returns a cubic-spline interpolated value y. */ void splint(float xa[], float ya[], float y2a[], int n, float x, float *y) { void nrerror(char error_text[]); int klo,khi,k; float h,b,a; /* We will find the right place in the table by means of bisection. This is optimal if sequential calls to this routine are at random values of x. If sequential calls are in order, and closely spaced, one would do better to store previous values of klo and khi and test if they remain appropriate on the next call. */ klo=1; khi=n; while (khi-klo > 1) { k=(khi+klo) >> 1; if (xa[k] > x) khi=k; else klo=k; } //klo and khi now bracket the input value of x. h=xa[khi]-xa[klo]; if (h == 0.0) nrerror("Bad xa input to routine splint"); //The xa’s must be distinct. a=(xa[khi]-x)/h; b=(x-xa[klo])/h; //Cubic spline polynomial is now evaluated. *y=a*ya[klo]+b*ya[khi]+((a*a*a-a)*y2a[klo]+(b*b*b-b)*y2a[khi])*(h*h)/6.0; } /* Driver for polint routine */ int testpolintshort(void) { int i,n,nfunc; float dy,f,x,y,*xa,*ya; printf("generation of interpolation tables\n"); printf(" ... sin(x) 0<x<PI\n"); printf(" ... exp(x) 0<x<1 \n"); printf("how many entries go in these tables?\n"); if (scanf("%d",&n) == EOF) return 1; for (nfunc=1;nfunc<=2;nfunc++) { xa=vector(1,n); ya=vector(1,n) ; if (nfunc == 1) { printf("\nsine function from 0 to PI\n"); printf("\n x y \n"); for (i=1;i<=n;i++) { xa[i] =i*PI/n; ya[i] =sin(xa[i]); printf("%12.6f %12.6f \n", xa[i],ya[i]); } } else if( nfunc == 2) { printf("\nexponential function from 0 to 1\n"); printf("\n x y \n"); for (i=1;i<=n;i++) { xa[i] =i*1.0/n; ya[i]=exp(xa[i]) ; printf("%12.6f %12.6f \n", xa[i],ya[i]); } } else { break; } printf("\n%9s %13s %16s %13s\n", "x", "f(x)", "interpolated", "error"); // for (i=1;i<=10;i++) // { if (nfunc == 1) { //x=(-0.05+i/10.0)*PI; x=(0.5)*PI; f=sin(x); } else if (nfunc == 2) { //x=(-0.05+i/10.0); x=(0.5); f=exp(x); } /* else if (nfunc == 3) { x=(-0.05+i/10.0); f=x*x ; } */ //polint(xa,ya,n,x,&y,&dy); //polint(&xa[4],&ya[4],3,x,&y,&dy); //printf("%12.6f %12.6f %12.6f %4s %11f\n", x,f,y," ",dy); // } polint(&xa[n/2],&ya[n/2],5,x,&y,&dy); printf("%12.6f %12.6f %12.6f %4s %11f\n", x,f,y," ",dy); free_vector(ya,1,n); free_vector(xa,1,n); printf("\n***********************************\n"); printf("press RETURN\n"); (void) getchar(); } return 0; } int testpolintfull(void) { int i,n,nfunc; float dy,f,x,y,*xa,*ya; printf("generation of interpolation tables\n"); printf(" ... sin(x) 0<x<PI\n"); printf(" ... exp(x) 0<x<1 \n"); printf("how many entries go in these tables?\n"); if (scanf("%d",&n) == EOF) return 1; xa=vector(1,n); ya=vector(1,n) ; for (nfunc=1;nfunc<=2;nfunc++) { if (nfunc == 1) { printf("\nsine function from 0 to PI\n"); for (i=1;i<=n;i++) { xa[i] =i*PI/n; ya[i] =sin(xa[i]); } } else if( nfunc == 2) { printf("\nexponential function from 0 to 1\n"); for (i=1;i<=n;i++) { xa[i] =i*1.0/n; ya[i]=exp(xa[i]) ; } } else { break; } printf("\n%9s %13s %16s %13s\n", "x", "f(x)", "interpolated", "error"); for (i=1;i<=10;i++) { if (nfunc == 1) { x=(-0.05+i/10.0)*PI; f=sin(x); } else if (nfunc == 2) { x=(-0.05+i/10.0); f=exp(x); } polint(xa,ya,n,x,&y,&dy); printf("%12.6f %12.6f %12.6f %4s %11f\n", x,f,y," ",dy); } printf("\n***********************************\n"); printf("press RETURN\n"); (void) getchar(); } free_vector(ya,1,n); free_vector(xa,1,n); return 0; } int testpolintlambda(void) { int i,n, l = 1, linit; float dy,f,x,y; FILE *nf; float *xa,*ya; xa=dvector(1,137); ya=dvector(1,137) ; if((nf = fopen("In.dat", "r")) == NULL) nrerror("In.dat not found\n"); while(!feof(nf)) { fscanf(nf, "%f%f", &xa[l], &ya[l]); printf("%d %12.6f %12.6f \n", l, xa[l],ya[l]); l++; } fclose(nf); linit = xa[1]; nf = fopen("Is.dat", "w"); for (i=1; i<l-2;i++) { polint(&xa[i], &ya[i],2,(float) i+linit,&y,&dy); printf("\n%d %12.6f %12.6f ", i, xa[i],ya[i]); printf("\n%d %d %12.6f %12.6f", i, i+linit,y,dy); printf("\n**************"); fprintf(nf, "%12.6lf %12.6lf %d %12.6lf \n",xa[i],ya[i], i+linit, y); } fclose(nf); } int testsplint(void) { int i,n,nfunc; float f,x,y,yp1,ypn,*xa,*ya,*y2; printf("generation of interpolation tables\n"); printf(" ... sin(x) 0<x<PI\n"); printf(" ... exp(x) 0<x<1 \n"); printf("how many entries go in these tables?\n"); if (scanf("%d",&n) == EOF) return 1; xa=vector(1,n); ya=vector(1,n) ; y2=vector(1,n) ; for (nfunc=1;nfunc<=2;nfunc++) { if (nfunc == 1) { printf("\nsine function from 0 to PI\n"); for (i=1;i<=n;i++) { xa[i] =i*PI/n; ya[i] =sin(xa[i]); } yp1 = cos(xa[1]); ypn = cos(xa[n]); } else if( nfunc == 2) { printf("\nexponential function from 0 to 1\n"); for (i=1;i<=n;i++) { xa[i] =i*1.0/n; ya[i]=exp(xa[i]) ; } yp1 = exp(xa[1]); ypn = exp(xa[n]); } else { break; } spline(xa,ya,n,yp1,ypn,y2); printf("\n%9s %13s %16s %13s\n", "x", "f(x)", "interpolated", "error"); for (i=1;i<=10;i++) { if (nfunc == 1) { x=(-0.05+i/10.0)*PI; f=sin(x); } else if (nfunc == 2) { x=(-0.05+i/10.0); f=exp(x); } splint(xa,ya,y2,n,x,&y); printf("%12.6f %12.6f %12.6f \n", x,f,y); } printf("\n***********************************\n"); printf("press RETURN\n"); (void) getchar(); } free_vector(ya,1,n); free_vector(xa,1,n); return 0; } int testsplintrc(void) { int i,n,nfunc, linit, l = 1; float f,x,y,yp1,ypn; float *xa,*ya,*y2; FILE *nf; double a,b; if((nf = fopen("In.dat", "r")) == NULL) nrerror("In.dat not found\n"); while(!feof(nf)) { fscanf(nf, "%lf%lf", &a, &b); l++; } l--; fclose(nf); xa=vector(1,l); ya=vector(1,l) ; y2=vector(1,l) ; if((nf = fopen("In.dat", "r")) == NULL) nrerror("In.dat not found\n"); for (i=1; i<l; i++) { fscanf(nf, "%f%f", &xa[i], &ya[i]); printf("%d %12.6f %12.6f \n", i, xa[i],ya[i]); } printf("**********************************************\n"); fclose(nf); /* l=1; while(!feof(nf)) { fscanf(nf, "%f%f", &xa[l], &ya[l]); printf("%d %12.6f %12.6f \n", l, xa[l],ya[l]); l++; } printf("**********************************************\n"); l--; fclose(nf); */ linit = xa[1]; yp1 = 1e30; ypn = 1e30; spline(xa,ya,l,yp1,ypn,y2); nf = fopen("Is.dat", "w"); for (i=1; i<l;i++) { splint(&xa[i], &ya[i],&y2[i],3,(float) i+linit,&y); //polint(&xa[i], &ya[i],3,(float) i+linit,&y,&dy); printf("\n%d %12.6f %12.6f %d %d %12.6f ", i, xa[i],ya[i],i, i+linit,y); //printf("\n%d %d %12.6f ", i, i+linit,y); //printf("\n**************"); fprintf(nf, "%12.6lf %12.6lf %d %12.6lf \n",xa[i],ya[i], i+linit, y); } fclose(nf); free_vector(ya,1,n); free_vector(xa,1,n); return 0; }
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 }
76 
77 
78 /*Given arrays xa[1..n] and ya[1..n], and given a value of x, this routine returns a value of
79 y and an accuracy estimate dy. The value returned is that of the diagonal rational function,
80 evaluated at x, which passes through the n points (xai, yai), i = 1...n.
81 */
82 void ratint(float xa[], float ya[], int n, float x, float *y, float *dy)
83 
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 }
129 
130 
131 /*Given arrays x[1..n] and y[1..n] containing a tabulated function, i.e., yi = f(xi), with
132 x1 < x2 < .. . < xN, and given values yp1 and ypn for the first derivative of the interpolating
133 function at points 1 and n, respectively, this routine returns an array y2[1..n] that contains
134 the second derivatives of the interpolating function at the tabulated points xi. If yp1 and/or
135 ypn are equal to 1 × 1030 or larger, the routine is signaled to set the corresponding boundary
136 condition for a natural spline, with zero second derivative on that boundary.
137 */
138 void spline(float x[], float y[], int n, float yp1, float ypn, float y2[])
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 }
172 /*
173 It is important to understand that the program spline is called only once to
174 process an entire tabulated function in arrays xi and yi. Once this has been done,
175 values of the interpolated function for any value of x are obtained by calls (as many
176 as desired) to a separate routine splint (for “spline interpolation”):
177 */
178 
179 /*Given the arrays xa[1..n] and ya[1..n], which tabulate a function (with the xai’s in order),
180 and given the array y2a[1..n], which is the output from spline above, and given a value of
181 x, this routine returns a cubic-spline interpolated value y.
182 */
183 void splint(float xa[], float ya[], float y2a[], int n, float x, float *y)
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 }
212 
213 /* Driver for polint routine */
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 }
307 
308 int testpolintfull(void)
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 }
378 
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 }
418 
419 int testsplint(void)
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 }
496 
497 int testsplintrc(void)
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 }
#define TINY
Definition: Interpolation.c:13
int testpolintshort(void)
float * vector()
#define FREERETURN
Definition: Interpolation.c:14
double * dvector()
#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 splint(float xa[], float ya[], float y2a[], int n, float x, float *y)
int testpolintfull(void)
static double p
Definition: SOLPOS.C:131
int testsplint(void)
void spline(float x[], float y[], int n, float yp1, float ypn, float y2[])
void free_vector()
int testsplintrc(void)
void ratint(float xa[], float ya[], int n, float x, float *y, float *dy)
Definition: Interpolation.c:82
int testpolintlambda(void)
void nrerror()
______________________________________________________________________________________
Generated on Mon Sep 18 2017 11:44:09 for DAS - Rel. 3.1.6 - 18/09/2017.