Advertisement
luizaspan

Molas acopladas (matriz tridiagonal)

Sep 16th, 2015
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 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 25
  28. #define C 1
  29. #define K 6
  30. #define m 1
  31. #define omega 2
  32. #define alpha 8
  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.     d[0] -= K;
  45.     d[n-1] -= K;
  46.  
  47.     a[0] /= d[0];
  48.     B[0] /= d[0];
  49.     for (int i = 1; i < n; ++i)
  50.     {
  51.         a[i] /= d[i] - b[i]*a[i-1];
  52.         B[i] = (B[i] - b[i]*B[i-1])/(d[i] - b[i]*a[i-1]);
  53.     }
  54.    
  55.     B[n] = (B[n] - b[n]*B[n-1])/(d[n]-b[n]*a[n-1]);
  56.  
  57.     x[0] = B[0];
  58.     for (int i = n-1; i >= 1; --i)
  59.     {
  60.         x[n] = B[n];
  61.         x[i] = B[i] - a[i]*x[i+1];
  62.     }
  63.  
  64.     for (int i = 0; i < n; ++i)
  65.     {
  66.         printf("%d %lf\n", i, x[i]);
  67.     }
  68.  
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement