《理学院 数值计算方法 实验二》由会员分享,可在线阅读,更多相关《理学院 数值计算方法 实验二(13页珍藏版)》请在金锄头文库上搜索。
1、实验二实验二实验实验名称名称线性方程组的数值解法姓姓 名名张见学学 号号08119054班班 级级信计(2)指指导导教教师师张昆实验实验日期日期2010-11-12成成 绩绩一、一、实验目的实验目的 1、掌握 Gauss 消去法及 Gauss 列主元消去法,能用这两种方法求解方程组; 2、掌握 Jacobi 和 G-S 迭代法,能应用 Jacobi 和 G-S 迭代法求解方程组; 1、掌握相应数值算法的程序编写; 2、理解迭代法收敛的充要条件,会判断迭代法的收敛性。二、二、实验题目实验题目 1、分别用列主元消去法与不选主元消去法求解,分析算法对结果的影响:a) =0.3 10 1559.143
2、1 5.291 6.130 12 11.2952 1211, =59.17 46.78 1 2b) =10 701 32.09999962 5 15 1 0102, =8 5.909901 5 12、给定矩阵 A=hilb(n)与向量 b, =11 21 5 1 21 31 6 1 51 61 9, =1 2 3 4 5a) 求的 LU 分解;A b) 利用的 LU 分解求解方程组:;x=b;A = 2 = 3c) 利用的 LU 分解求,n 值自己给定。A 1三、三、实验原理实验原理 1、当系数矩阵 A 的秩增广矩阵 B 的秩时,方程组无解向量;当系数矩阵 A 的秩=增广矩阵 B 的秩 d T
3、hend = Abs(a(i, k)Ni = iEnd IfNext iFor j = k To nt = a(Ni, j)a(Ni, j) = a(k, j)a(k, j) = tNext je = b(Ni)b(Ni) = b(k)b(k) = eFor i = k + 1 To ng = a(i, k) / a(k, k)For j = k To na(i, j) = a(i, j) - a(k, j) * gNext jb(i) = b(i) - b(k) * gNext i Next k x(n) = b(n) / a(n, n) For i = n - 1 To 1 Step -1
4、t = 0For j = i + 1 To nt = t + a(i, j) * x(j)Next jx(i) = (b(i) - t) / a(i, i) Next i Picture2.Print “解得结果为:“ For i = 1 To n Picture2.Print x(i) Next i End Sub (b) 不选主元消元法Private Sub Command1_Click() Dim a() As Double, b() As Double, p As Double, t As Double, d As Double, Ni As Integer, x() As Doubl
5、e Dim n As Integer, m As Integer m = 0 n = Val(InputBox(“输入线性方程组的阶数 n“) 输入系数矩阵 ReDim Preserve a(n, n) Picture1.Print “系数矩阵:“ For v = 1 To nFor w = 1 To na(v, w) = Val(InputBox(“a(“ If m Mod n = 0 Then Picture1.PrintNext w Next v Print Picture1.Print “常数向量:“ ReDim Preserve x(n) ReDim Preserve b(n) Fo
6、r v = 1 To nb(v) = Val(InputBox(输入常数向量 Next vFor k = 1 To nFor i = k + 1 To ng = a(i, k) / a(k, k)For j = k To na(i, j) = a(i, j) - a(k, j) * gNext jb(i) = b(i) - b(k) * gNext i Next k x(n) = b(n) / a(n, n) For i = n - 1 To 1 Step -1t = 0 For j = i + 1 To n t = t + a(i, j) * x(j)Next j x(i) = (b(i)
7、- t) / a(i, i) Next i Picture2.Print “解得结果为:“ For i = 1 To nPicture2.Print x(i) Next i End Sub 2) Private Sub Command1_Click() Dim a() As Double, b() As Double, t As Double, d As Double, Ni As Integer, U() As Double, L() As Double, m As Double Dim n As Integer Dim x() As Double, y() As Double m = 0
8、n = Val(InputBox(“输入线性方程组的阶数 n“) 输入系数矩阵 ReDim Preserve a(n, n) ReDim Preserve U(n, n) ReDim Preserve L(n, n) Picture1.Print “系数矩阵:“ For v = 1 To nFor w = 1 To na(v, w) = Val(InputBox(“a(“ If m Mod n = 0 Then Picture1.PrintNext w Next v ReDim Preserve b(n) Picture1.Print “常数向量:“ For v = 1 To nb(v) =
9、Val(InputBox(输入常数向量 Next v For k = 1 To nFor i = k + 1 To nm = a(i, k) / a(k, k)L(i, k) = mFor j = k To na(i, j) = a(i, j) - a(k, j) * mNext jb(i) = b(i) - b(k) * mNext i Next k For i = 1 To nFor j = i To nU(i, j) = a(i, j)Next jNext iPicture2.Print “L 为:“For i = 1 To nFor j = 1 To nL(i, i) = 1Pictu
10、re2.Print L(i, j); Space(5);Next jPicture2.PrintNext iPicture2.Print “*“Picture2.Print “U 为:“For i = 1 To nFor j = 1 To nPicture2.Print U(i, j); Space(5);Next jPicture2.PrintNext iPicture2.Print “*“ ReDim Preserve x(n) ReDim Preserve y(n) x(n) = b(n) / a(n, n) For i = n - 1 To 1 Step -1t = 0For j = i + 1 To nt = t + a(i, j) * x(j)Next jx(i) = (b(i) - t) / a(i, i) Next i For i = 1 To nPicture2.Print “X(“ “)“ x(i) Next i End Sub