Sunday, May 19, 2024

Python and Calculator Basic: Transforming Quadratic Polynomials

Python and Calculator Basic: Transforming Quadratic Polynomials

Calculators: Casio fx-CG 50, TI-84 Plus CE


Problem


Sometimes we are called to simplify quadratic polynomials, like so:


A * x^2 + B * x + C = (S * x + T)^2 + U


where A, B, C, S, T, and U are constants, which can be complex. In this problem, we are given A, B, and C, determine S, T, and U.


Observe that:

(S * x + T)^2 + U = S^2 * x^2 + 2 * S * T * x + T^2 + U


Then, matching this to the left side:

A * x^2 + B * x + C = S^2 * x^2 + 2 * S * T * x + T^2 + U


x^2 coefficient: A = S^2 which implies that S = ±√A

x coefficient: B = 2 * S * T which implies that T = B / (2 * S)

Constant coefficient: C = T^2 + U which implies that U = C – T^2


(For today’s blog, I am just going to use the principal square root, S = √A. Taking the negative square root will also provide accurate results.)



Example: Transform x^2 + 6 * x + 8 into the form (S * x + T)^2 + U.


Note that A = 1, B = 6, and C = 8.

Then:

S = √1 = 1

T = 6 / ( 2 * 1) = 3

U = 8 – 3^2 = -1


x^2 + 6 * x + 8 = (x + 3)^2 – 1



Code: Python


This code was entered on a fx-CG 50, but it should work on all calculators and platforms with Python. No modules are needed.


Title: quadtrans.py

711 bytes


from math import *

print(“Quadratic \nTransformation”)

print(“1. -> (s*x+t)**2+u”)

print(“2. -> a*x**2+b*x+c”)

ch=int(input(“choice? “))


if ch==1:

  print(“a*x**2+b*x+c ->”)

  a=eval(input(“a? “))

  b=eval(input(“b? “))

  c=eval(input(“c? “))

  print(“Principal Root”)

  print(“-> (s*x+t)**2+u”)

  s=a**(1/2)

  t=b/(2*s)

  u=c-t**2

  print(“s= “+str(s))

  print(“t= “+str(t))

  print(“u= “+str(u))

elif ch==2:

  print(“(s*x+t)**2+u ->”)

  s=eval(input(“s? “))

  t=eval(input(“t? “))

  u=eval(input(“u? “))

  print(“-> a*x**2+b*c+c”)

  a=s**2

  b=2*s*t

  c=t**2+u

  print(“a= “+str(a))

  print(“b= “+str(b))

  print(“c= “+str(c))

else:

  print(“Not a valid choice.”)


Basic Code: Casio fx-CG 50

Title: QUADTRNS, 316 bytes


a+bi

Menu “QUADRATIC TRANS.”, “-> (S×x+T)²+U”, 1, “-> A×x²+B×x+C”, 2


Lbl 1

ClrText

A×x²+B×x+C ->”

A”? → A

B”? → B

C”? → C

PRINCIPAL ROOT” ◢

A → S

B÷(2×S) → T

C–T² → U

ClrText

-> (S×x+T)²+U”

S=”

S ◢

T=”

T ◢

U=”

U

Stop


Lbl 2

(S×x+T)²+U ->”

S”? → S

T”? → T

U”? → U

S² → A

2×S×T → B

T²+U → C

-> A×x²+B×x+C”

A=”

A ◢

B=”

B ◢

C=”

C

Stop


Basic Code: TI-84 Plus CE

Title: QUADTRNS (318 bytes)


a+bi

Menu (“QUADRATIC TRANS.”, “-> (S*X+T)²+U”, 1, “-> A*X²+B*X+C”, 2)


Lbl 1

ClrHome

Disp “A*X²+B*X+C ->”

Prompt A, B, C

Disp “PRINCIPAL ROOT”

Wait 0.5

(A) → S

B/(2*S) → T

C–T² → U

ClrHome

Disp “-> (S*X+T)²+U”

Disp “S= “+toString(S)

Disp “T= “+toString(T)

Disp “U= “+toString(U)

Stop


Lbl 2

Disp “(S*X+T)²+U ->”

Prompt S, T, U

S² → A

2*S*T → B

T²+U → C

ClrHome

Disp “-> A*X²+B*X+C”

Disp “A= “+toString(A)

Disp “B= “+toString(B)

Disp “C= “+toString(C)

Stop



Examples


4 * x^2 + 8 * x + 36 < - > (2 * x + 2)^2 + 32


A = 4, B = 8. C = 36

S = 2, T= 2, U = 32


x^2 – 8 * x + 3 < - > (x – 4)^2 – 13


A = 1, B = -8, C = 3

S = 1, T = -4, U = -13


The program allows for complex and imaginary coefficients:


-4 * x^2 + 8 * x + 16 < - > (2i * x – 2i)^2 + 20


A = -4, B = 8, C = 16

S = 2i, T = -2i, U = 20



Hope you find this useful. Until next time,


Eddie


All original content copyright, © 2011-2024. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.