t1nman

Math.lab6

May 19th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #define alpha1 2
  4. #define beta1 0.5
  5. #define gamma1 1.8765
  6. #define alpha2 0.3
  7. #define beta2 2
  8. #define gamma2 3.4567
  9.  
  10. float S(const float *X, const float *F, float x, int N);
  11. int find_i(const float *X, float x, int N);
  12. void down_up(float a, float b, float c, float f, int N, float C[]);
  13.  
  14. int main(int argc, char **argv)
  15. {  
  16.     const int N = 5;
  17.     const float X[] = {0.2, 0.26, 0.28, 0.31, 0.32, 0.38};
  18.     const float F[] = {1.2214, 1.2765, 1.3071, 1.3456, 1.3775, 1.4568};
  19.     const float x = 0.32;
  20.     int i = find_i(X,x,N);
  21.     printf("\nS%d = %f\n\n", i, S(X,F,x,N));
  22.  
  23.     return 0;
  24. }
  25.  
  26. float S(const float *X, const float *F, float x, int N)
  27. {
  28.     int i = find_i(X,x,N);
  29.     float h0 = X[i] - X[i-1];
  30.     float h1 = X[i+1] - X[i];
  31.     float f = 6*( (F[i+1] - F[i])/h1 - (F[i] - F[i-1])/h0 );
  32.     float C[N+1];
  33.     down_up(h1, 2*(h0+h1), h0, f, N, C);
  34.  
  35.     float a = F[i];
  36.     float d = (C[i] - C[i-1])/h0;
  37.     float b = (h0/2)*C[i] - (pow(h0,2)/6)*d + (F[i]-F[i-1])/h0;
  38.     float Si = a + b*(x-X[i]) + (C[i]/2)*pow((x-X[i]),2) + (d/6)*pow((x-X[i]),3);
  39.  
  40.  
  41.     return Si;
  42. }
  43.  
  44. void down_up(float a, float b, float c, float f, int N, float Y[])
  45. {
  46.     float X[N+1], Z[N+1];
  47.     int i;
  48.  
  49.     X[0] = -1*beta1/alpha1;
  50.     Z[0] = gamma1/alpha1;
  51.    
  52.     for (i = 0; i < N; i++) {
  53.         X[i+1] = -1*a/(b+c*X[i]);
  54.         Z[i+1] = (f-c*Z[i])/(b+c*X[i]);
  55.     }
  56.  
  57.     Y[N] = (gamma2 - alpha2*Z[N]) / (beta2 + alpha2*X[N]);
  58.  
  59.     for (i = N; i > 0; i--) {
  60.         Y[i-1] = X[i] * Y[i] + Z[i];
  61.     }
  62. }
  63.  
  64. int find_i(const float *X, float x, int N)
  65. {
  66.     int i;
  67.  
  68.     for (i = 0; i < N+1; i++)
  69.         if (x <= X[i])
  70.             break;
  71.     return i;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment