Ejemplo n.º 1
0
void Natural_Cubic_Spline( const real *h, const real *f, 
			   cubic_spline_coef *coef, unsigned int n, 
			   MPI_Comm comm )
{
  int i;
  real *a, *b, *c, *d, *v;

  /* allocate space for the linear system */
  a = (real*) smalloc( n * sizeof(real), "cubic_spline:a", comm );
  b = (real*) smalloc( n * sizeof(real), "cubic_spline:a", comm );
  c = (real*) smalloc( n * sizeof(real), "cubic_spline:a", comm );
  d = (real*) smalloc( n * sizeof(real), "cubic_spline:a", comm );
  v = (real*) smalloc( n * sizeof(real), "cubic_spline:a", comm );

  /* build the linear system */
  a[0] = a[1] = a[n-1] = 0;
  for( i = 2; i < n-1; ++i )
    a[i] = h[i-1];

  b[0] = b[n-1] = 0;
  for( i = 1; i < n-1; ++i )
    b[i] = 2 * (h[i-1] + h[i]); 

  c[0] = c[n-2] = c[n-1] = 0;
  for( i = 1; i < n-2; ++i )
    c[i] = h[i];

  d[0] = d[n-1] = 0;
  for( i = 1; i < n-1; ++i )
    d[i] = 6 * ((f[i+1]-f[i])/h[i] - (f[i]-f[i-1])/h[i-1]);
  
  v[0] = 0;
  v[n-1] = 0;
  Tridiagonal_Solve( &(a[1]), &(b[1]), &(c[1]), &(d[1]), &(v[1]), n-2 );
  
  for( i = 1; i < n; ++i ){
    coef[i-1].d = (v[i] - v[i-1]) / (6*h[i-1]);
    coef[i-1].c = v[i]/2;
    coef[i-1].b = (f[i]-f[i-1])/h[i-1] + h[i-1]*(2*v[i] + v[i-1])/6;
    coef[i-1].a = f[i];
  }

  sfree( a, "cubic_spline:a" );
  sfree( b, "cubic_spline:b" );
  sfree( c, "cubic_spline:c" );
  sfree( d, "cubic_spline:d" );
  sfree( v, "cubic_spline:v" );
}
Ejemplo n.º 2
0
void Complete_Cubic_Spline( const double *h, const double *f, double v0, double vlast,
                            cubic_spline_coef *coef, unsigned int n,
                            MPI_Comm comm )
{
  int i;
  double *a, *b, *c, *d, *v;

  /* allocate space for the linear system */
  a = (double*) smalloc( n * sizeof(double), "cubic_spline:a", comm );
  b = (double*) smalloc( n * sizeof(double), "cubic_spline:a", comm );
  c = (double*) smalloc( n * sizeof(double), "cubic_spline:a", comm );
  d = (double*) smalloc( n * sizeof(double), "cubic_spline:a", comm );
  v = (double*) smalloc( n * sizeof(double), "cubic_spline:a", comm );

  /* build the linear system */
  a[0] = 0;
  for( i = 1; i < n; ++i )
    a[i] = h[i-1];

  b[0] = 2*h[0];
  for( i = 1; i < n; ++i )
    b[i] = 2 * (h[i-1] + h[i]);

  c[n-1] = 0;
  for( i = 0; i < n-1; ++i )
    c[i] = h[i];

  d[0] = 6 * (f[1]-f[0])/h[0] - 6 * v0;
  d[n-1] = 6 * vlast - 6 * (f[n-1]-f[n-2]/h[n-2]);
  for( i = 1; i < n-1; ++i )
    d[i] = 6 * ((f[i+1]-f[i])/h[i] - (f[i]-f[i-1])/h[i-1]);

  Tridiagonal_Solve( &(a[0]), &(b[0]), &(c[0]), &(d[0]), &(v[0]), n );

  for( i = 1; i < n; ++i ){
    coef[i-1].d = (v[i] - v[i-1]) / (6*h[i-1]);
    coef[i-1].c = v[i]/2;
    coef[i-1].b = (f[i]-f[i-1])/h[i-1] + h[i-1]*(2*v[i] + v[i-1])/6;
    coef[i-1].a = f[i];
  }

  sfree( a, "cubic_spline:a" );
  sfree( b, "cubic_spline:b" );
  sfree( c, "cubic_spline:c" );
  sfree( d, "cubic_spline:d" );
  sfree( v, "cubic_spline:v" );
}