Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #define N 2
  5.  
  6. void Vector(double*, double[N][N], double);
  7. void Output(const char*, double*);
  8. void YMT(double[N][N], double*, double*, double);
  9. double Inaccuracy(double*, double, int);
  10. int ComputingInaccuracy(double*, double, int);
  11.  
  12. int main() {
  13. double AD[N][N];
  14. double X0[N], X[N];
  15. double X_analit[N];
  16. double h[] = { 0.01, 0.001, 0.0001 };
  17.  
  18. AD[0][0] = -1;
  19. AD[0][1] = 100;
  20. AD[1][0] = -100;
  21. AD[1][1] = -1;
  22.  
  23. for (int i = 0; i < 3; i++) {
  24. printf("h = %f\n%dx%d LAE systems\nn = %d\n", h[i], N, N, N);
  25. Vector(X0, AD, 0.0);
  26. Output("X0", X0);
  27. YMT(AD, X0, X, h[i]);
  28. Vector(X_analit, AD, h[i]);
  29. printf("analitic value of X:\n");
  30. Output("X", X_analit);
  31. for (int j = 0; j < N; j++) {
  32. printf("X[%d]:\neps=%32.16f\nepsComp=%11d\n", j, Inaccuracy(X, X_analit[j], j), ComputingInaccuracy(X, X_analit[j], j));
  33. }
  34. printf("\n\n");
  35. }
  36. return 0;
  37. }
  38.  
  39. double Inaccuracy(double* X, double Z, int i) {
  40. return fabs((X[i] - Z) / Z);
  41. }
  42.  
  43. int ComputingInaccuracy(double* X, double Y, int k) {
  44. int i = 0, state = 0, y_i, z_i;
  45. double y, z;
  46. y = Y;
  47. z = X[k];
  48. y_i = (int)y;
  49. z_i = (int)z;
  50. while (y_i == z_i) {
  51. if (y_i && z_i && !state)
  52. state = 1;
  53. if ((y_i == z_i) && (y_i || (!y_i && state == 1)))
  54. i++;
  55. y = y * 10 - y_i * 10;
  56. z = z * 10 - z_i * 10;
  57. y_i = (int)y;
  58. z_i = (int)z;
  59. }
  60. return i;
  61. }
  62.  
  63. void Vector(double* Z, double AD[N][N], double t) {
  64. Z[0] = (3*cos(100*t) + sin(100*t))*exp(-t);
  65. Z[1] = (cos(100*t) - 3*sin(100*t))*exp(-t);
  66. }
  67.  
  68. void Output(const char* c, double* Y) {
  69. printf("vector %s:\n", c);
  70. for (int i = 0; i < N; i++) {
  71. printf("%sij = %32.16f\n", c, Y[i]);
  72. }
  73. }
  74.  
  75. void YMT(double AD[N][N], double X0[N], double X[N], double h) {
  76. double X1[N];
  77. for (int i = 0; i < N; i++) {
  78. X1[i] = X0[i] + h * (AD[i][0] * X0[0] + AD[i][1] * X0[1]);
  79. X[i] = X0[i] + (h / 2) * (AD[i][0] * X0[0] + AD[i][1] * X0[1] + AD[i][0] * X1[0] + AD[i][1] * X1[0]);
  80. }
  81. Output("X", X);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement