Advertisement
luizaspan

Molas acopladas (matriz tridiagonal) II

Sep 16th, 2015
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. /*
  5. Matriz tridiagonal da forma:
  6. d a 0 0 0 0       x       B
  7. b d a 0 0 0       x       B
  8. 0 b d a 0 0   *   x   =   B
  9. 0 0 b d a 0       x       B
  10. 0 0 0 b d a       x       B
  11. 0 0 0 0 b d       x       B
  12. */
  13.  
  14. /*
  15. V = [1 2 3 4 5 6 7 8 9 10]
  16. dVdx = V(i+1) - V(i)
  17.    
  18. -1  1   0   0  0     1         1
  19.  0 -1   1   0  0     2         1
  20.  0  0  -1   1  0  *  3     =   1
  21.  0  0   0  -1  1     4         1
  22.  0  0   0   0 -1     5         1
  23.  
  24. dv = f(x) <=> A * v = f(x)
  25. */
  26.  
  27. #define n 250
  28. #define C 1
  29. #define K 6
  30. #define m 1
  31. #define omega 0.1
  32. #define alpha (2*K - m*omega*omega)
  33.  
  34. int main(void)
  35. {
  36.     double a[n], b[n], B[n], x[n], d[n];
  37.  
  38.     for (int i = 0; i < n; ++i)
  39.     {
  40.         b[i] = -K;
  41.         a[i] = -K;
  42.         d[i] = alpha;
  43.     }
  44.  
  45.     d[0] -= K;
  46.     d[n-1] -= K;
  47.  
  48.     a[0] /= d[0];
  49.     B[0] /= d[0];
  50.  
  51.     for (int i = 1; i < n; ++i)
  52.     {
  53.         a[i] /= d[i] - b[i]*a[i-1];
  54.         B[i] = (B[i] - b[i]*B[i-1])/(d[i] - b[i]*a[i-1]);
  55.     }
  56.  
  57.     B[n] = (B[n] - b[n]*B[n-1])/(d[n] - b[n]*a[n-1]);
  58.  
  59.     x[0] = B[0];
  60.  
  61.     for (int i = n-1; i >= 1; --i)
  62.     {
  63.         x[n] = B[n];
  64.         x[i] = B[i] - a[i]*x[i+1];
  65.     }
  66.  
  67.     for (int i = 0; i < n; ++i)
  68.     {
  69.         printf("%d %lf\n", i, x[i]);
  70.     }
  71.  
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement