





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Codes for numerical analysis equations c++
Typology: Essays (university)
1 / 9
This page cannot be seen from the preview
Don't miss anything!






#include
float x1, x2, x3; x3 = c3 / matrix[2][2]; x2 = (c2 - (x3matrix[1][2])) / matrix[1][1]; x1 = (c1 - (x3matrix[0][2]) - (x2*matrix[0][1])) / matrix[0][0]; cout << "X1= " << x1 << endl; cout << "X2= " << x2 << endl; cout << "X3= " << x3 << endl; cout << endl; } float det(float matrix[3][4]) { //det of any matrix float result = ( matrix[0][0] * (matrix[1][1] * matrix[2][2] - (matrix[1][2] * matrix[2][1])) - matrix[0][1] * ((matrix[1][0] * matrix[2][2]) - (matrix[1][2] * matrix[2][0])) + matrix[0][2] * ((matrix[1][0] * matrix[2][1]) - (matrix[1][1] * matrix[2][0])) ); return result; } void swappy(float &a, float &b) { //important to swap columns in A1,A2,A float temp = a; a = b; b = temp; } void Cramer(float matrix[3][4]) { float A1, A2, A3; cout << "A Matrix \n"; printMatrix(matrix, 3, 3); float A = det(matrix); cout << "\nDetriment of A = " << A << endl; //to find matrix A for (int i = 0; i < 3; i++) {//swap First column with the Last Column (output column - >b) swappy(matrix[i][0], matrix[i][3]); } cout << "Matrix of A1 \n"; printMatrix(matrix, 3, 3); A1 = det(matrix);
cout << "\n\nDetriment of A1 = " << A1 << endl; //to find matrix A for (int i = 0; i < 3; i++) {//swap second column with the Last Column (output column - >b) swappy(matrix[i][0], matrix[i][3]); //revert the changes we made in the first swap swappy(matrix[i][1], matrix[i][3]); //then swap second column with the Last Column (output column - >b) } cout << "Matrix of A2 \n"; printMatrix(matrix, 3, 3); A2 = det(matrix); cout << "\n\nDetriment of A2 = " << A2 << endl; //to find matrix A for (int i = 0; i < 3; i++) { swappy(matrix[i][1], matrix[i][3]);//revert the changes we made in the second swap swappy(matrix[i][2], matrix[i][3]);//then swap second column with the Last Column (output column) } cout << "Matrix of A3 \n"; printMatrix(matrix, 3, 3); A3 = det(matrix); cout << "\n\nDetriment of A3 = " << A3 << endl; //to find x1,x2,x float x1 = A1 / A; float x2 = A2 / A; float x3 = A3 / A; cout << "X1= " << x1 << endl; cout << "X2= " << x2 << endl; cout << "X3= " << x3 << endl; cout << endl; //:) } void main() { float matrix[3][4]; char c, option; do { cout << "Please enter the Augmented Matrix\n"; for (int i = 0; i < 3; i++) { cout << "enter " << i + 1 << " Row\n"; for (int j = 0; j < 4; j++) { cin >> matrix[i][j];