190 likes | 336 Views
Today’s Assignment. Construct A Flowchart To Evaluate The Roots Of A Quadratic Equation, Including Complex Roots a x 2 + b x + c = 0 r 1,2 = -b +- sqrt(b 2 – 4ac) 2a real, distinct real, repeated complex conjugate special case when a = 0. Quadratic Flowchart.
E N D
Today’s Assignment Construct A Flowchart To Evaluate The Roots Of A Quadratic Equation, Including Complex Roots • a x2 + b x + c = 0 • r1,2 = -b +- sqrt(b2 – 4ac) 2a • real, distinct • real, repeated • complex conjugate • special case when a = 0
Quadratic Solver Flowchart START Enter a, b, c Calculate b2 – 4ac compare: b2 – 4ac < 0 > 0 = 0 CC roots real,repeated roots real, distinct roots R +- i I r1 r1, r2 End
Scientific Programming Class 5
Logical Variables • L1 = (logical expression) • L1, L2, L3 are either .TRUE. or .FALSE. • IMPLICIT NONE • INTEGER:: i, j, k, etc • REAL:: a1, a2, a3, etc • LOGICAL:: L1, L2, L3, etc
Relational Operators • Relational Operators • == Equal To (Note doubling - ==) • /= Not Equal To • > Greater Than • >= Greater Than or Equal To • < Less Than • <= Less Than or Equal To • For comparing arithmetic operations, constants, variables or character strings.
Combinational Operators • L1.AND.L2 • .TRUE. If both are .TRUE. • L1.OR.L2 • .TRUE. If either or both are .TRUE. • L1.EQV.L2 • .TRUE. If L1 & L2 are the same • L1.NEQV.L2 • .TRUE. If L1 & L2 are different • .NOT.L1 • Opposite L1 • For comparing logical statements
Combinational Operators • Hierarchy of Operations • Arithmetic • Relational L to R • .NOT. • .AND. L to R • .OR. L to R • .EQV. & .NEQV. L to R • Use ( ) to control the order
Block IF • IF (logical statement) THEN • Statement 1 • Statement 2 • ….. • END IF • Next line of code • …
Block IF (cont.) • If (logical statement) is .TRUE., the program executes the block of code following the IF THEN statement • If (logical statement) is .FALSE., the program executes the code following the END IF statement
ELSE & ELSE IF • IF (logical statement 1) THEN • Statement 1 • Statement 2 • ….. • ELSE IF (logical statement 2) THEN • Statement 1 • Statement 2 • …………………….More ELSE IF statements • ELSE • Statement 1 • Statement 2 • ….. • END IF • Next line of code • …
ELSE & ELSE IF (cont.) • If (logical statement 1) is .TRUE., the program executes the block of code following the IF THEN statement and jumps to the statement following the END IF statement • If (logical statement 1) is .FALSE., the program jumps to the first ELSE IF statement and on, and on, and on….. • The last clause may be an ELSE statement without a logical statement
Example: Moody Chart • Laminar Flow: Re <= 2200 • f = 64/Re • Turbulent Flow: Re > 2200 • Colebrook Formula:
Example: Moody Chart • Write, compile & run a Fortran program that calculates D-W friction factors for a pipe with inside diameter, D (in), roughness, e (mm), & Reynold’s Number, Re (dim). The program must use logic to control which friction factor formula to use in the calculation for f. • Use READ statements for the e, D & Re input so that the program can be used for any input.
Example: Moody Chart • PROGRAM colebrook1 • !e = pipe roughness (mm) • !d = pipe inside diameter (in) • !relruff = relative roughness (dim) • !re = Reynolds Number (dim) • !f = Darcy-Weisbach Friction Factor (dim) • IMPLICIT NONE • REAL:: f, f0, f1, re, e, d, relruff • INTEGER:: k • WRITE(*,*)'Enter e in mm & D in inches' • READ(*,*) e, d • WRITE(*,*)'Pipe Roughness, e =',e,'mm ','Pipe Diameter, D =',d,'inches' • relruff = e/(d*25.4) • WRITE(*,*)'relative roughness =', relruff • WRITE(*,*)'Enter Reynolds No' • DO k = 1, 11 • READ(*,*) re • IF (re<=2200.) THEN • f = 64./re • WRITE(*,*)'For Reynolds No =',re,'Darcy-Weisbach Friction Factor =',f • ELSE • f0 = .25*((LOG10((relruff/3.7)+(5.74/(re**.9))))**(-2)) • f1 = (-2.)*LOG10(((relruff/3.7)+(2.51/(re*(f0**.5))))) • f = (1./f1)**2 • WRITE(*,*)'For Reynolds No =',re,'Darcy-Weisbach Friction Factor =',f • END IF • END DO • END
Example: Moody Chart c:\FortranSources>colebrook1 Enter e in mm & D in inches .26,2.067 Pipe Roughness, e = 0.2600000 mm Pipe Diameter, D = 2.067000 inches relative roughness = 4.9522114E-03 Enter Reynolds No 500 For Reynolds No = 500.0000 Darcy-Weisbach Friction Factor = 0.1280000 1000 For Reynolds No = 1000.000 Darcy-Weisbach Friction Factor = 6.4000003E-02 1500 For Reynolds No = 1500.000 Darcy-Weisbach Friction Factor = 4.2666666E-02 2000 For Reynolds No = 2000.000 Darcy-Weisbach Friction Factor = 3.2000002E-02 2200 For Reynolds No = 2200.000 Darcy-Weisbach Friction Factor = 2.9090909E-02 3000 For Reynolds No = 3000.000 Darcy-Weisbach Friction Factor = 4.7594767E-02 10000 For Reynolds No = 10000.00 Darcy-Weisbach Friction Factor = 3.7515681E-02
Assignment: 2nd Order Solver • Write, compile & run a Fortran program that uses the quadratic formula to determine the roots of the 2nd order equation: • a x2 + b x + c = 0 • The program must be able to handle all cases: • real distinct roots • real, repeated roots • complex conjugate roots • the special case where a = 0. • Use READ statements for the a, b, & c input so that the program can be used repeatedly without restarting.