prob 6-16

prob 6-16 - If dummy > big Then big = dummy p =...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
% Himen Patel % 9-16, Numericals % Option Explicit Dim n As Integer, er As Integer, i As Integer Dim a(10, 10) As Single, b(10) As Single, x(10) As Single Range("a1").Select n = 3 a(1, 1) = 1: a(1, 2) = 1: a(1, 3) = -1 a(2, 1) = 6: a(2, 2) = 2: a(2, 3) = 2 a(3, 1) = -3: a(3, 2) = 4: a(3, 3) = 1 b(1) = 1: b(2) = 10: b(3) = 2 Call Gauss(a(), b(), n, x(), er) If er = 0 Then For i = 1 To n Next i Else MsgBox "ill-conditioned system" End If End Sub Sub Gauss(a, b, n, x, er) Dim i As Integer, j As Integer Dim s(10) As Single Const tol As Single = 0.000001 er = 0 For i = 1 To n s(i) = Abs(a(i, 1)) For j = 2 To n If Abs(a(i, j)) > s(i) Then s(i) = Abs(a(i, j)) Next j Next i Call Eliminate(a, s(), n, b, tol, er) If er <> -1 Then Call Substitute(a, n, b, x) End If End Sub Sub Pivot(a, b, s, n, k) Dim p As Integer, ii As Integer, jj As Integer Dim factor As Single, big As Single, dummy As Single p = k big = Abs(a(k, k) / s(k)) For ii = k + 1 To n dummy = Abs(a(ii, k) / s(ii))
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: If dummy &gt; big Then big = dummy p = ii End If Next ii If p &lt;&gt; k Then For jj = k To n dummy = a(p, jj) a(p, jj) = a(k, jj) a(k, jj) = dummy Next jj dummy = b(p) b(p) = b(k) b(k) = dummy dummy = s(p) s(p) = s(k) s(k) = dummy End If End Sub Sub Substitute(a, n, b, x) Dim i As Integer, j As Integer Dim sum As Single x(n) = b(n) / a(n, n) For i = n - 1 To 1 Step -1 sum = 0 For j = i + 1 To n sum = sum + a(i, j) * x(j) Next j x(i) = (b(i) - sum) / a(i, i) Next i End Sub Sub Eliminate(a, s, n, b, tol, er) Dim i As Integer, j As Integer, k As Integer Dim factor As Single For k = 1 To n - 1 Call Pivot(a, b, s, n, k) If Abs(a(k, k) / s(k)) &lt; tol Then er = -1 Exit For End If For i = k + 1 To n factor = a(i, k) / a(k, k) For j = k + 1 To n a(i, j) = a(i, j) - factor * a(k, j) Next j b(i) = b(i) - factor * b(k) Next i Next k If Abs(a(k, k) / s(k)) &lt; tol Then er = -1 End Sub...
View Full Document

This note was uploaded on 03/28/2011 for the course CAE 311 taught by Professor Himn during the Spring '11 term at SJVCS.

Page1 / 2

prob 6-16 - If dummy &amp;amp;gt; big Then big = dummy p =...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online