Ladies_Man

cubic spline eng wiki algo

Jun 13th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include "math.h"
  4. #include "stdlib.h"
  5.  
  6. using namespace std;
  7.  
  8. #define xtype float
  9. #define n 20
  10.  
  11. xtype _a = 2.477520;
  12. xtype _b = 0.232206;
  13.  
  14. xtype f(xtype x) { return _a * exp(_b*x); }
  15.  
  16.  
  17. typedef struct spline {
  18.     xtype a[n + 1];
  19.     xtype b[n];
  20.     xtype c[n + 1];
  21.     xtype d[n];
  22.     xtype x[n];
  23. } _spline;
  24.  
  25. _spline populateSpline(xtype x[n], xtype y[n]) {
  26.     int i;
  27.  
  28.     xtype a[n+1], b[n], d[n];
  29.     xtype h[n], alpha[n];
  30.     xtype c[n+1], l[n+1], mu[n+1], z[n+1];
  31.  
  32.  
  33.     for (i = 0; i <= n; i++)
  34.         a[i] = y[i];
  35.  
  36.     for (i = 0; i < n; i++)
  37.         h[i] = x[i+1] - x[i];
  38.  
  39.  
  40.     for (i = 1; i < n; i++)
  41.         alpha[i] = (a[i+1] - a[i]) * 3/h[i] -
  42.                         (a[i] - a[i-1]) * 3/h[i-1];
  43.  
  44.  
  45.     l[0] = 1, mu[0] = z[0] = 0;
  46.  
  47.     for (i = 1; i < n-1; i++) {
  48.  
  49.         l[i] = 2*(x[i+1] - x[i-1]) - h[i-1]*mu[i-1];
  50.         mu[i] = h[i] / l[i];
  51.         z[i] = (alpha[i] - h[i-1]*z[i-1]) / l[i];
  52.  
  53.         printf("l:%.3f mu:%.3f z:%.3f\n",
  54.                 l[i], mu[i], z[i]);
  55.     }
  56.  
  57.  
  58.  
  59.     l[(int)n] = 1;
  60.     z[(int)n] = c[(int)n] = 0;
  61.  
  62.     for (i = n - 1; i >= 0; --i) {
  63.         c[i] = z[i] - mu[i]*c[i+1];
  64.  
  65.         b[i] = (a[i+1] - a[i]) / h[i] -
  66.                     h[i]*(c[i+1] + 2*c[i]) / 3;
  67.  
  68.         d[i] = (c[i+1] - c[i]) / (3 * h[i]);
  69.         printf("b:%.3f c:%.3f d:%.3f\n",
  70.                 b[i], c[i], d[i]);
  71.     }
  72.  
  73.  
  74.     _spline spZ;
  75.     for (i = 0; i < n; i++) {
  76.         spZ.a[i] = a[i];
  77.         spZ.b[i] = b[i];
  78.         spZ.c[i] = c[i];
  79.         spZ.d[i] = d[i];
  80.         spZ.x[i] = x[i];
  81.         printf("a:%.3f b:%.3f c:%.3f d:%.3f x:%.3f\n",
  82.                 a[i], b[i], c[i], d[i], x[i]);
  83.     }
  84.  
  85.     return spZ;
  86. }
  87.  
  88. xtype countSplineAt(_spline spline, int i, xtype x) {
  89.  
  90.     //S_j[x] = a_j + b_j(x-xj) + c_j(x-xj)^2 + d_j(x-xj)^3
  91.  
  92.     return  spline.a[i] +
  93.             spline.b[i]*(x - spline.x[i]) +
  94.             spline.c[i]*(x - spline.x[i])*(x - spline.x[i]) +
  95.             spline.d[i]*(x - spline.x[i])*(x - spline.x[i])*(x - spline.x[i]);
  96. }
  97.  
  98.  
  99.  
  100.  
  101. int main() {
  102.     int i;
  103.     xtype xs[] = {0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0};
  104.     xtype ys[] = {3.02, 3.21, 2.95, 4.06, 4.03, 5.39, 5.97, 6.51, 6.77, 7.79};
  105.  
  106.  
  107.  
  108.     xtype xsNew[n];
  109.     xtype ysNew[n];
  110.     xsNew[0] = 0.25;
  111.     ysNew[0] = f(xsNew[0]);
  112.  
  113.     printf("increasing frequency of X-values\n");
  114.     printf("[0](%.2f, %f)\n", xsNew[0], ysNew[0]);
  115.     for (i = 1; i < 20; i++) {
  116.  
  117.         xsNew[i] = xsNew[i-1] + 0.25;
  118.         ysNew[i] = f(xsNew[i]);
  119.  
  120.         printf("[%d](%.2f, %f)\n", i, xsNew[i], ysNew[i]);
  121.     }
  122.     int iAmount = i;
  123.  
  124.     printf("populating spline\n");
  125.     _spline spline = populateSpline(xsNew, ysNew);
  126.  
  127.  
  128.     for (i = 1; i < iAmount; i++) {
  129.  
  130.         xtype sVal = countSplineAt(spline, i, xsNew[i]);
  131.  
  132.         printf("y[%d]=%f s[%d]=%.16f d=%f\n",
  133.                 i, ysNew[i], i, sVal, fabs(ysNew[i] - sVal));
  134.     }
  135.     printf("done\n");
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment