1 / 22

Scientific Programming

Scientific Programming. Class 8. In-Class Exercise. REAL::a1,a2,a3,a4,c a1 = 10420. ! $/sem a2 = 16.0 ! hrs/sem a3 = 3.0 ! hrs/course a4 = 30.0 ! classes/course c = (a1/a2)*(a3/a4) WRITE(*,*) ‘c = ‘,c WRITE(*,*) ‘ hidden message ‘

mimi
Download Presentation

Scientific Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Scientific Programming Class 8

  2. In-Class Exercise • REAL::a1,a2,a3,a4,c • a1 = 10420. ! $/sem • a2 = 16.0 ! hrs/sem • a3 = 3.0 ! hrs/course • a4 = 30.0 ! classes/course • c = (a1/a2)*(a3/a4) • WRITE(*,*) ‘c = ‘,c • WRITE(*,*) ‘ hidden message ‘ • END

  3. In-Class Exercise (cont.) c = 65.13 $/class (The cost of a single session of ME 112)

  4. Today’s Assignment • Write A “Brute Force” Fortran Program To Solve For All The Real Roots Of A 5th Order Polynomial Within A Specified Range For x • f(x) = ax5+bx4+cx3+dx2+ex+f = 0 • Input: • a,b,c,d,e,f – polynomial coefficients • xlow, xhigh – range for x • dx – step size for x • Output: • Up to 5 values of r +- uncertainty • I will email you test case values, a - f, after today’s class – turn in this test case with your program

  5. “Brute Force” Root Finder f>0 f<0 r = either value of x +- dx

  6. Polysolve1 • PROGRAM polysolve1 • IMPLICIT NONE • REAL:: a, b, c, d, e, f • REAL:: x, dx, fo, fn • INTEGER:: i,k • WRITE(*,*)'Enter Values For a, b, c, d, e, f' • READ(*,*) a,b,c,d,e,f • WRITE(*,*)'Enter Starting Values For x, dx, k' • READ(*,*)x, dx, k • DO i = 1, k • fo = a*x**5 + b*x**4 + c*x**3 + d*x**2 + e*x + f • fn = a*(x+dx)**5+b*(x+dx)**4+c*(x+dx)**3+d*(x+dx)**2+e*(x+dx)+f • IF ((fo>0.).AND.(fn>0.).OR.(fo<0.).AND.(fn<0.)) THEN • ELSE • WRITE(*,*) 'There Is A Real Root At x =',x,'+-',dx • END IF • x = x + dx • END DO • END

  7. Polysolve2 • PROGRAM polysolve2 • IMPLICIT NONE • REAL:: a, b, c, d, e, f • REAL:: xlow, xhigh, x, dx, fo, fn, i • WRITE(*,*)'Enter Values For a, b, c, d, e, f' • READ(*,*) a,b,c,d,e,f • WRITE(*,*)'Enter Starting Values For xlow, xhigh, dx' • READ(*,*)xlow, xhigh, dx • x = xlow • DO i = xlow,xhigh,dx !Holdover from Fortran 77 - Not Recommended - Indices should be integer type • fo = a*x**5 + b*x**4 + c*x**3 + d*x**2 + e*x + f • fn = a*(x+dx)**5+b*(x+dx)**4+c*(x+dx)**3+d*(x+dx)**2+e*(x+dx)+f • IF ((fo>0.).AND.(fn>0.).OR.(fo<0.).AND.(fn<0.)) THEN • ELSE • WRITE(*,*) 'There Is A Real Root At x =',x,'+-',dx • END IF • x = x + dx • END DO • END

  8. Polysolve3 • PROGRAM polysolve3 • IMPLICIT NONE • REAL:: a, b, c, d, e, f • REAL:: xlow, xhigh, x, dx, fo, fn • INTEGER:: i, k • WRITE(*,*)'Enter Values For a, b, c, d, e, f' • READ(*,*) a,b,c,d,e,f • WRITE(*,*)'Enter Starting Values For xlow, xhigh, dx' • READ(*,*)xlow, xhigh, dx • x = xlow • k = (xhigh - xlow)/dx • DO i = 1, k !Recommended - Indices are now integer type • fo = a*x**5 + b*x**4 + c*x**3 + d*x**2 + e*x + f • fn = a*(x+dx)**5+b*(x+dx)**4+c*(x+dx)**3+d*(x+dx)**2+e*(x+dx)+f • IF ((fo>0.).AND.(fn>0.).OR.(fo<0.).AND.(fn<0.)) THEN • ELSE • WRITE(*,*) 'There Is A Real Root At x =',x,'+-',dx • END IF • x = x + dx • END DO • END

  9. Polysolve3 Output • c:\FortranSources>polysolve3 • Enter Values For a, b, c, d, e, f • 1,-25,230,-950,1689,-945 • Enter Starting Values For xlow, xhigh, dx • 0,10,.1 • There Is A Real Root At x = 0.9000001 +- 0.1000000 • There Is A Real Root At x = 2.999999 +- 0.1000000 • There Is A Real Root At x = 4.999998 +- 0.1000000 • There Is A Real Root At x = 6.999996 +- 0.1000000 • There Is A Real Root At x = 8.999998 +- 0.1000000 • c:\FortranSources>polysolve3 • Enter Values For a, b, c, d, e, f • 1,-25,230,-950,1689,-945 • Enter Starting Values For xlow, xhigh, dx • 0,10,.01 • There Is A Real Root At x = 0.9999993 +- 9.9999998E-03 • There Is A Real Root At x = 2.999998 +- 9.9999998E-03 • There Is A Real Root At x = 4.990019 +- 9.9999998E-03 • There Is A Real Root At x = 6.990065 +- 9.9999998E-03 • There Is A Real Root At x = 8.990110 +- 9.9999998E-03 • c:\FortranSources>polysolve3 • Enter Values For a, b, c, d, e, f • 1,-25,230,-950,1689,-945 • Enter Starting Values For xlow, xhigh, dx • 0,10,.001 • There Is A Real Root At x = 0.9999907 +- 1.0000000E-03 • There Is A Real Root At x = 2.999965 +- 1.0000000E-03 • There Is A Real Root At x = 4.999820 +- 1.0000000E-03 • There Is A Real Root At x = 6.999675 +- 1.0000000E-03 • There Is A Real Root At x = 8.999006 +- 1.0000000E-03

  10. Polysolve3 Output • c:\FortranSources>polysolve3 • Enter Values For a, b, c, d, e, f • 1,-25,230,-950,1689,-945 • Enter Starting Values For xlow, xhigh, dx • 4,8,.0001 • There Is A Real Root At x = 4.999956 +- 9.9999997E-05 • There Is A Real Root At x = 6.999969 +- 9.9999997E-05 • c:\FortranSources>

  11. Limitations of Method • Range of x is trial & error • Complex Conjugate Roots • 1 R & 2 CC or, 3 R & 1 CC, or 5 R • Cannot find them • Real, Repeated Roots • Usually won’t find them • If found, can’t identify

  12. Test Case:

  13. Things To Come - Loops • The While Loop • The DO WHILE Loop • The Iterative Loop • CYCLE Statement • EXIT Statement

  14. The WHILE Loop • DO • … • … • IF (logical_expression) EXIT • … • … • END DO • …

  15. The DO WHILE Loop • DO WHILE (logical_expression) • … • … • … • END DO • …

  16. The Iterative Loop • DO index = istart, iend, incr • … • … • END DO • … • aka “counting” loop

  17. The CYCLE Statement • DO i = 1, 10 • … • … • IF (logical expression) CYCLE • … • … • END DO

  18. The EXIT Statement • DO i = 1, 10 • … • … • IF (logical expression) EXIT • … • … • END DO • …

  19. Named Loops • name: DO i = 1, 10 • … • … • IF (logical expression) EXIT • … • … • END DO name • …

  20. Nested Loops • outer: DO i = 1, 10 • … • inner: DO j = 1, 100 • … • … • … • END DO inner • … • END DO outer • …

  21. Polysolvewhile • PROGRAM polysolvewhile • IMPLICIT NONE • REAL:: a, b, c, d, e, f • REAL:: xlow, xhigh, x, dx, fo, fn • WRITE(*,*)'Enter Values For a, b, c, d, e, f' • READ(*,*) a,b,c,d,e,f • WRITE(*,*)'Enter Starting Values For xlow, xhigh, dx' • READ(*,*)xlow, xhigh, dx • x = xlow • DO • fo = a*x**5 + b*x**4 + c*x**3 + d*x**2 + e*x + f • fn = a*(x+dx)**5+b*(x+dx)**4+c*(x+dx)**3+d*(x+dx)**2+e*(x+dx)+f • IF ((fo>0.).AND.(fn>0.).OR.(fo<0.).AND.(fn<0.)) THEN • ELSE • WRITE(*,*) 'There Is A Real Root At x =',x,'+-',dx • END IF • x = x + dx • IF (x>xhigh) EXIT • END DO • END

  22. Assignment: • Read and understand the material in this slide set. • Practice Problem: Write, compile and run a Fortran Program that will accept a number as input and tell you whether it is a Prime Number. Note: The embedded function, AINT(), can be used to good advantage in this case.

More Related