derivative.c
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:4k
源码类别:

通讯编程

开发平台:

Visual C++

  1. /* This entire routine written by PW. */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "xgraph.h"
  5. void
  6. Der1()
  7. {
  8.   PointList *theList, *D1List, *D2List;
  9.   double m,b;
  10.   int i;
  11.   
  12.   for (i=0;i<MAXSETS;i++) {
  13.     theList = PlotData[i].list;
  14.     if (theList) {
  15.       DataD1[i].list = (PointList *)malloc(sizeof(PointList));
  16.       DataD2[i].list = (PointList *)malloc(sizeof(PointList));
  17.       D1List = DataD1[i].list;
  18.       D2List = DataD2[i].list;
  19.     } else 
  20.       DataD1[i].list = DataD2[i].list = NULL;
  21.     DataD1[i].setName = 
  22.       (char *)malloc(sizeof(char)*strlen(PlotData[i].setName));
  23.     DataD2[i].setName = 
  24.       (char *)malloc(sizeof(char)*strlen(PlotData[i].setName));
  25.     strcpy(DataD1[i].setName,PlotData[i].setName);
  26.     strcpy(DataD2[i].setName,PlotData[i].setName);
  27.     while (theList) {
  28.       int j;
  29.  
  30.       D1List->numPoints = D2List->numPoints = theList->numPoints;
  31.       D1List->xvec = (double *)malloc(sizeof(double)*theList->numPoints);
  32.       D1List->yvec = (double *)malloc(sizeof(double)*theList->numPoints);
  33.       D1List->next = NULL;
  34.       D2List->xvec = (double *)malloc(sizeof(double)*theList->numPoints);
  35.       D2List->yvec = (double *)malloc(sizeof(double)*theList->numPoints);
  36.       D2List->next = NULL;
  37.       for (j=1;j<theList->numPoints-1;j++) {
  38.         D1List->xvec[j] = D2List->xvec[j] = theList->xvec[j];
  39.         D1List->yvec[j] = (theList->yvec[j+1] - theList->yvec[j-1]) /
  40.                 (theList->xvec[j+1] - theList->xvec[j-1]);
  41.         D2List->yvec[j] = (theList->yvec[j+1] + theList->yvec[j-1] -
  42.                  2.0*theList->yvec[j]) /
  43.                 ((theList->xvec[j+1] - theList->xvec[j]) * 
  44.                  (theList->xvec[j]-theList->xvec[j-1]));
  45.  
  46.       }
  47.       /* Extrapolate to get the endpoints ... */
  48.       /* end near 0 */
  49.       D1List->xvec[0] = theList->xvec[0];
  50.       D1List->xvec[theList->numPoints-1] = 
  51.        theList->xvec[theList->numPoints-1];
  52.       m = (D1List->yvec[2]-D1List->yvec[1]) / 
  53.           (theList->xvec[2]-theList->xvec[1]);
  54.       b = D1List->yvec[1] - m*theList->xvec[1];
  55.       D1List->yvec[0] = m*theList->xvec[0] + b;
  56.       /* end near numPoints-1 */
  57.       m = (D1List->yvec[theList->numPoints-2]-
  58.            D1List->yvec[theList->numPoints-3]) / 
  59.         (theList->xvec[theList->numPoints-2]-
  60.          theList->xvec[theList->numPoints-3]);
  61.       b = D1List->yvec [theList->numPoints-2] - 
  62.           m*theList->xvec[theList->numPoints-2];
  63.       D1List->yvec[theList->numPoints-1] = 
  64.           m*theList->xvec[theList->numPoints-1] + b;
  65.       /* Extrapolate to get the endpoints ... */
  66.       /* end near 0 */
  67.       D2List->xvec[0] = theList->xvec[0];
  68.       D2List->xvec[theList->numPoints-1] = 
  69.        theList->xvec[theList->numPoints-1];
  70.       m = (D2List->yvec[2]-D2List->yvec[1]) / 
  71.           (theList->xvec[2]-theList->xvec[1]);
  72.       b = D2List->yvec[1] - m*theList->xvec[1];
  73.       D2List->yvec[0] = m*theList->xvec[0] + b;
  74.       /* end near numPoints-1 */
  75.       m = (D2List->yvec[theList->numPoints-2]-
  76.            D2List->yvec[theList->numPoints-3]) / 
  77.         (theList->xvec[theList->numPoints-2]-
  78.          theList->xvec[theList->numPoints-3]);
  79.       b = D2List->yvec[theList->numPoints-2] - 
  80.           m*theList->xvec[theList->numPoints-2];
  81.       D2List->yvec[theList->numPoints-1] = 
  82.            m*theList->xvec[theList->numPoints-1] + b;
  83.       theList = theList->next;
  84.       if (theList) {
  85.         D1List->next = (PointList *)malloc(sizeof(PointList)); 
  86.         D2List->next = (PointList *)malloc(sizeof(PointList)); 
  87.         D1List = D1List->next;
  88.         D2List = D2List->next;
  89.       }
  90.     }
  91.   }
  92. void
  93. Bounds(loX, loY, hiX, hiY, Ord)
  94.   double *loX, *loY, *hiX, *hiY;
  95.   int Ord;
  96. {
  97.   int i;
  98.   PointList *theList;
  99.   if ((Ord<1) || (Ord>2)) {
  100.     printf ("Internal Error - Cannot eval deriv > 2 in Bounds.n");
  101.     exit(1);
  102.   }
  103.   *loX = *loY = *hiX = *hiY = 0.0;
  104.   for (i=0;i<MAXSETS;i++) {
  105.     if (Ord == 1)
  106.       theList = DataD1[i].list;
  107.     else
  108.       theList = DataD2[i].list;
  109.     while (theList) {
  110.       int j;
  111.  
  112.       for (j=0;j<theList->numPoints;j++) {
  113.         *loX = (theList->xvec[j]<*loX?theList->xvec[j]:*loX);
  114.         *loY = (theList->yvec[j]<*loY?theList->yvec[j]:*loY);
  115.         *hiX = (theList->xvec[j]>*hiX?theList->xvec[j]:*hiX);
  116.         *hiY = (theList->yvec[j]>*hiY?theList->yvec[j]:*hiY);
  117.       }
  118.       theList = theList->next;
  119.     }
  120.   }
  121. }