1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
//
// spline.h
//
#ifndef _spline_h
#define _spline_h
#include "usrdef.h"
void spline(double x[], double y[], const int n, double yp1, double ypn, double y2[])
{
int i, k;
double p, qn, sig, un;//, *u
vector<double> u(n-1);
if (yp1 > 0.99e30)
y2[1] = u[1] = 0.0;
else{
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++) {
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)
qn = un =0.0;
else{
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--)
y2[k] = y2[k]*y2[k+1]+u[k];
u.clear();
//delete[] u;
}
void splint(double xa[], double ya[], double y2a[], int n, double x, double yy)
{
void nrerror(char error_text[]);
int klo, khi, k;
double h, b, a;
klo = 1;
khi = n;
while (khi-klo > 1) {
k = (khi+klo) >>1;
if (xa[k] >x) khi=k;
else klo = k;
}
h = xa[khi]-xa[klo];
if (h == 0.0) cout<<"Bad Xa input to routine splint"<<"\n";
a = (xa[khi]-x)/h;
b = (x-xa[klo])/h;
yy = a*ya[klo]+b*ya[khi]+((a*a*a-a)*y2a[klo]+(b*b*b-b)*y2a[khi])*(h*h)/6.0;
}
#endif
|