数值计算实验报告

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

} System.out.println("【下三角矩阵】\nL = "); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) System.out.print("\t"+l[i][j]); System.out.println(); } System.out.println("【上三角矩阵】\nU = "); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) System.out.print("\t"+u[i][j]); System.out.println(); } System.out.println("【请输入右端向量b】\nb = "); for (int i = 0; i < n; i++) { b[i] = scan.nextFloat(); bb[i] = b[i]; } //处理-前代 for (int j = 0; j < n; j++) { if (l[j][j] == 0) break; y[j] = b[j]/l[j][j]; for (int i = j+1; i < n; i++) b[i] = b[i] - l[i][j]*y[j]; } System.out.println("【前代】\ny = "); for (int i = 0; i < n; i++) System.out.print("\t"+y[i]); //处理-回代 for (int j = n-1; j >= 0; j--) { if (u[j][j] == 0) break; x[j] = y[j]/u[j][j]; for (int i = 0; i < j; i++) y[i] = y[i] - u[i][j]*x[j]; } System.out.println("\n【回代】\nx = "); for (int i = 0; i < n; i++)
2.0006466
-2.9977272
3.9966254
【残差】 r= 0.0 -3.0517578E-5 -1.5258789E-5 7.6293945E-6 【解 x+z】 x= -1.0000247
1.9998885
-3.0003924
4.000582
**********************(1)********************** 【残差】 r= 1.5258789E-5 0.0 1.5258789E-5 0.0 【解 x+z】 x= -0.99996364 2.0001647
System.out.print("\t"+x[i]); /****************** (b)用双精度运算计算残差r=b-Ax *****************/ //计算残差 for (int i = 0; i < n; i++) { double temp = 0; for (int j = 0; j < n; j++) { temp += aa[i][j]*x[j]; } r[i] = bb[i] - (float)temp; } System.out.println("\n【残差】\nr = "); for (int i = 0; i < n; i++) System.out.print("\t"+r[i]); /***************** (c)通过求解线性方程组Az=r,得到“改进”解x+z *****************/ //重置a、l、u、b数组 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { a[i][j] = aa[i][j]; l[i][j] = ll[i][j]; u[i][j] = uu[i][j]; } b[i] = r[i]; } //处理-前代 for (int j = 0; j < n; j++) { if (l[j][j] == 0) break; y[j] = b[j]/l[j][j]; for (int i = j+1; i < n; i++) b[i] = b[i] - l[i][j]*y[j]; } //处理-回代 for (int j = n-1; j >= 0; j--) { if (u[j][j] == 0) break; z[j] = y[j]/u[j][j];
【实验操作过程及结果】 【请输入维数】 n=4 【请输入矩阵】 a= 21 67 88 73 76 63 7 20 0 85 56 54 19.3 43 30.2 29.4 【下三角矩阵】 L= 1.0 0.0 0.0 0.0 3.6190476 1.0 0.0 0.0 0.0 -0.47360042 1.0 0.0 0.9190476 0.10350224 0.20147139 【上三角矩阵】 U= 21.0 67.0 88.0 73.0 0.0 -179.4762 -311.4762
System.out.print("【请输入维数】\nn = "); n = scan.nextInt(); a = new float[n][n]; aa = new float[n][n];//保存a数组 m = new float[n][n]; l = new float[n][n]; ll = new float[n][n]; u = new float[n][n]; uu = new float[n][n]; b = new float[n]; bb = new float[n];//保存b数组 x = new float[n]; y = new float[n]; r = new float[n]; z = new float[n];
System.out.println("【请输入矩阵】\na = "); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { a[i][j] = scan.nextFloat(); aa[i][j] = a[i][j]; if (i == 0) { u[i][j] = a[i][j]; uu[i][j] = a[i][j]; } } l[i][i] = 1; ll[i][i] = 1; } /***************** (a)用单精度高斯消去法程序求解方程组Ax=b *****************/ //处理-高斯消去法 for (int k = 0; k < n-1; k++) { if (a[k][k] == 0) break; for (int i = k+1; i < n; i++) { m[i][k] = a[i][k]/a[k][k]; if (i > k) { l[i][k] = m[i][k]; ll[i][k] = m[i][k]; } } for (int j = k+1; j < n; j++) { for (int i = k+1; i < n; i++) { a[i][j] = a[i][j] - m[i][k]*a[k][j]; if (i <= j) { u[i][j] = a[i][j]; uu[i][j] = a[i][j]; } } }
for (int i = 0; i < j; i++) y[i] = y[i] - u[i][j]*z[j]; } System.out.println("\n【解x+z】\nx = "); for (int i = 0; i < n; i++) { x[i] += z[i]; System.out.print("\t"+x[i]); } /*************** (d)重复步骤(b)、(c)知道解没有进一步改进 **************/ for (int index = 0; index < 38888; index++) { System.out.println("\n\n**********************("+(index+1)+")**** ******************"); //计算残差 for (int i = 0; i < n; i++) { float temp = 0; for (int j = 0; j < n; j++) { temp += aa[i][j]*x[j]; } r[i] = bb[i] - (float)temp; } System.out.println("\n【残差】\nr = "); for (int i = 0; i < n; i++) System.out.print("\t"+r[i]); //重置a、l、u、b数组 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { a[i][j] = aa[i][j]; l[i][j] = ll[i][j]; u[i][j] = uu[i][j]; } b[i] = r[i]; } //处理-前代 for (int j = 0; j < n; j++) { if (l[j][j] == 0)
数值计算实验报告
Numerical Calculation [P 87 : T 2.5]
姓名:蔺越檀 班级:09 级软件 6 班 学号:200900301117 提交时间:2011-4-6
【实验题目】 用单精度高斯消去法求解方程组 Ax=b 【实验目的】 通过对方程的求教解,熟悉高斯消去法、前代、回代的方法,了解上下三角矩阵的求法,学 会计算残差,以及通过计算残差不断得到改进解,以得到较为精确的方程组的解。 【实验要求】 (a) 用单精度高斯消去法程序求解方程组 Ax=b (b) 用双精度运算计算残差 r=b-Ax (c) 通过求解线性方程组 Az=r,得到“改进”解 x+z (d) 重复步骤(b)、(c)知道解没有进一步改进 【代码及注释】 import java.util.Scanner; public class NC_2_5 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n; float a[][], aa[][], m[][], l[][], ll[][], u[][], uu[][], b[], bb[], x[], y[], r[], z[];
1.0
-244.19049
0.0 0.0 -91.51526 -61.64872 0.0 0.0 0.0 0.0042390823 【请输入右端向量 b】 b= 141 109 218 93.7 【前代】 y= 141.0
-401.2857
27.950928
0.01694Βιβλιοθήκη Baidu024
【回代】 x= -0.9998568
break; y[j] = b[j]/l[j][j]; for (int i = j+1; i < n; i++) b[i] = b[i] - l[i][j]*y[j]; } //处理-回代 for (int j = n-1; j >= 0; j--) { if (u[j][j] == 0) break; z[j] = y[j]/u[j][j]; for (int i = 0; i < j; i++) y[i] = y[i] - u[i][j]*z[j]; } System.out.println("\n【解x+z】\nx = "); for (int i = 0; i < n; i++) { x[i] += z[i]; System.out.print("\t"+x[i]); } if ((r[0]==0.0)&&(r[1]==0.0)&&(r[2]==0.0)&&(r[3]==0.0)) break;//解没有进一步改进时则退出 } } }
相关文档
最新文档