home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_01 / 8n01057a < prev    next >
Text File  |  1990-02-19  |  2KB  |  84 lines

  1. *****Hermite (Listing 2)*****
  2.  
  3.  
  4. #define SUB(i,j,k) (i)*(k)+(j)
  5.  
  6.  
  7. double Hermite (x,y,n,norm,lambda,k,flag,res,err)
  8. /*
  9.  *  Given n data points (x[],y[]) find the Hermite cubic approximation
  10.  *  to this data using the k nots lambda[]. If flag = true then find the
  11.  *  knots from the routine app_knots() otherwise lambda[] is set by the
  12.  *  user. The 2k result is returned in res[] and the error at each point
  13.  *  is returned in err[].The overdetermined system of equations is 
  14.  *  solved with respect to the value of norm, uses L1-norm if norm = 1,
  15.  *  uses the L2-norm if norm = 2, and uses the minimax norm if norm = 3.
  16.  *  The return value z is the size of the resultant norm.
  17.  */
  18. double *x,*y,*lambda,*res,*err;
  19. int n,norm,k,flag;
  20. {
  21. double *a,z;
  22. int i,j,l,kk,m,m2;
  23. /*
  24.  *   Find whether the knots are to be computed.
  25.  */
  26. if (flag) app_knots (x,n,lambda,k);
  27. /*
  28.  *   Now form the system of equations one equation per data point.
  29.  */
  30. m2 = n*2*k;
  31. /*
  32.  *  Allocate space for the matrix.
  33.  */
  34. a = (double*)calloc(m2,sizeof(double));
  35. if (a==0) printf ("\n NO DYNAMIC SPACE AVAILABLE");
  36. else
  37.   {
  38.     for (i=0; i<m2; i++) *(a+i) = 0.0;
  39.     coeff_cubic (a,n,m,x,y,lambda,k);  /* Set up the matrix.  */
  40.       switch (norm)
  41.         {
  42.           case 1:
  43.                z = L1_approx(a,n,m,m,y,res,err);  /* L1-norm solution  */
  44.                break;
  45.           case 2:
  46.                z = CH_lsq (a,n,m,m,y,res,err);    /* L2-norm solution  */
  47.                break;
  48.           default:
  49.                z = Cheby (a,n,m,m,y,res,err);     /* Minimax norm solution  */
  50.                break;
  51.         }
  52.    free (a);
  53.   }
  54. return (z);
  55. }
  56.  
  57.  
  58.  
  59. void app_knots (x,n,lambda,k)
  60. /*
  61.  *  Given n x[] values compute k knots lambda[] such that the 
  62.  *  distribution of points in each interval is nearly the same.
  63.  */
  64. double *x,*lambda;
  65. int n,k;
  66. {
  67. int i,j,s,t;
  68. lambda[0] = x[0];
  69. lambda[k-1] = x[n-1]; 
  70. if (k>2)
  71.     {
  72.      i = n/(k-1);  j = (n-(i*(k-3)))/2;
  73.      lambda[1] = x[j];
  74.      if (k>3)
  75.             {
  76.              s = j;
  77.              for (t=2; t<k-1; t++)
  78.                 {s+=i;
  79.                  lambda[t] = x[s];
  80.                 }
  81.            }
  82.     }
  83. }
  84.