Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming assignment #1. Solutions and Discussion

Similar presentations


Presentation on theme: "Programming assignment #1. Solutions and Discussion"— Presentation transcript:

1 Programming assignment #1. Solutions and Discussion
Programming assignment # Solutions and Discussion Numerical Methods for PDEs Spring 2007 Jim E. Jones

2 The Problem PDE solution

3 Coding issues: storing the approximate solution u
Its easiest to store u in a two-dimensional array big enough to hold the known boundary values as well. This will allow the same update formulas to work at all grid points. There is no need for special code to deal with boundary cases. We need to work out the indexing of this array and how it relates to the physical space We can chose to have an input parameter related to the mesh size (h) or grid size (n) Let’s chose n = the number of intervals in x direction. Since we just dealt with square grids, this is also the number of intervals in y.

4 Example: n=8 Index Space Physical Space (x=2.0,y=2.0) (i=9,j=9)

5 Example: n=8 Index Space Physical Space
(x=2.0,y=2.0) (i=9,j=9) Physical Space Index Space This differs from what we had in previous lecture because we now have indexing from 1 (FORTRAN, MATLAB) rather than 0 (C). (x=1.0,y=1.0) (i=1,j=1)

6 Example: n=8 h=1/n, n=8 (x=2.0,y=2.0) (i=9,j=9)
(x=1.0+(i-1)*h,y=1.0+(j-1)*h) (i,j) h=1/n, n=8 (x=1.0,y=1.0) (i=1,j=1)

7 Code Outline HW1(n,tol,max_its) Load Boundary Values and RHS
Gauss-Seidel Loop Loop over interior (2<j<n,2<i<n) Update u(i,j) Loop over interior Calculate max residual Convergence Check Calculate max error

8 Code Outline HW1(n,tol,max_its) Load Boundary Values and RHS
Gauss-Seidel Loop Loop over interior (2<j<n,2<i<n) Update u(i,j) Loop over interior Calculate max residual Convergence Check Calculate max error

9 MATLAB code Code posted on class web site:
Note the debugging examples. Questions on coding?

10 Assignment #1 Due Jan. 29 Code up the finite difference method using Gauss-Seidel to solve the linear system as discussed in these slides. You need to be able to run different problem sizes, so write your code with h (or n) as an input argument. You should also include the Gauss-Seidel stopping criterion tol as an input argument. You can write it for general right hand side and boundary conditions, or hard code the specific example. Do not store the A matrix. Verify that your code is working – do a small problem for which you know the answer.

11 Assignment #1 Due Jan. 29 Run your code for the example problem.
Experiment by varying h and tol to see how they effect the number of iterations required and the infinity norm of the final, total error. Pick at least 3 values of h and 3 values of tol. I suggest h=.1,.05,.025 and tol=10e-2,10e-3,10e-4 this makes 9 runs total. Make table that includes the number of Gauss-Seidel iterations required for each run. Make table that includes the infinity norm of the final, total error for each run.

12 Assignment #1 Due Jan. 29 From your table of results, answer these questions: For fixed tolerance, how does the number of iterations depend on h? If h is halved does the number of iterations go up or down? By what factor? How does the error depend on h? If h is halved does the error go up or down? By what factor?

13 Investigations begin …
OK, so we’ve got input parameters to play with: n, controls how fine the mesh is, and tol, controls how well we solve the discrete system Au=b. We want to see the effect of these on the two outputs: gs_its, the number of Gauss-Seidel iterations required, and e_max, the infinity norm of the final, total error.

14 Data, data, data… GS its E max Tol n=8 n=16 n=32 n=64 1.00E+00 29 110
E max Tol n=8 n=16 n=32 n=64 1.00E+00 29 110 432 1713 3.909E-02 4.829E-02 5.004E-02 5.049E-02 1.00E-01 43 169 670 2668 4.097E-03 4.851E-03 5.019E-03 5.049E-03 1.00E-02 58 229 909 3623 2.156E-04 4.301E-04 4.889E-04 5.025E-04 1.00E-03 72 288 1147 4578 1.433E-04 8.414E-06 3.847E-05 4.761E-05 1.00E-04 87 347 1386 5534 1.826E-04 4.313E-05 7.179E-06 2.094E-06 1.00E-05 101 407 1625 6489 1.862E-04 4.756E-05 1.156E-05 2.524E-06 1.00E-06 116 466 1863 7444 1.866E-04 4.799E-05 1.200E-05 2.968E-06 1.00E-07 130 525 2102 8399 1.867E-04 4.803E-05 1.204E-05 3.012E-06 1.00E-08 145 585 2340 9355 1.205E-05 3.017E-06 1.00E-09 159 644 2579 10311

15 Slicing and dicing data: its vs. tol
GS its E max Tol n=8 n=16 n=32 n=64 1.00E+00 29 110 432 1713 3.909E-02 4.829E-02 5.004E-02 5.049E-02 1.00E-01 43 169 670 2668 4.097E-03 4.851E-03 5.019E-03 5.049E-03 1.00E-02 58 229 909 3623 2.156E-04 4.301E-04 4.889E-04 5.025E-04 1.00E-03 72 288 1147 4578 1.433E-04 8.414E-06 3.847E-05 4.761E-05 1.00E-04 87 347 1386 5534 1.826E-04 4.313E-05 7.179E-06 2.094E-06 1.00E-05 101 407 1625 6489 1.862E-04 4.756E-05 1.156E-05 2.524E-06 1.00E-06 116 466 1863 7444 1.866E-04 4.799E-05 1.200E-05 2.968E-06 1.00E-07 130 525 2102 8399 1.867E-04 4.803E-05 1.204E-05 3.012E-06 1.00E-08 145 585 2340 9355 1.205E-05 3.017E-06 1.00E-09 159 644 2579 10311

16 Number of iterations vs. tolerance

17 Number of iterations vs. tolerance
For fixed n, each time we reduce the tolerance by an order of magnitude, the required iterations goes up by a constant amount. N= more iterations to get one more order reduction N= N= N= Looks like halving h makes the number of iterations to get an order reduction go up by 4.

18 Slicing and dicing data: its vs. n
GS its E max Tol n=8 n=16 n=32 n=64 1.00E+00 29 110 432 1713 3.909E-02 4.829E-02 5.004E-02 5.049E-02 1.00E-01 43 169 670 2668 4.097E-03 4.851E-03 5.019E-03 5.049E-03 1.00E-02 58 229 909 3623 2.156E-04 4.301E-04 4.889E-04 5.025E-04 1.00E-03 72 288 1147 4578 1.433E-04 8.414E-06 3.847E-05 4.761E-05 1.00E-04 87 347 1386 5534 1.826E-04 4.313E-05 7.179E-06 2.094E-06 1.00E-05 101 407 1625 6489 1.862E-04 4.756E-05 1.156E-05 2.524E-06 1.00E-06 116 466 1863 7444 1.866E-04 4.799E-05 1.200E-05 2.968E-06 1.00E-07 130 525 2102 8399 1.867E-04 4.803E-05 1.204E-05 3.012E-06 1.00E-08 145 585 2340 9355 1.205E-05 3.017E-06 1.00E-09 159 644 2579 10311

19 Number of iterations vs. problem size

20 Number of iterations vs. problem size
For fixed tolerance, looks like halving h makes the number of iterations go up by 4.

21 A dart tossing interlude before looking at data for errors
Suppose that a dart tosser has faulty vision. His failure to hit the target is due to two factors: Vision error ev: he “sees” the target shifted some distance from its actual position, so his aim is off. Skill error es: his darts can deviate from where he aims by some distance.

22 Vision Error He “sees” the target somewhere on a circle of radius ||ev|| centered about the true target T. T

23 Vision Error He aims at the seen target t*. The vision error is the distance between T and t*. T t*

24 Skill Error He throws at t*, but due to limited skill, he hits at t, somewhere on a circle of radius ||es|| centered at t*. T t* t

25 Skill Error The total error, the amount by which his toss misses the target, is bounded by the sum of the two errors. Note: he may get lucky and errors cancel. T t* t

26 How to improve

27 How to improve Get new glasses Practice tossing darts

28 Back to the problem We care about is the solution to the PDE, upde.
We approximately solve a discrete problem Au=b. The error is the difference between the true PDE solution and our approximate solution. The total error is the sum of the discretization error and the algebraic error.

29 Slicing and dicing data: error vs. tol
GS its E max Tol n=8 n=16 n=32 n=64 1.00E+00 29 110 432 1713 3.909E-02 4.829E-02 5.004E-02 5.049E-02 1.00E-01 43 169 670 2668 4.097E-03 4.851E-03 5.019E-03 5.049E-03 1.00E-02 58 229 909 3623 2.156E-04 4.301E-04 4.889E-04 5.025E-04 1.00E-03 72 288 1147 4578 1.433E-04 8.414E-06 3.847E-05 4.761E-05 1.00E-04 87 347 1386 5534 1.826E-04 4.313E-05 7.179E-06 2.094E-06 1.00E-05 101 407 1625 6489 1.862E-04 4.756E-05 1.156E-05 2.524E-06 1.00E-06 116 466 1863 7444 1.866E-04 4.799E-05 1.200E-05 2.968E-06 1.00E-07 130 525 2102 8399 1.867E-04 4.803E-05 1.204E-05 3.012E-06 1.00E-08 145 585 2340 9355 1.205E-05 3.017E-06 1.00E-09 159 644 2579 10311

30 Number of error vs. tolerance

31 Number of error vs. tolerance
In this region the algebraic error dominates. Reducing tolerance reduces error by similar factor.

32 Relationship between tolerance and algebraic error
We’re looking for the solution u* so that After completing a Gauss-Seidel iteration, we have an approximate solution ugs. One computable measure of the quality of the approximation is its residual. The residual is not the algebraic error, but is related to it by the matrix A.

33 Number of error vs. tolerance
In this region the discretization error dominates. Reducing tolerance further doesn’t help.

34 Slicing and dicing data: error vs. n
GS its E max Tol n=8 n=16 n=32 n=64 1.00E+00 29 110 432 1713 3.909E-02 4.829E-02 5.004E-02 5.049E-02 1.00E-01 43 169 670 2668 4.097E-03 4.851E-03 5.019E-03 5.049E-03 1.00E-02 58 229 909 3623 2.156E-04 4.301E-04 4.889E-04 5.025E-04 1.00E-03 72 288 1147 4578 1.433E-04 8.414E-06 3.847E-05 4.761E-05 1.00E-04 87 347 1386 5534 1.826E-04 4.313E-05 7.179E-06 2.094E-06 1.00E-05 101 407 1625 6489 1.862E-04 4.756E-05 1.156E-05 2.524E-06 1.00E-06 116 466 1863 7444 1.866E-04 4.799E-05 1.200E-05 2.968E-06 1.00E-07 130 525 2102 8399 1.867E-04 4.803E-05 1.204E-05 3.012E-06 1.00E-08 145 585 2340 9355 1.205E-05 3.017E-06 1.00E-09 159 644 2579 10311

35 Number of error vs. problem size

36 Number of error vs. problem size
For small tolerance, where error is dominated by discretization error, looks like halving h makes the total error number do down 4.

37 Investigations conclude: general things we saw
An order of magnitude reduction in tolerance requires a fixed number of additional iterations. As h gets smaller, iterations increase like h-2 As we decrease the tolerance, the total error is reduced by a similar factor until error is dominated by discretization error. Further reductions in the tolerance are pointless. As h gets smaller, the discretization error decreases like h2


Download ppt "Programming assignment #1. Solutions and Discussion"

Similar presentations


Ads by Google