Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdlib.h"
- #include "stdio.h"
- #include "math.h"
- float* Jacobi(int size, float** A, float eps)
- {
- float* x = (float*)malloc(sizeof(float) * size);
- float* tempX = (float*)malloc(sizeof(float) * size);
- for (int i = 0; i < size; i++)
- x[i] = tempX[i] = 0;
- float norm;
- do {
- for (int i = 0; i < size; i++) {
- tempX[i] = A[i][size];
- for (int g = 0; g < size; g++) {
- if (i != g)
- tempX[i] -= A[i][g] * x[g];
- }
- tempX[i] /= A[i][i];
- }
- norm = fabs(x[0] - tempX[0]);
- for (int h = 0; h < size; h++) {
- if (fabs(x[h] - tempX[h]) > norm)
- norm = fabs(x[h] - tempX[h]);
- x[h] = tempX[h];
- }
- } while (norm > eps);
- free(tempX);
- return x;
- }
- int main(int argc, char** argv)
- {
- int size = 0;
- do {
- printf("Введите порядок матрицы:\n");
- scanf("%d", &size);
- } while (size < 1);
- float** a = (float**)malloc(sizeof(void*) * size);
- printf("Введите матрицу:\n");
- for (int i = 0; i < size; i++)
- {
- a[i] = (float*)malloc(sizeof(float) * (size + 1));
- for (int j = 0; j <= size; j++)
- scanf("%f", &a[i][j]);
- }
- printf("\n");
- float* x = Jacobi(size, a, 0.001f);
- for (int i = 0; i < size; i++)
- {
- printf("x[%d]=%f\n", i + 1, x[i]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment