Afficher les solutions pour les variables
Dans cette étape, vous allez terminer la mise en œuvre de la règle de Cramer en calculant et en affichant les solutions pour les variables d'un système d'équations linéaires.
Ouvrez le fichier source existant :
cd ~/project
nano cramer_solver.c
Mettez à jour le code avec le calcul des solutions selon la règle de Cramer :
#include <stdio.h>
#define MAX_SIZE 3
// La fonction computeDeterminant précédente reste la même
float computeDeterminant(float matrix[MAX_SIZE][MAX_SIZE], int n) {
if (n == 1) {
return matrix[0][0];
}
if (n == 2) {
return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
}
float det = 0;
float submatrix[MAX_SIZE][MAX_SIZE];
int sign = 1;
for (int x = 0; x < n; x++) {
int subi = 0;
for (int i = 1; i < n; i++) {
int subj = 0;
for (int j = 0; j < n; j++) {
if (j == x) continue;
submatrix[subi][subj] = matrix[i][j];
subj++;
}
subi++;
}
det += sign * matrix[0][x] * computeDeterminant(submatrix, n - 1);
sign = -sign;
}
return det;
}
// Fonction pour résoudre des équations linéaires en utilisant la règle de Cramer
void solveUsingCramersRule(float matrix[MAX_SIZE][MAX_SIZE], float constants[MAX_SIZE], int n) {
float mainDeterminant = computeDeterminant(matrix, n);
// Vérifiez si le système a une solution unique
if (mainDeterminant == 0) {
printf("The system has no unique solution (determinant is zero).\n");
return;
}
// Créez des matrices temporaires pour chaque variable
float tempMatrix[MAX_SIZE][MAX_SIZE];
float solutions[MAX_SIZE];
// Calculez les solutions pour chaque variable
for (int i = 0; i < n; i++) {
// Copiez la matrice originale
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
tempMatrix[j][k] = matrix[j][k];
}
}
// Remplacez la i-ème colonne par les constantes
for (int j = 0; j < n; j++) {
tempMatrix[j][i] = constants[j];
}
// Calculez le déterminant pour cette variable
solutions[i] = computeDeterminant(tempMatrix, n) / mainDeterminant;
}
// Affichez les solutions
printf("\nSolutions:\n");
for (int i = 0; i < n; i++) {
printf("x%d = %.2f\n", i+1, solutions[i]);
}
}
int main() {
float matrix[MAX_SIZE][MAX_SIZE];
float constants[MAX_SIZE];
int n;
// Le code d'entrée reste le même
printf("Enter the number of equations (max 3): ");
scanf("%d", &n);
// Entrez les coefficients
printf("Enter the coefficients of the matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("Enter coefficient a[%d][%d]: ", i+1, j+1);
scanf("%f", &matrix[i][j]);
}
}
// Entrez les constantes
printf("Enter the constants:\n");
for (int i = 0; i < n; i++) {
printf("Enter constant b[%d]: ", i+1);
scanf("%f", &constants[i]);
}
// Résolvez en utilisant la règle de Cramer
solveUsingCramersRule(matrix, constants, n);
return 0;
}
Compilez le programme mis à jour :
gcc -o cramer_solver cramer_solver.c
Exécutez le programme :
./cramer_solver
Exemple de sortie :
Enter the number of equations (max 3): 2
Enter the coefficients of the matrix:
Enter coefficient a[1][1]: 2
Enter coefficient a[1][2]: 1
Enter coefficient a[2][1]: -3
Enter coefficient a[2][2]: 4
Enter the constants:
Enter constant b[1]: 4
Enter constant b[2]: 5
Solutions:
x1 = 2.00
x2 = 1.00
Points clés concernant le calcul des solutions :
- La fonction
solveUsingCramersRule()
met en œuvre la règle de Cramer.
- Elle vérifie si le système a une solution unique en vérifiant le déterminant principal.
- Pour chaque variable, elle crée une matrice temporaire et calcule son déterminant.
- Les solutions sont calculées en divisant le déterminant de chaque variable par le déterminant principal.
Le programme résout maintenant entièrement un système d'équations linéaires en utilisant la règle de Cramer.