'****************************************************** '* ROOTS OF A SECOND DEGREE EQUATION WITH COMPLEX * '* COEFFICIENTS AZ2 + BZ + C = 0 * '* -------------------------------------------------- * '* SAMPLE RUN: * '* (Find complex roots of equation: * '* (-4+i)z2 + (2-3i)z +(5-i) = 0). * '* * '* SOLVING A COMPLEX 2ND DEGREE EQUATION * '* az2 + bz + c = 0 * '* * '* a real part = -4 * '* a imaginary part = 1 * '* b real part = 2 * '* b imaginary part = -3 * '* c real part = 5 * '* c imaginary part = -1 * '* * '* Root 1 = 1.444644 - 0.352759 i * '* Root 2 = -0.797586 - 0.235476 i * '* * '* -------------------------------------------------- * '* Ref.: "Mathématiques en Turbo-Pascal By M. Ducamp * '* and A. Reverchon (vol 2), Eyrolles, Paris, 1988" * '* [BIBLI 05]. * '* * '* Basic Release By J-P Moreau, Paris. * '* (www.jpmoreau.fr) * '****************************************************** 'Program TEQUA2C DEFDBL A-H, O-Z DEFINT I-N DIM a(2), b(2), c(2), s1(2), s2(2) 'complex numbers CLS PRINT PRINT " SOLVING A COMPLEX 2ND DEGREE EQUATION" PRINT " az2 + bz + c = 0" PRINT INPUT " a real part = "; a(1) INPUT " a imaginary part = "; a(2) INPUT " b real part = "; b(1) INPUT " b imaginary part = "; b(2) INPUT " c real part = "; c(1) INPUT " c imaginary part = "; c(2) GOSUB 1000 'call equa2c(a,b,c,s1,s2) F1\$ = " Root 1 = ###.######" F2\$ = " + ###.###### i" F3\$ = " - ###.###### i" F4\$ = " Root 2 = ###.######" PRINT PRINT USING F1\$; s1(1); IF s1(2) > 0 THEN PRINT USING F2\$; ABS(s1(2)) ELSE PRINT USING F3\$; ABS(s1(2)) END IF PRINT USING F4\$; s2(1); IF s2(2) > 0 THEN PRINT USING F2\$; ABS(s2(2)) ELSE PRINT USING F3\$; ABS(s2(2)) END IF PRINT END 1000 'Subroutine equa2c DIM u(2), v(2), w(2)'complex numbers u(1) = b(1) * b(1) - b(2) * b(2) - 4 * a(1) * c(1) + 4 * a(2) * c(2) u(2) = 2 * b(1) * b(2) - 4 * a(1) * c(2) - 4 * a(2) * c(1) r = SQR(u(1) * u(1) + u(2) * u(2)) v(1) = SQR((r + u(1)) / 2) v(2) = SQR((r - u(1)) / 2) IF u(2) < 0 THEN v(2) = -v(2) w(1) = (-b(1) - v(1)) / 2 w(2) = (-b(2) - v(2)) / 2 u(1) = (-b(1) + v(1)) / 2 u(2) = (-b(2) + v(2)) / 2 r = a(1) * a(1) + a(2) * a(2) s1(1) = (a(1) * w(1) + a(2) * w(2)) / r s1(2) = (a(1) * w(2) - a(2) * w(1)) / r s2(1) = (a(1) * u(1) + a(2) * u(2)) / r s2(2) = (a(1) * u(2) - a(2) * u(1)) / r RETURN 'end of file tequa2.bas