Tcl/Tk Cookbook - Tcl/Tk and FORTRAN


Step 1: FORTRAN application to solve a quadratic equation

C
CThis program solves the quadratic equation
C          2
C	a x  + b x + c = 0
C
	IMPLICITDOUBLE PRECISION (a-h,o-z)
	COMMON IFLAG
c 
	PRINT *, 'Input the coefficients a,b,c'
	READ *,a,b,c
	CALL qsolve(a,b,c,x1,x2)
	IF (IFLAG .EQ. 0) THEN
	    PRINT*,'IFLAG = ',IFLAG 
	    PRINT *, 'ROOTS ARE REAL'
	ELSE
	    PRINT*,'IFLAG = ',IFLAG
	    PRINT*,'ROOTS ARE COMPLEX -- (RealPart, ImagPart) = (x1,x2)'
	END IF
	WRITE(UNIT=6, FMT=*)'x1 = ',x1,'  ','x2 = ',x2 
	END
	SUBROUTINE qsolve(a,b,c,x1,x2)
C+
C 
C FUNCTIONAL DESCRIPTION:	
C 
C    solves the quadratic equation
C 
C input parameters   a,b,c  (DOUBLEPRECISION)
C 
C-
	IMPLICITDOUBLE PRECISION (a-h,o-z)
	COMMON IFLAG
c
c compute the discriminant
c
	disc = (b*b - 4*a*c)
	PRINT*,'DISC : ',DISC
	IF (disc .GE. 0) THEN
	    x1 = (- b + sqrt( disc ) )/(2.0*A)
	    x2 = (- b - sqrt( disc ) )/(2.0*A)
	    iflag = 0
	ELSE
	    iflag = 1
	    x1 = -b/(2*A)
	    x2 = sqrt(-disc)/(2*A)
	END IF
	RETURN
	END

Place this code in a file quads.f in your current working directory and compile it by typing at commandline:

		f77 -o quads quads.f

The following is the output from running quads twice with two different sets of coefficients:


holly% quads
 Input the coefficients a,b,c
2 5 6
 DISC :    -23.000000000000
 IFLAG =   1
 ROOTS ARE COMPLEX -- (RealPart, ImagPart) = (x1,x2)
 x1 =    -1.2500000000000  x2 =     1.1989578808282



holly% quads
 Input the coefficients a,b,c
2 5 3
 DISC :     1.0000000000000
 IFLAG =   0
 ROOTS ARE REAL
 x1 =    -1.0000000000000  x2 =    -1.5000000000000


Next step presents the Tk front-end to this application.