Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

1: Sử dụng phương pháp Cramer giải 2 hệ phương trình có ít nhất 5 ẩn.

Phần lập trình


#include <stdio.h>
#include <stdlib.h>
void input(int n, double **matrix) {
printf("Nhap ma tran he so:\n");
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n + 1; ++j) {
Câu scadnf("%lf", &matrix[i][j]);
}
}
}
void swapColumn(int c1, int c2, int n, double **matrix) {
for(int i = 0; i < n; ++i) {
double t = matrix[i][c1];
matrix[i][c1] = matrix[i][c2];
matrix[i][c2] = t;
}
}
double determinant(int n, double **matrix) {
if(n == 1)return matrix[0][0];
double res = 0;
double **newMatrix = (double **)malloc(sizeof(double *)*(n - 1));
for(int i = 0; i < n - 1; ++i) {
newMatrix[i] = (double *)malloc(sizeof(double)*(n - 1));
}
for(int k = 0; k < n; ++k) {
int r = 0;
for(int i = 1; i < n; ++i) {
int c = 0;
for(int j = 0; j < n; ++j) {
if(j == k)continue;
newMatrix[r][c] = matrix[i][j];
++c;
}
++r;
}
int sign;
if(k%2 == 0)sign = 1;
else sign = -1;
res = res + sign*matrix[0][k]*determinant(n - 1, newMatrix);
}
for(int i = 0; i < n - 1; ++i) {
free(newMatrix[i]);
}
free(newMatrix);
return res;
}
double cramer_method(int n, double **matrix) {
double a[n + 1];
a[0] = determinant(n, matrix);
for(int i = 0; i < n; ++i) {
swapColumn(i, n, n, matrix);
a[i + 1] = determinant(n, matrix);
swapColumn(i, n, n, matrix);
}
double x[n + 1];
for(int i = 1; i <= n; ++i) {
x[i] = a[i]/a[0];
}
printf("Nghiem cua he phuong trinh la:\n");
for(int i = 1; i <= n; ++i) {
printf("x%d = %lf\n", i, x[i]);
}
}
int main() {
int n;
printf("Nhap so an cua phuong trinh: ");
scanf("%d", &n);
double **matrix = (double **)malloc(sizeof(double *)*n);
for(int i = 0; i < n; ++i) {
matrix[i] = (double *)malloc(sizeof(double)*(n + 1));
}
input(n, matrix);
cramer_method(n, matrix);
return 0;
}
Câu 2: Sử dụng phương pháp Gauss giải 2 hệ phương trình có ít nhất 5
ẩn.
Phần lập trình
#include <stdio.h>
#include <stdlib.h>
void input(int n, double **matrix) {
printf("Nhap ma tran he so:\n");
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n + 1; ++j) {
scanf("%lf", &matrix[i][j]);
}
}
}
void display(int row, int col, double **matrix) {
for(int i = 0; i < row; ++i) {
for(int j = 0; j < col; ++j) {
printf("%lf\t", matrix[i][j]);
}
printf("\n");
}
}
void swapRow(int r1, int r2, int n, double **matrix) {
for(int i = 0; i < n + 1; ++i) {
double t = matrix[r1][i];
matrix[r1][i] = matrix[r2][i];
matrix[r2][i] = t;
}
}
void gauss_method(int n, double **matrix) {
for(int i = 0; i < n; ++i) {
for(int j = i + 1; j < n; ++j) {
if(matrix[j][i] > matrix[i][i]) {
swapRow(i, j, n, matrix);
}
}
if(matrix[i][i] == 0)break;
for(int j = i + 1; j < n; ++j) {
double t = -matrix[j][i]/matrix[i][i];
for(int k = 0; k < n + 1; ++k) {
matrix[j][k] += matrix[i][k]*t;
}
}
}
printf("Ma tran dua ve tam giac tren:\n");
display(n, n + 1, matrix);
printf("Nghiem cua he phuong trinh la:\n");
double x[n];
for(int i = n - 1; i >= 0; --i) {
x[i] = matrix[i][n];
for(int j = i + 1; j < n; ++j) {
x[i] -= x[j]*matrix[i][j];
}
x[i] /= matrix[i][i];
printf("x%d = %lf\n", i + 1, x[i]);
}

}
int main() {
int n;
printf("Nhap so an cua phuong trinh: ");
scanf("%d", &n);
double **matrix = (double **)malloc(sizeof(double *)*n);
for(int i = 0; i < n; ++i) {
matrix[i] = (double *)malloc(sizeof(double)*(n + 1));
}
input(n, matrix);
gauss_method(n, matrix);
return 0;
}

You might also like