Download presentation
Presentation is loading. Please wait.
1
Algorithms and Programming
Dr. Theodore Cleveland University of Houston CIVE 1331 – Computing for Engineers Lecture 011 NEXT LECTURE: Powerpoint, EXCLE
2
Iterative Method for Solving Systems of Linear Equations
In certain cases iteration methods are preferred over direct methods When the coefficient matrix is sparse (many zeroes) the methods can be more rapid. Iterative methods are almost always more economical in the amount of computer memory required.
3
Iterative Method Jacobi Method Inefficient in terms of speed. Robust
Simple to Implement Scales very well May not always work (like other Iterative methods) By Scales very well, we mean that it works well with large linear systems
4
Jacobi Example The best way to explain the Jacobi Method is with an example: Given the following system whose solution is (1,1,1): 8x1 + 1x2 + 1x3 = 10 2x1 + 1x2 + 9x3 =12 1x1 - 7x2 + 2x3 = -4 1) Rearrange system so that diagonal coefficients are as large as possible:
5
Jacobi Example 2) Then we rewrite each row in terms of a single variable: x1 = x x x2 = x x x3 = x x 3) We then create an initial guess of (x1, x2, x3)= (0, 0, 0), and evaluate to find the each of the equations to get the next estimation. x1 = * * x2 = * * x3 = * * or (x1, x2, x3)= (1.25, 0.571, 1.333) 4) We repeat step 3 with our new estimate of the solution, the result will eventually converge to (1, 1, 1)
6
Gauss-Seidel Iterative Method
A modification of the Jacobi method, called Gauss-Seidel, uses the new variable estimates as soon as they are calculated which dramatically speeds the process. For example: x1 = (0.00) (0.00) = 1.25 x2 = (1.25) (0) = x3 = (1.25) (.75) = 0.972
7
Spreadsheet Implementation
The Jacobi method can be rather simply implemented with an Excel Spreadsheet. The following Excel Worksheet solves the Linear system {8x1 + 1x2 + 1x3 = 8, 1x1 - 7x2 + 2x3 = -4, 2x1 + 1x2 + 9x3 =12} Discussions: For simplification we use matrix multiplication Matrix multiplication is used to simplify the calculation of the updates, although the formulas could be “hard-wired” into each cell. We will use the Jacobi Method to solve potential flow problems
8
Fortran Implementation
We are about to view a Fortran program that implements the Jacobi method. For simplicity, the code is somewhat crude because it does not stop calculations when a convergence is met but rather after a set number of iterations. You are not expected to know how to write the Fortran code yet, it is being presented now to give you an idea of how you might write a program in a procedural language such as Fortran. (Next Slide contains code)
9
Fortran Implementation of Jacobi Method
program jacobi_1 implicit real*8 (a-h,o-z) parameter (np=10) dimension a(np,np),x(np),b(np) open(unit=11,status='unknown',file='input.dat') open(unit=12,status='unknown',file='output.dat') rewind(11) rewind(12) read(11,*)n write(12,*)'num. equations = ',n write(12,*)'input matrix' do 10 ir=1,n read(11,*)(a(ir,jc),jc=1,n) write(12,1001)(a(ir,jc),jc=1,n) 10 continue write(12,*)'input rhs' do 20 ir=1,n read(11,*)b(ir) x(ir)=0. 20 continue write(12,111)(b(ir),ir=1,n) c normalize matrix and rhs do 30 ir=1,n temp=a(ir,ir) b(ir)=b(ir)/temp do 31 jc=1,n a(ir,jc)=a(ir,jc)/temp 31 continue 30 continue c put zeros on main diagonal write(12,*)'normalized matrix' do 32 ir=1,n a(ir,ir)=0.d0 write(12,111)(a(ir,jc),jc=1,n) 32 continue write(12,*)'normalized rhs' write(12,111)(b(ir),ir=1,n) c begin iterations do 40 k=1,100 do 51 ir=1,n sum=0.d0 do 52 jc=1,n sum=sum-a(ir,jc)*x(jc) continue x(ir)=b(ir)+sum 51 continue 40 continue c write solution write(12,*)'solution vector' do 60 ir=1,n write(12,110)x(ir) 60 continue close(11) close(12) stop 110 format(1x,f13.6) 111 format(12(1x,f6.2)) end Discussion Note that the numbers 1 through 58 are not part of the actual fortran code, they are included so I can refer to a line of code. In lines 2 through 5, we declare 8 bit precision-reals, a-h,o-z we set a parameter np(for number of parameters?) equal to 10 - this is the number of unknowns we will solve for Finally we create the matricies a and x as 10x10 and b as a 1x10 matricies of reals Line 5 and 6 open input and output data files - Note that we will use the number 11 to refer to the input file and 12 to refer to the output file. Lines 7 through 14 read in the number of equations(also variables) into the matrix a, and writes a copy of the matrix to the output file Line 15 thorugh 20 reads in the “Right Hand Side” of the matrix equation into the matrix b. At the same time we are also setting the initial value of the x vector to zero. Line 21 writes the matrix b to the output file Lines 23 through 29 normalizes the matrix, by this we mean we divide each element in the matrix by the value of the element in the diagonal. (Note that we did this for the coefficient matrix A, as well as the right hand side matrix B. *Note that for the Jacobi method we would like to interchange rows so that the diagaonals are dominant, for now we will assume the input matrix was already organized as such. In lines 31 through 35 we are doing two things at once, we are writing the normalized matrix to the output file,in the same loop, we are setting each diagonal value to 0. Lines 39 trhough 47 perform 100 iterations of the matrix multiplication. Note that the loop on line 39 controls the number of iterations. The loop on line 40 runs through each row of our matrix, this represents a single equation. The loop on line 42 runs through each term in each equation. Line 49 through 52 writes out the solution Line 53,54 close the data files Lines 56,57 are used to define the format of our output data The following comments removed from header: c example program to solve linear system by jacobi iteration c assumes coefficient array is diagionally dominant c assumes that matrix is dense (few zeroes)
10
Sample Input To get you familiar with input and output in Fortran we are going to look at the input file and the code sections of our program used to read the corresponding input data. 3 8. -4. 12. Input File: Fortran Code: read(11,*)n do 10 ir=1,n read(11,*)(a(ir,jc),jc=1,n) 10 continue do 20 ir=1,n read(11,*)b(ir) 20 continue Note that Line a reads data line 1. Correspondingly each of the n iterations of line c reads one row of lines 2 through 4, in this case n equals 3 Line f reads a single value from the input file, But again we note that it is executed 3 times as per line e.
11
Sample Output Fortran code:
We can also compare the output data with the corresponding Fortran code write(12,*)'num. equations = ',n write(12,*)'input matrix‘ do 10 ir=1,n write(12,111)(a(ir,jc),jc=1,n) 10 continue write(12,*)'input rhs' write(12,111)(b(ir),ir=1,n) write(12,*)'normalized matrix' do 32 ir=1,n write(12,1101)(a(ir,jc),jc=1,n) 32 continue write(12,*)'normalized rhs' write(12,*)'solution vector' do 60 ir=1,n write(12,110)x(ir) 60 continue 110 format(1x,f13.6) 111 format(12(1x,f6.2)) num. equations = input matrix input rhs normalized matrix normalized rhs solution vector Sample Output: The practice of going through this code/output is left to student. Note however that instead of using the default formating “*” we use 110 and 111 format codes defined at the end of the file.
12
Numerical Integration
Often Integration can not be easily determined analytically Numerical Methods provide an approximation to the analytical value Riemann Sums (Rectangular Method) Trapezoid Method Simpson’s Rule
13
Riemann Sums Refers to approximating the Integral by breaking the area under the integral into rectangles of some small width, Dx, and a height f(x). Discussion: This approximation should be intuitive given that we know the limit as (x->0) of f(x)dx is By definition the integral of f(x) In some cases a Riemann sum is computed using the left edge of the rectangle, in others a right edge is used. In general the most accurate approximation however will be the midpoint between xn and xn+1. In this class the Rectangular method will refer to using the midpoint of the rectangle
14
Excel Implementation of Integration by Riemann Sums
In the example to the right, the algorithm for calculating the Integral of from 0 to 2 is approximated using an Excel spreadsheet in four iterations to The exact solution is The exact solution is
15
Increased Iterations for accuracy
Riemann Sum approximation with 40 iterations ( ) Riemann Sum approximation with 4000 iterations ( ) Recall exact solution is
16
Trapezoid Method A better yet approximation can be made using the trapezoid method, in which the sub-sections of the Integral are approximated by trapezoids. The important distinction between the Rectangular method and Trapezoid method is that in the Rectangular methods we average the x values and find “f of x” of the average. In the Trapazoid method we find “f of x” of both edges and then those values.
17
Excel Implementation of Integration by Trapezoid
In the example to the right, the algorithm for calculating the Integral of from 0 to 2 is approximated using an Excel spreadsheet in four iterations to The exact solution is The exact solution is Riemann Sum approximation with 4 iterations ( )
18
Increased Iterations for accuracy
Riemann Sum approximation with 40 iterations ( ) Riemann Sum approximation with 4000 iterations ( ) Recall exact solution is
19
Parabolic Panels In Trapezoidal method we used linear interpolation to approximate the area under the curve using two points, the upper left corner (x , f(x)) and the upper right corner(x+Dx , f(x+ Dx)) of DA. We could also choose to approximate the curve with a quadratic function. A method that uses a quadratic panels to do such an integration is called Simpson’s Rule (sometimes called Simpson’s 1/3rd Rule) Discussion: If Linear interpolation requires a minimum of 2 points, how many points are required for a quadratic interpolation? ANS) 3
20
Excel Implementation of Integration by Riemann Sums
In the example to the right, the algorithm for calculating the Integral of from 0 to 2 is approximated using an Excel spreadsheet in four iterations to The exact solution is The exact solution is
21
Increased Iterations for accuracy
Riemann Sum approximation with 40 iterations ( ) Riemann Sum approximation with 4000 iterations ( ) Recall exact solution is
22
Simpson’s Rule If we pick the three points, needed for the quadratic approximation as x1, (x1+ Dx), and (x1 + 2*Dx), then Simpson’s rule simplifies the process of finding the equation for quadratic equation passing through those points and the integration of that equation into the relatively simple formula: Note that in our diagram the points are separated by the same distance Dx. This is not a limitation with using Quadratic panels, but it is required in order to use the simplification to quadratic panes that Simpson’s Rule provides. Also note that the second equation in the diagram is a general formula obtained by combining several applications of Simpson’s rule into one equation. Finally note that A single application of Simpson’s rule uses three points, or two panels. Two applications use 5 points or 4 panels. DA = (1/3){ f(x1) + 4*f(x1+Dx) + f(x1+2Dx) }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.