220 likes | 344 Views
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 ‘
E N D
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 ‘ • END
In-Class Exercise (cont.) c = 65.13 $/class (The cost of a single session of ME 112)
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
“Brute Force” Root Finder f>0 f<0 r = either value of x +- dx
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
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
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
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
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>
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
Things To Come - Loops • The While Loop • The DO WHILE Loop • The Iterative Loop • CYCLE Statement • EXIT Statement
The WHILE Loop • DO • … • … • IF (logical_expression) EXIT • … • … • END DO • …
The DO WHILE Loop • DO WHILE (logical_expression) • … • … • … • END DO • …
The Iterative Loop • DO index = istart, iend, incr • … • … • END DO • … • aka “counting” loop
The CYCLE Statement • DO i = 1, 10 • … • … • IF (logical expression) CYCLE • … • … • END DO
The EXIT Statement • DO i = 1, 10 • … • … • IF (logical expression) EXIT • … • … • END DO • …
Named Loops • name: DO i = 1, 10 • … • … • IF (logical expression) EXIT • … • … • END DO name • …
Nested Loops • outer: DO i = 1, 10 • … • inner: DO j = 1, 100 • … • … • … • END DO inner • … • END DO outer • …
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
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.