Exercise6.java
资源名称:Exercise6.rar [点击查看]
上传用户:fyeng04
上传日期:2022-08-07
资源大小:1k
文件大小:1k
源码类别:
Java编程
开发平台:
Java
- class Exercise3_2{
- public static void main(String[]args){
- double x1,x2,x3,x4;
- double sum=0;
- double[][] a={{48,-24,0,-12,4},{-24,24,12,12,4},{0,6,20,2,-2},{-6,6,2,16,-2}}; //创建增广矩阵
- for(int i=1;i<4;i++) a[i][0]=a[i][0]/a[0][0]; //对矩阵进行LU分解
- for(int i=1;i<4;i++){
- for(int j=1;j<5;j++){
- if(i<=j){
- for(int k=0;k<i;k++) sum=sum+a[k][j]*a[i][k];
- a[i][j]=a[i][j]-sum;
- sum=0;
- }
- else{
- for(int k=0;k<j;k++) sum=sum+a[k][j]*a[i][k];
- a[i][j]=(a[i][j]-sum)/a[j][j];
- sum=0;
- }
- }
- }
- x4=a[3][4]/a[3][3]; //求解同解三角矩阵
- x3=(a[2][4]-x4*a[2][3])/a[2][2];
- x2=(a[1][4]-x4*a[1][3]-x3*a[1][2])/a[1][1];
- x1=(a[0][4]-x4*a[0][3]-x3*a[0][2]-x2*a[0][1])/a[0][0];
- System.out.println("x1= "+x1);
- System.out.println("x2= "+x2);
- System.out.println("x3="+x3);
- System.out.println("x4="+x4);
- }
- }