Numerical Analysis Problem #2 (QBasic program from Feb. 1993)
' Assignment #1 Problem # 2
'
' This program attempts to find the value of R and S for an expression
' in the form : X^2 - R*X -S which is a factor of the given 4th
' degree polynomial:
' X^4 + 2*X^3 - 7*X^2 + 3 .
'
'Bairstow's Method will be used to refine a guess for the values of R
'and S, and the coefficients of the remaining quotient terms will be
'displayed. Various initial guesses will be attempted to illustrate
'the different program outputs that may result.
PRINT
N = 4 '4th degree polynomial
A(3) = 1 'Coefficients of initial polynomial
A(4) = 2
A(5) = -7
A(6) = 0
A(7) = 3
B(1) = 0: B(2) = 0: C(1) = 0: C(2) = 0 'Causes unnecessary terms to be disregarded
INPUT "Enter initial guess (R,S) :"; R, S 'Prompt user for initial guess
PRINT
DR = 100: DS = 100 'Makes certain to fail tolerances
Tolerance = .000001 'Set tolerance
PRINT " R S delta-R delta-S" 'Heading
DO WHILE ABS(DR) >= Tolerance OR ABS(DS) >= Tolerance 'Continue if tolerance not satisfied
Retry: FOR I = 3 TO N + 3 'Loop thru subscripts
B(I) = A(I) + R * B(I - 1) + S * B(I - 2) 'Set the B's
C(I) = B(I) + R * C(I - 1) + S * C(I - 2) 'Set the C's
NEXT I
PRINT USING " ###.####### "; R; S; DR; DS 'Print Values
Denominator = C(N + 1) * C(N + 1) - C(N + 2) * C(N) 'Calculate Denom.of Remainder
IF Denominator = 0 THEN 'Compare to zero
R = R + 1 'Increment R
S = S + 1 'Increment S
GOTO Retry 'Retry with the new guess
END IF
'Calculate delta-R & delta-S
DR = (B(N + 3) * C(N) - B(N + 2) * C(N + 1)) / Denominator
DS = (C(N + 2) * B(N + 2) - C(N + 1) * B(N + 3)) / Denominator
R = R + DR 'Effect changes
S = S + DS
LOOP 'Continue the tolerance loop
PRINT
PRINT "One quadratic factor is "; 'Display a quadratic factor
PRINT "X^2 + "; -R; " X + "; -S
PRINT
PRINT "The remaining factor has coefficients:"; 'Display what's left-over
FOR I = 3 TO 5
PRINT USING " ##.###### "; B(I);
NEXT I
END
***** Program output (run #1):
Enter initial guess (R,S) :? 0,0
R S delta-R delta-S
0.0000000 0.0000000 100.0000000 100.0000000
0.1224490 0.4285714 0.1224490 0.4285714
0.1705088 0.4877028 0.0480599 0.0591313
0.1732498 0.4890449 0.0027410 0.0013422
0.1732538 0.4890428 0.0000040 -0.0000022
One quadratic factor is X^2 + -.1732538 X + -.4890428
The remaining factor has coefficients: 1.000000 2.173254 -6.134433
***** Program output (run #2):
Enter initial guess (R,S) :? 1,1
R S delta-R delta-S
1.0000000 1.0000000 100.0000000 100.0000000
One quadratic factor is X^2 + -1 X + -1
The remaining factor has coefficients: 1.000000 3.000000 -3.000000
***** Program output (run #3):
Enter initial guess (R,S) :? 2,2
R S delta-R delta-S
2.0000000 2.0000000 100.0000000 100.0000000
2.2253520 -0.9718311 0.2253521 -2.9718311
2.4284804 -1.2954395 0.2031282 -0.3236085
2.4091680 -1.2795196 -0.0193123 0.0159200
2.4093218 -1.2803308 0.0001538 -0.0008112
One quadratic factor is X^2 + -2.409322 X + 1.280331
The remaining factor has coefficients: 1.000000 4.409322 2.343144
Summary :
This polynomial can be factored into quadratic factors in several ways.
The second run of the program indicates integer value for the coefficients
and therefore will provide the easiest route:
X^4 + 2*X^3 - 7*X^2 + 3 = ( X^2 - X - 1 )( X^2 + 3*X -3 )
The quadratic formula can be used to factor into:
( X-(1+sqr 5)/2 )( X-(1-sqr 5)/2 )( X+(3+sqr 21)/2 )( X+(3+sqr 21)/2 )
Because real quadratic polynomial factors can be formed in 3 different ways,
the program output showing several possibilities is explained.
This will generally occur in all 4th degree polynomials with all real roots.