freesky

task_sem2_3.1

Nov 3rd, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define e 1e-10
  6.  
  7. double *el(int n, int i, int j, double *a) {
  8.     return a + i * n + j;
  9. }
  10.  
  11. int maxLine(int n, int i, int j, double *a) {
  12.     double max = *el(n, i, j, a);
  13.     int maxi = i;
  14.  
  15.     for (int k = i; k < n; k++)
  16.         if (fabs(*el(n, k, j, a)) > fabs(max)) {
  17.             max = *el(n, k, j, a);
  18.             maxi = k;
  19.         }
  20.  
  21.     if (max == 0.0)
  22.         return -1;
  23.     else
  24.         return maxi;
  25. }
  26.  
  27. void swapLines(int n, double *a, double *b, int l, int m) {
  28.     for (int j = 0; j < n; j++) {
  29.         double t = *el(n, l, j, a);
  30.         *el(n, l, j, a) = *el(n, m, j, a);
  31.         *el(n, m, j, a) = t;
  32.     }
  33.  
  34.     double t = *(b + l);
  35.     *(b + l) = *(b + m);
  36.     *(b + m) = t;
  37. }
  38.  
  39. int linsys(int n, double *a, double *b, double *x) {
  40.     for (int i = 0; i < n - 1; i++) { // for all lines excluding the last...
  41.         int maxl = maxLine(n, i, i, a); // ...we find main item in column and then...
  42.         if (maxl == -1)
  43.             return 0;
  44.         if (maxl > *el(n, i, i, a))
  45.             swapLines(n, a, b, i, maxl);
  46.  
  47.         for (int k = i + 1; k < n; k++) { // ...we will set all items from current column to zero excluding current line
  48.             double coeff = -(*el(n, k, i, a)) / *el(n, i, i, a);
  49.             for (int l = i; l < n; l++)
  50.                 *el(n, k, l, a) += *el(n, i, l, a) * coeff;
  51.  
  52.             *(b + k) += *(b + i) * coeff;
  53.         }
  54.     }
  55.  
  56.     if (*el(n, n - 1, n - 1, a) == 0.0)
  57.         return 0;
  58.     *(x + n - 1) = *(b + n - 1) / *el(n, n - 1, n - 1, a);
  59.     for (int i = n - 2; i >= 0; i--) {
  60.         double y = 0.0;
  61.         for (int j = i + 1; j < n; j++)
  62.             y += *el(n, i, j, a) * *(x + j);
  63.  
  64.         *(x + i) = (*(b + i) - y) / *el(n, i, i, a);
  65.     }
  66.  
  67.     return 1;
  68. }
  69.  
  70. int main(int argc, char *argv[]) {
  71.     double df[4], f[2], mods[2];
  72.     double x0, y0; // approximation
  73.  
  74.     mods[0] = 0.0; // set mods to zero
  75.     mods[1] = 0.0;
  76.  
  77.     printf("Начальное приближение x = "); scanf("%lf", &x0);
  78.     printf("Начальное приближение y = "); scanf("%lf", &y0);
  79.  
  80.     getchar();
  81.     do {
  82.         printf("поправка к x = %.16e\nпоправка к y = %.16e\n", mods[0], mods[1]);
  83.         getchar();
  84.        
  85.         // calculate df matrix
  86.         *(df + 0) = log(x0) + 1;
  87.         *(df + 1) = log(y0) + 1;
  88.         *(df + 2) = pow(x0, -(2.0 / 3.0)) / 3;
  89.         *(df + 3) = pow(y0, -(2.0 / 3.0)) / 3;
  90.  
  91.         // calculate f matrix
  92.         *(f + 0) = -(x0 * log(x0) + y0 * log(y0) - 1);
  93.         *(f + 1) = -(cbrt(x0) + cbrt(y0) - 2);
  94.  
  95.         if (!linsys(2, df, f, mods)) {
  96.             printf("Несовместная система уравнений\n");
  97.             return 1;
  98.         }
  99.  
  100.         x0 += mods[0];
  101.         y0 += mods[1];
  102.     } while (sqrt(pow(mods[0], 2) + pow(mods[1], 2)) > e);
  103.  
  104.     printf("x = %.16e\ny = %.16e\n", x0, y0);
  105.  
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment