PROGRAM Series_RLC

   DIM ZRectangular(2), ZPolar(2)


   LET R = 50
   LET C = 1E-6
   LET L = 2E-3
   LET F = 1000

   CALL Calc_Series_RLC((R), (L), (C), (F), ZRectangular, ZPolar)

   CALL Display_Results(ZRectangular, ZPolar)

END



! ===================================================================
! SUBROUTINE: Calc_Series_RLC
! ===================================================================
!
! PURPOSE: To calculate the rectangular and polar forms of
!          impedance of a series RLC circuit.
!
! PARAMETERS:
!
!    INPUT: R ... Resistance in ohms
!           L ... Inductance in henries
!           C ... Capacitance in farads
!           F ... Frequence in hertz
!
!    OUTPUT: ZRect(2) ... Rectangular form of calculated result
!            ZPolar() ... Polar form of calculated result
!
!    IN/OUT: NONE
!
! SHARED VARIABLES: NONE
!
! GLOBAL VARIBLES: NONE
!
! PRECONDITIONS: OUTPUT parameters must be declared in calling routine
!
! POSTCONDITIONS: Calculated result reported to calling routine
!                 through OUTPUT parameters.
!
! ROUTINES CALLED: NONE
!
! PSEUDOCODE:
!
!    1 - Trap for overflow
!
!    2 - Calculate OMEGA
!
!    3 - Calculate inductive reactance
!
!    4 - Calculate capacitive reactance
!
!    5 - Solve impedance
!
!        a) Test for negative resistance, impossible condition.
!           Make all OUTPUT parameters zero.
!
!        b) Assign calculated values to OUTPUT parameters.
!           Assign -90, 0, or +90 degrees to polar form if
!           resistance is zero.
!
!
! DIAGRAMS:
!
!    ARRAYS IN CALLING ROUTINE:
!
!                   ZRect(2)
!               ________________
!              |                |
!      (1)     |   Resistance   |
!              |________________|
!              |                |
!      (2)     |   Reactance    |
!              |________________|
!
!
!                   ZPolar(2)
!               ________________
!              |                |
!      (1)     |   Impedance    |
!              |________________|
!              |                |
!      (2)     |   Phase Angle  |
!              |________________|
!
!
! ===================================================================
SUB Calc_Series_RLC(R, L, C, F, ZRect(), ZPolar())

   OPTION ANGLE DEGREES

   ! Trap numerical overflow
   !
   IF F = 0 OR C = 0 THEN
      LET ZRect(1)  =  R
      LET ZRect(2)  = -MAXNUM
      LET ZPolar(1) =  MAXNUM
      LET ZPolar(2) = -90
      EXIT SUB
   END IF

   LET OMEGA = 2*PI*F

   ! Solve inductive reactance
   !
   LET XL = OMEGA*L

   ! Solve capacitive reactance
   !
   LET XC = 1/(OMEGA*C)

   ! Solve reactance
   !
   LET X  = XL - XC

   ! Solve impedance, rectangluar and polar forms
   !
   IF R >= 0 THEN
      LET ZRect(1)  = R
      LET ZRect(2)  = X
      LET ZPolar(1) = SQR(R^2 + X^2)
      IF R = 0 THEN
         IF X < 0 THEN
            LET ZPolar(2) = -90
         ELSEIF X > 0 THEN
            LET ZPolar(2) = 90
         ELSE
            LET ZPolar(2) = 0
         END IF
      ELSE
         LET ZPolar(2) = ATN(X/R)
      END IF
   ELSE
      ! We don't handle negative resistances
      LET ZRect(1) = 0
      LET ZRect(2) = 0
      LET ZPolar(1) = 0
      LET ZPolar(2) = 0
   END IF

END SUB



SUB Display_Results(ZRectangular(), ZPolar())

   ! Display rectangular form of impedance
   !
   PRINT "Zr = "; STR$(ZRectangular(1));

   IF ZRectangular(2) < 0 THEN
      PRINT " -j"; STR$(ABS(ZRectangular(2)));
   ELSE
      PRINT " +j"; STR$(ZRectangular(2));
   END IF

   ! Display polar form of impedance
   !
   PRINT ",  Zp = "; STR$(ZPolar(1)); " Ohms at "; 
   PRINT STR$(ZPolar(2)); " Degrees"

END SUB

