Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define e 1e-10
- double *el(int n, int i, int j, double *a) {
- return a + i * n + j;
- }
- int maxLine(int n, int i, int j, double *a) {
- double max = *el(n, i, j, a);
- int maxi = i;
- for (int k = i; k < n; k++)
- if (fabs(*el(n, k, j, a)) > fabs(max)) {
- max = *el(n, k, j, a);
- maxi = k;
- }
- if (max == 0.0)
- return -1;
- else
- return maxi;
- }
- void swapLines(int n, double *a, double *b, int l, int m) {
- for (int j = 0; j < n; j++) {
- double t = *el(n, l, j, a);
- *el(n, l, j, a) = *el(n, m, j, a);
- *el(n, m, j, a) = t;
- }
- double t = *(b + l);
- *(b + l) = *(b + m);
- *(b + m) = t;
- }
- int linsys(int n, double *a, double *b, double *x) {
- for (int i = 0; i < n - 1; i++) { // for all lines excluding the last...
- int maxl = maxLine(n, i, i, a); // ...we find main item in column and then...
- if (maxl == -1)
- return 0;
- if (maxl > *el(n, i, i, a))
- swapLines(n, a, b, i, maxl);
- for (int k = i + 1; k < n; k++) { // ...we will set all items from current column to zero excluding current line
- double coeff = -(*el(n, k, i, a)) / *el(n, i, i, a);
- for (int l = i; l < n; l++)
- *el(n, k, l, a) += *el(n, i, l, a) * coeff;
- *(b + k) += *(b + i) * coeff;
- }
- }
- if (*el(n, n - 1, n - 1, a) == 0.0)
- return 0;
- *(x + n - 1) = *(b + n - 1) / *el(n, n - 1, n - 1, a);
- for (int i = n - 2; i >= 0; i--) {
- double y = 0.0;
- for (int j = i + 1; j < n; j++)
- y += *el(n, i, j, a) * *(x + j);
- *(x + i) = (*(b + i) - y) / *el(n, i, i, a);
- }
- return 1;
- }
- int main(int argc, char *argv[]) {
- double df[4], f[2], mods[2];
- double x0, y0; // approximation
- mods[0] = 0.0; // set mods to zero
- mods[1] = 0.0;
- printf("Начальное приближение x = "); scanf("%lf", &x0);
- printf("Начальное приближение y = "); scanf("%lf", &y0);
- getchar();
- do {
- printf("поправка к x = %.16e\nпоправка к y = %.16e\n", mods[0], mods[1]);
- getchar();
- // calculate df matrix
- *(df + 0) = log(x0) + 1;
- *(df + 1) = log(y0) + 1;
- *(df + 2) = pow(x0, -(2.0 / 3.0)) / 3;
- *(df + 3) = pow(y0, -(2.0 / 3.0)) / 3;
- // calculate f matrix
- *(f + 0) = -(x0 * log(x0) + y0 * log(y0) - 1);
- *(f + 1) = -(cbrt(x0) + cbrt(y0) - 2);
- if (!linsys(2, df, f, mods)) {
- printf("Несовместная система уравнений\n");
- return 1;
- }
- x0 += mods[0];
- y0 += mods[1];
- } while (sqrt(pow(mods[0], 2) + pow(mods[1], 2)) > e);
- printf("x = %.16e\ny = %.16e\n", x0, y0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment