C ++程序实现高斯约旦消除
这是一个实现高斯约旦消除的C++程序。用于分析联立方程的线性系统。它主要集中在通过行运算将方程组简化为对角矩阵形式,以便直接获得解。
算法
Begin
n = size of the input matrix
To find the elements of the diagonal matrix:
Make nested for loops j = 0 to n and i = 0 to n
The element in the first row and the first column is made 1
and then the remaining elements in the first column are made 0.
Similarly, the elements in the second row and the second
column is made 1, and then the other elements in the second
column are reduced to 0 and so on.
Print all calculated solution values.
End示例
#include<iostream>
using namespace std;
int main() {
int i,j,k,n; // declare variables and matrixes as
input
float a[10][10],b,x[10];
printf("\nEnter the size of matrix: ");
scanf("%d",&n);
printf("\nEnter the elements of augmented matrix (rowwise):\ n");
for(i=1; i<=n; i++) {
for(j=1; j<=(n+1); j++) {
cout << "A[" << i << ", " << j << " ]=";
cin >> a[i][j];
}
}
//找到对角矩阵的元素
for(j=1; j<=n; j++) {
for(i=1; i<=n; i++) {
if(i!=j) {
b=a[i][j]/a[j][j];
for(k=1; k<=n+1; k++) {
a[i][k]=a[i][k]-b*a[j][k];
}
}
}
}
cout<<"\nThe solution is:\n";
for(i=1; i<=n; i++) {
x[i]=a[i][n+1]/a[i][i];
cout<<"x"<<i << "="<<x[i]<<" ";
}
return(0);
}输出结果
Enter the size of matrix: 3 Enter the elements of augmented matrix row-wise: A[1, 1 ]=1 A[1, 2 ]=2 A[1, 3 ]=-4 A[1, 4 ]=2 A[2, 1 ]=7 A[2, 2 ]=6 A[2, 3 ]=-2 A[2, 4 ]=-5 A[3, 1 ]=0 A[3, 2 ]=-3 A[3, 3 ]=-5 A[3, 4 ]=-8 The solution is: x1=-2.89831 x2=2.5678 x3=0.059322
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短