Name: ឡៅ ជីងសាន (Lao Chingsan)
Class: ITE_M6_G8
Instructions
I. ចូរសរសេរកូដ ដើម្បីគណនានិងដោះស្រាយ៖
Output :
១) ដេទែមីណង់ (Determinant)
២) ម៉ាទ្រីសច្រាស (Inverse matrix)
៣) Gaussian elimination
៤) Iteration method
២) ម៉ាទ្រីសច្រាស (Inverse matrix)
៣) Gaussian elimination
៤) Iteration method
1. Determinant with C++ :
//Lao Chingsan
//Determinant with c++
#include <iostream>
using namespace std;
//Cofactor Function
void getCofactor(int mat[20][20], int temp[20][20], int p,int q, int n){
int i = 0, j = 0;
for (int row = 0; row < n; row++){
for (int col = 0; col < n; col++){
if (row != p && col != q){
temp[i][j++] = mat[row][col];
if (j == n - 1){
j = 0;
i++;
}
}
}
}
}
//Determinant Function
float detMat(int A[20][20], int n)
{
int D = 0;
if (n == 1)
return A[0][0];
int temp[20][20];
int sign = 1;
for (int f = 0; f < n; f++) {
getCofactor(A, temp, 0, f, n);
D += sign * A[0][f] * detMat(temp, n - 1);
sign = -sign;
}
return D;
}
//Input Function
void inputmatrix(int mat[20][20],int n){
cout<<"\nInput element of matrix A["<<n<<"]["<<n<<"] : "<<endl;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cout <<"A["<<i<<"]["<<j<<"] : ";cin>>mat[i][j];
}
}
}
//Main Function
int main (){
int n, i, j;
int a[20][20];
cout<<"\nInput square matrix : ";cin>>n;
inputmatrix(a,n);
cout<<"\nMatrix A["<<n<<"]["<<n<<"] :\n\n";
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cout<<a[i][j] << "\t";
}
cout << endl;
}
cout<<"\nDeterminant = "<<detMat(a,n)<<endl;
}
Output :
Input square matrix : 3
Input element of matrix A[3][3] :
A[0][0] : 1
A[0][1] : 1
A[0][2] : 1
A[1][0] : 1
A[1][1] : -1
A[1][2] : 2
A[2][0] : 2
A[2][1] : 1
A[2][2] : -1
Matrix A[3][3] :
1 1 1
1 -1 2
2 1 -1
Determinant = 7
2. Inverse Matrix with C++ :
//Lao Chingsan
//Inverse Matrix with C++
#include <iostream>
using namespace std;
//Cofactor Function
void getCofactor(int mat[20][20], int temp[20][20], int p,int q, int n){
int i = 0, j = 0;
for (int row = 0; row < n; row++){
for (int col = 0; col < n; col++){
if (row != p && col != q){
temp[i][j++] = mat[row][col];
if (j == n - 1){
j = 0;
i++;
}
}
}
}
}
//Determinant Function
float detMat(int A[20][20], int n)
{
int D = 0;
if (n == 1)
return A[0][0];
int temp[20][20];
int sign = 1;
for (int f = 0; f < n; f++) {
getCofactor(A, temp, 0, f, n);
D += sign * A[0][f] * detMat(temp, n - 1);
sign = -sign;
}
return D;
}
//Adjoint Function
int adjoint(int adj[20][20],int A[20][20],int n)
{
if (n == 1) {
adj[0][0] = 1;
}
int sign = 1, temp[20][20];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
getCofactor(A, temp, i, j, 20);
sign = ((i + j) % 2 == 0) ? 1 : -1;
adj[j][i] = (sign) * (detMat(temp, n - 1));
}
}
}
//Input Function
void inputmatrix(int mat[20][20],int n){
cout<<"\nInput element of matrix A["<<n<<"]["<<n<<"] : "<<endl;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cout <<"A["<<i<<"]["<<j<<"] : "; cin>>mat[i][j];
}
}
}
//Inverse Function
float invmat(int mat[20][20],float n){
float a;
float invmatrix[20][20];
int temp[20][20];
if (detMat == 0){
cout<<"No Inverse Matrix"<<endl;
return false;
}else{
a = detMat(mat,n);
adjoint(temp,mat,n);
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
invmatrix[i][j] = temp[i][j]/a;
}
}
cout<<"\nInverse matrix : \n";
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cout<<invmatrix[i][j]<<"\t";
}
cout<<endl;
}
}
}
//Main Function
int main (){
int n,i,j;
int a[20][20],adjj[20][20];
cout<<"\nInput square matrix : ";cin>>n;
inputmatrix(a,n);
cout<<"\nMatrix A["<<n<<"]["<<n<<"] :\n";
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cout<<a[i][j] << "\t";
}
cout << endl;
}
cout<<"\nDeterminant = "<<detMat(a,n)<<endl;
adjoint(adjj,a,n);
cout<<"\nAdjoint : "<<endl;
for (i = 0 ; i < n;i++){
for(j = 0; j < n ;j++){
cout<<adjj[i][j]<<"\t";
}
cout<<endl;
}
invmat(a,n);
return 0 ;
}
Input square matrix : 3
Input element of matrix A[3][3] :
A[0][0] : 1
A[0][1] : 1
A[0][2] : 1
A[1][0] : 1
A[1][1] : -1
A[1][2] : 2
A[2][0] : 2
A[2][1] : 1
A[2][2] : -1
Matrix A[3][3] :
1 1 1
1 -1 2
2 1 -1
Determinant = 7
Adjoint :
-1 2 3
5 -3 -1
3 1 -2
Inverse matrix :
-0.142857 0.285714 0.428571
0.714286 -0.428571 -0.142857
0.428571 0.142857 -0.285714
3. Gaussian elimination with C++ :
//Lao Chingsan
//Code to find root
//Gaussian elimination with C++
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
//Display function
void display(vector< vector<double> > a) {
int n = a.size();
for (int i=0; i<n; i++) {
for (int j=0; j<n+1; j++) {
cout << a[i][j] << "\t";
if (j == n-1) {
cout << "| ";
}
}
cout << "\n";
}
cout << endl;
}
//Gaussian Function
vector<double> gaussian(vector< vector<double> > a) {
int n = a.size();
for (int i=0; i<n; i++) {
double max_el = abs(a[i][i]);
int max_row = i;
for (int k=i+1; k<n; k++) {
if (abs(a[k][i]) > max_el) {
max_el = abs(a[k][i]);
max_row = k;
}
}
for (int k=i; k<n+1;k++) {
double temp = a[max_row][k];
a[max_row][k] = a[i][k];
a[i][k] = temp;
}
for (int k=i+1; k<n; k++) {
double c = -a[k][i]/a[i][i];
for (int j=i; j<n+1; j++) {
if (i==j) {
a[k][j] = 0;
} else {
a[k][j] += c * a[i][j];
}
}
}
}
vector<double> x(n);
for (int i=n-1; i>=0; i--) {
x[i] = a[i][n]/a[i][i];
for (int k=i-1;k>=0; k--) {
a[k][n] -= a[k][i] * x[i];
}
}
return x;
}
//Main function
int main() {
int n;
cout << "\nInput square matrix : ";cin >> n;
vector<double> line(n+1,0);
vector< vector<double> > a(n,line);
cout<<"\nInput element of matrix A["<<n<<"]["<<n<<"] :\n\n";
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
cout <<"A["<<i<<"]["<<j<<"] : "; cin >> a[i][j];
}
}
cout<<"\nInput value of ";
for (int k=1; k<=n; k++){
cout << "x" << k << ", ";
}
cout << " : \n";
for (int i=0, k=1; i<n, k<=n; i++, k++) {
cout << "x" << k << " : "; cin >> a[i][n];
}
cout<<"\nMatrix a :\n\n";
display(a);
vector<double> x(n);
x = gaussian(a);
cout << "Root =\t";
for (int i=0; i<n; i++) {
cout << x[i] << ", ";
}
cout << endl;
}
Output :
Input square matrix : 3
Input element of matrix A[3][3] :
A[0][0] : 1
A[0][1] : 1
A[0][2] : 1
A[1][0] : 1
A[1][1] : -1
A[1][2] : 2
A[2][0] : 2
A[2][1] : 1
A[2][2] : -1
Input value of x1, x2, x3, :
x1 : 7
x2 : 9
x3 : 1
Matrix a :
1 1 1 | 7
1 -1 2 | 9
2 1 -1 | 1
Root = 2, 1, 4,
4. Iteration method with C++:
4.1 Iteration with Jacobi method :
//Iterative to find root with Jacobi method
#include<iostream>
using namespace std;
//Jacobi Function
void jac(int n)
{
float x=0, y = 0;
float tmp = 0;
for (int i = 0; i < n; i++)
{
tmp = -1.0 / 2 * y + 2;
y = 1.0 / 3 * x + 5.0 / 3;
x = tmp;
cout <<"\tx: " << x << "\t\t\t" << "y: " << y << endl;
}
}
//Main Function
int main()
{
int rep;
cout << "input the number of repitition: "; cin >> rep;
cout << "\nIteration with Jacobi method :\n\n";
jac(rep);
cout << endl;
return 0;
}
Output :
input the number of repitition: 10
Iteration with Jacobi method :
x: 2 y: 1.66667
x: 1.16667 y: 2.33333
x: 0.833333 y: 2.05556
x: 0.972222 y: 1.94444
x: 1.02778 y: 1.99074
x: 1.00463 y: 2.00926
x: 0.99537 y: 2.00154
x: 0.999228 y: 1.99846
x: 1.00077 y: 1.99974
x: 1.00013 y: 2.00026
4.2 Iteration with Gauss-Seidel method :
//Iterative to find root with Gauss-Seidel method
#include<iostream>
using namespace std;
//Gauss_Seidel Function
void gs(int rep)
{
float x = 0, y = 0;
for (int i = 0; i < rep; i++)
{
x = -1.0 / 2 * y + 2;
y = 1.0 / 3 * x + 5.0 / 3;
cout << "\tx:" << x << "\t\t\ty: " << y << endl;
}
}
//Main Function
int main(){
int rep;
cout << "Input the number of repitition: "; cin >> rep;
cout << "\nIteration with Gauss-Seidel Method\n\n";
gs(rep);
return 0;
}
Output :
Input the number of repitition: 5
Iteration with Gauss-Seidel Method
x:2 y: 2.33333
x:0.833333 y: 1.94444
x:1.02778 y: 2.00926
x:0.99537 y: 1.99846
x:1.00077 y: 2.00026
Thank you!
Enjoy your day.
Comments
Post a Comment