HW6%20Solutions - Problem 3 Option Explicit ' The main...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: Problem 3 Option Explicit ' The main program reads the matrix A and vector b, calls the ' subroutine Gauss to solve the system of equations Ax = b, ' and saves the results in the worksheet. Sub Gaussian_Elimination() Dim i As Integer, J As Integer, k As Integer, n As Integer Dim A() As Double, b() As Double, x() As Double n = Cells(5, 3) ReDim A(1 To n, 1 To n) As Double, b(1 To n) As Double, x(1 To n) As Double ' Read the matrix A For i = 1 To n For J = 1 To n A(i, J) = Cells(7 + i, 1 + J) Next J Next i ' Read the vector b For i = 1 To n b(i) = Cells(7 + i, 6) Next i ' Calling a subroutine that solves the system of equations Ax = b Call Gauss(n, A, b, x) ' Output of the vector x and the modified vector b For i = 1 To n Cells(7 + i, 8) = x(i) Cells(12 + i, 6) = b(i) Next i ' Output of the upper triangular matrix For i = 1 To n For J = 1 To n Cells(12 + i, 1 + J) = A(i, J) Next J Next i End Sub ' Subroutine to solve a system of linear equations Ax = b ' n = size of the system (number of equations/unknowns) ' A = n-by-n matrix (input); upper triangular matrix (output) ' b = n-component vector (input); modified on output ' x = n-component vector (output) Sub Gauss(n As Integer, A() As Double, b() As Double, x() As Double) ' Perform Gaussian elimination Dim i As Integer, J As Integer, k As Integer Dim sum As Double, coef As Double For k = 1 To n - 1 ' row k will be subtracted from all rows below it ' Perform partial pivoting before the next step of the Gaussian elimination Call pivot(A, b, n, k) For i = k + 1 To n ' i = number of a row below row k coef = A(i, k) / A(k, k) ' row k will be multiplied by coef before ' subtracting it from the i-th row ' Perform the following operation: ' row(i) - coef*row(k) -> row(i) For J = 1 To n ' j = column number A(i, J) = A(i, J) - coef * A(k, J) Next J b(i) = b(i) - coef * b(k) Next i Next k ' Gaussian elimination is done!...
View Full Document

Page1 / 13

HW6%20Solutions - Problem 3 Option Explicit ' The main...

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

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