Showing posts with label synthetic division. Show all posts
Showing posts with label synthetic division. Show all posts

Sunday, September 29, 2024

TI-30Xa Algorithm: Synthetic Division

TI-30Xa Algorithm:  Synthetic Division



Introduction


Synthetic division is a fairly simple division algorithm that divides a polynomial by the nominal (x-a):


p(x) / (x  - c) where


p(x) = a_n * x^n + a_n-1 * x^(n-1) + a_n-2 * x^(n - 2) + … + a_1 * x + a_0

c = a numerical constant


the result, q(x), is a polynomial of order n-1:


q(x) = b_n-1 * x^(n-1) + b_n-2 * x^(n - 2) + … + b_1 * x + b_0 + b_r / (x - c)

b_r = remainder term


where 


b_n-1 = a_n

b_i-1 = b_i  * c + a_i for i = n-2 down to r (“-1”, see the example below)  


For example, for the polynomial:


p(x) = a3 * x^3 + a2 * x^2 + a1 * x + a0


q(x) = p(x) / (x - c) 


p(x) has the order n = 3, so q(x) will have the order n = 3 - 1 = 2:


q(x) = b2 * x^2 + b1 * x + b0 + br / (x - c)


b2 = a3

b1 = b2 * c + a2

b0 = b1 * c + a1

br = b0 * c + a0  (the algorithm stops, this is the remainder term)


If br = 0, the x - c divides p(x) evenly, and x = c is a root of p(x).  


Note: for any “missing” terms, fill the term with 0.   

Example:  x^3 + 4 * x - 5 becomes x^3 + 0 * x^2 + 4 * x - 5.



Calculator Algorithm


  1. Store c in one of the memory registers 1, 2, or 3.   We’ll call this memory register m for the purpose of the blog.

  2. Note the first coefficient of q(x):  a_n

  3. Compute the rest of the coefficients as follows:  [ × ] [ RCL ] m [ + ] a_ni [ = ]



Examples


Remember:  m is memory register 1, 2, or 3, your choice.  


Example 1:  (21 * x^2 + 42 * x + 144) / (x - 12) 


c = 12

a2 = 21

a1 = 42

a0 = 144


b1 = a2 = 21


12 [ STO ] m

Enter 21


b0:

[ × ] [ RCL ] m [ + ] 42 [ = ]

Result:  294


br:

[ × ] [ RCL ] m [ + ] 144 [ = ]

Result:  3672

Stop.


q(x) = 21 * x + 294 + 3672 / (x - 12)



Example 2:  (2 * x^3 + x - 3) / (x - 3) = (2 * x^3 + 0 * x^2 + x - 3) / (x - 3)


c = 3

a3 = 2

a2 = 0

a1 = 1

a0 = -3


b2 = a3 = 2

3 [ STO ] m

Enter 2


b1:

[ × ] [ RCL ] m [ + ] 0 [ = ]

Result:  6


b0:

[ × ] [ RCL ] m [ + ] 1 [ = ]

Result:  19


br:

[ × ] [ RCL ] m [ + ] 3 [ +/- ]  [ = ]

Result:  54


q(x) = 2 * x^2 + 6 * x + 19 + 54 / (x - 3)


Example 3:  (4 * x^3 + 8 * x^2 - 5 * x + 3) / (x + 2)


c = -2

a3 = 4

a2 = 8

a1 = -5

a0 = 3


b2 = a3 = 4


2 [ +/- ] [ STO ] m 

Enter 4


b2:

[ × ] [ RCL ] m [ + ] 8 [ = ]

Result:  0


b3:

[ × ] [ RCL ] m [ + ] 5 [+/-] [ = ]

Result:  -5


br:

[ × ] [ RCL ] m [ + ] 3 [ = ]

Result:  13


q(x) = 4 * x^2 - 5 + 13 / (x + 2)


I think this is a fundamental algorithm for students and math enthusiasts to learn, and it’s fairly simple to get a hang of it. 


Until next time,


Eddie


Quick update: Starting October 5, 2024, my schedule will allow me to blog once a week. Regular posts will go live every Saturday. Thank you for your support and compliments. I wish you all well.


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.


  

Saturday, November 18, 2023

HP 32S and HP 15C: Synthetic Division

HP 32S and HP 15C:   Synthetic Division



These programs perform synthetic division, where a polynomial of degree n is divided by the binomial term x - x_0:


[a_n × x^n + a_(n-1) × x^(n-1) + a_(n-2) × x^(n-2) + ... a_1 × x + a_0] ÷ (x - x_0)


With the result of the division is:


[q_(n-1) × x^(n-1) + q_(n-2) × x^(n-2) + ... + q_1 × x + q_0] + R ÷ (x - x_0)


where: 


q_(n-1) = a_n

R is the remainder.



HP 32S/32SII:  Synthetic Division


The degree of the polynomial can go up to 24, with the variables set up as:


p(x) = [ A + B × x + C × x^2 + D × x^3 + ... + Y × x^24 ] ÷ (x - Z)


where

A is the constant

B is the coefficient of x

C is the coefficient of x^2 

and so on...

Z = x_0


Flag 1 is used as an indicator that there are coefficients q_k.  



Code:


S01  LBL S

S02  CF 1

S03  1

S04  +

S05  STO i

S06  SF 1

S07  RCL (i)

S08  STOP

S09  DSE i


Z01  LBL Z

Z02  RCL× Z

Z03  RCL+ (i)

Z04  STOP

Z05  DSE i

Z06  GTO Z

Z07  CF 1

Z08  RTN


Total Memory:  S:  13.5 bytes, Z:  12.0 bytes, Total:  25.5 bytes


To run:  store the coefficients and x0.  Enter the degree of the polynomial and press XEQ S.  



HP 15C:  Synthetic Division


The degree of the polynomial can go as high as you want, as long as you have registers.   The variables set up as:


p(x) = [ R0 + R1 × x + R2 × x^2 + R3 × x^3 + ... ] ÷ (x - R0)


where

R0 is the constant

R1 is the coefficient of x

R2 is the coefficient of x^2 

and so on...

R0 = x_0


Flag 8 is used as an indicator (C on the screen) that there are coefficients q_k.  In effect, this turns on complex mode, which would create a stack of imaginary numbers.  The complex mode for the HP 15C requires five registers to run.  


Code:


001  LBL A:   42,21,11

002  CF 8:  43, 5, 8

003  1:  1

004  +:  40

005  STO I:  44, 25

006  SF 8:  43, 4, 8

007  RCL (i):  45, 24

008  R/S:   31

009  DSE I:  42, 5, 25


010  LBL 0:  42, 21, 0

011  RCL× 0:  45, 20, 0

012  RCL+ (i):  45,40,24

013  R/S:  31

014  DSE I:  42, 5,25

015  GTO 0:  22, 0

016  CF 8:  43, 5, 8

017  RTN:  43,32


To run:  store the coefficients and x0.  Enter the degree of the polynomial and press GSB A.  



Examples



Example 1:  [ 4x^3 + 3x^2 + 2x - 5 ] ÷ [ x -  1 ]


HP 32S:

A:  -5,  B:  2,  C:  3,  D:  4,  Z:  1


HP 15C: 

R1:  -5,  R2:  2,  R3:  3,  R4:  4,  R0:  1


Degree: 3


3 XEQ S/GSB A:


Results:  

4   (x^2)  R/S

7   (x)   R/S

9   (constant)  R/S

4  (remainder)


4x^2 + 7x + 9 + 4 ÷ (x-1)



Example 2:  [ x^4 - 3x^2 + 6x + 3 ] ÷ [ x + 3 ]


HP 32S:

A:  3,  B:  6,  C:  -3, D:  0,  E:  1,  Z:  -3


HP 15C:

R1:  3,  R2:  6,  R3:  -3, R4:  0,  R5:  1,  R0:  -3


Degree:  4


4 XEQ S/GSB A


Results:

3   (x^3)  R/S

6   (x^2)  R/S

-3   (x)   R/S

0   (constant)  R/S

1  (remainder)


x^3 - 3x^2 + 6x - 12 + 39 ÷ (x + 3)



Example 3:  [ x^3 - 42x^2 + 395x + 966 ] ÷ [ x - 21 ]


HP 32S:

A:  966,  B:  395,  C:  -42,  D:  1,  Z:  21


HP 15C:

R1:  966,  R2:  395,  R3:  -42,  R4:  1,  R0:  21


Degree:  3


3 XEQ S/GSB A:


Results:  

1   (x^2)  R/S

-21   (x)   R/S

-46   (constant)  R/S

0 (remainder)


x^2 - 21x - 46



Source


Hewlett Packard.  "Synthetic Division"   HP-65 Math Pac 1.   September 1974.   pp. 34-35, 99




Next blog post:  November 25, 2023


Eddie


All original content copyright, © 2011-2023.  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. 


Friday, May 11, 2018

Fun with the Sharp EL-5500 III (May 2018 Edition)


Fun with the Sharp EL-5500 III   (May 2018 Edition)

The EL-5500 III is one of my favorite pocket programming device, small, portable, and has my favorite programming language: BASIC. 

Let’s get started.

Note:  Substitute line numbers with labels that you find fit.  Add defined labels to the first line if you would like.  Labels can come from the bottom two rows of the QWERTY keyboard (A row and Z row).

Comments are followed by double slashes (//), they are not typed.

Sharp EL-5500 III Program:  Net Present Value

2  PAUSE “NET PRESEN VALUE”
4  CLEAR    // clears all the variables
6  INPUT “CF0:”; N, “RATE:”; I
8  J = 1
10 INPUT “FLOW:”; F, “FREQ:”; K
12 FOR L=1 TO K: N = N + F/(1 + I/100)^J: J = J+1
14 NEXT L
16 INPUT “MORE=1: “; L   // enter 1 to enter more cash flows, anything else to end entry
18 PRINT USING “#############.##”; “NPV: “; N
20 END

Example:

CF0:  -7,000,  Interest Rate:  8%
Flow 1:  2,000, Freq 1: 1  (enter 1 for MORE)
Flow 2:  1,500, Freq 2: 2  (enter 1 for MORE)
Flow 3:  2,500, Freq 3: 2  (we are at the end, enter anything other than 1 at MORE)

Result:
NPV:  2443.06

Sharp EL-5500 III Program: Synthetic Division

2 PAUSE “Synthetic Division”: CLEAR
4 PRINT “P(X)/(X – R)”: WAIT 59   // WAIT 59 is about 1 second
6 INPUT “DEGREE:”; N
8 DIM P(N): DIM Q(N)
10 FOR I=1 TO N
12 PRINT “COEF OF X^”; N-I
14 INPUT P(I): Q(I) = P(I)
16 NEXT I   // there is no line 18
20 INPUT “R:”; R
22 FOR I=0 TO N-1
24 Q(I+1) = R*Q(I) + P(I+1)
26 NEXT I
28 E = Q(N)
30 PRINT “Q(X) = “
32 FOR I=0 TO N-1
34 PRINT Q(I); “X^”; N-I-1
36 WAIT 150  // about 2.5 seconds
38 NEXT I
40 PRINT “+”; E; “/(X-“; R; “)”: END

Example:  (x^4 – 2*x^3 + 1) / (x – 1)
Degree: 4
Coefficients:  1, -2, 0, 0, 1
R: 1

Result: 1, -1, -1, -1, no remainder
x^3 – x^2 – x – x

Sharp EL-5500 III Program: Vector Basics

Cross product, dot product, norm of two vectors, angle between two vectors

2 PAUSE “Vector Basics”
4 INPUT “X1:”; X1, “Y1:”; Y1, “Z1:”; Z1  \\ vector 1
6 INPUT “X2:’; X2, “Y2:”; Y2, “Z2:”, Z2  \\ vector 2
8 C1 = Y1*Z2 – Y2*Z1: C2 = -X1*Z2 + X2*Z1: C3 = X1*Y2 – X2*Y1  // cross product
10 D = X1*X2 + Y1*Y2 + Z1*Z2  \\ dot product
12 N1 = √(SQU X1 + SQU Y1 + SQU Z1): N2 = √(SQU X2 + SQU Y2 + SQU Z2)  // norm, SQU is the x^2 key
14 DEGREE
16 A = ACS( D/(N1*N2))  // angle between vectors, ACS is ACOS
18 PRINT “CROSS X: ”; C1
20 PRINT “CROSS Y: ”; C2
22 PRINT “CROSS Z: ”; C3
24 PRINT “DOT: ”; D
26 PRINT “NORM V1: “; N1
28 PRINT “NORM V2: “; N2
30 PRINT “ANGLE: “; A
32 END


Example:  V1 = [-2, 3, 0] and V2 = [ 4, 2, -11]
Cross: [-33, -22, -16]
Dot: -2
Norm V1: 3.605551275
Norm V2: 11.87434209
Angle: 92.67749998°

Sharp EL-5500 III: Atwood Machine

M1:  mass hanging on the left side of the machine
M2:  mass hanging on the right side of the machine

The program asks to choose a unit system.  Enter 1 for US units (feet, pounds, seconds, g = 32.174 ft/s^2), anything else for SI units (meters, kilograms, seconds, g = 9.80665 m/s^2).

2 PAUSE “Atwood Machine”
4 INPUT “1: US, ELSE: SI “; I
6 IF I=1 THEN LET G=32.174
8 IF I<>1 THEN LET G=9.80665
10 INPUT “M1: “; M1, “M2: “; M2
12 A = (M1 – M2)*G / (M1 + M2)
14 T = M1 * (G-A)
16 PRINT “Accel.: “; A
18 PRINT “Tension: “: T
20 END

Example:  M1 = 11.82 kg, M2 = 9.38 kg, use SI units (enter anything other than 1 at the 1:US, ELSE:SI prompt)

Results:
Accel.: 1.128689906 m/s^2
Tension: 102.5734883 N


Eddie

All original content copyright, © 2011-2018.  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.  Please contact the author if you have questions.

Sunday, June 26, 2016

Fun with the HP 71B II

Fun with the HP 71B II

Four years ago, I had fun with the HP 71B programming:



That has been too long ago.  Time to pull out the 71B again.



Digital Root of an Integers

Add all the numbers of an integer and repeat until you have a single digit (1-9).

Program DROOT (52 bytes)

10 DESTROY N
20 INPUT “INTEGER:”;N
22 N=IP(N)
30 D=1+MOD(N-1,9)
40 DISP D

Easy Traverse Calculation

Calculates the new point knowing the original coordinates, direction, and angle of travel.  The angle 0° comes from due east and rotates counterclockwise (see diagram below). 



Program TRAVEZ (216 bytes)


15 DEGREES
20 INPUT “INIT. EASTING:”;E
25 INPUT “INIT. NORHTING:”;N
30 D=0
40 INPUT “DISTANCE:”;I
45 INPUT “ANGLE:”;A
50 E=I*COS(A)+E
55 N=I*SIN(A)+N
57 D=D+I
60 DISP N;’,’;E
65 PAUSE
70 INPUT “DONE? (Y=1,N=0)”;X
75 IF X=0 THEN 40
90 DISP “TOTAL DIST:”;D

Integral Using Simpson’s Rule

∫ FNF(X) dX from X = a to X = b

Line 10 is where you have to define your function (of X). 

Program SIMPSON  (200+ bytes)

10 DEF FNF(X)=insert function of X here
30 INPUT “LOWER:”;A
32 INPUT “UPPER:”;B
34 INPUT “# PART’NS (EVEN):”;N
36 RADIANS
38 T=FNF(A)+FNF(B)
40 H=(B-A)/N
50 FOR I=1 TO N-1
52 IF FP(I/2)=0 THEN 54 ELSE 56
54 T=2*FNF(A+I*H)+T @ GOTO 58
56 T=4*FNF(A+I*H)+T
58 NEXT I
60 T=T*H/3
62 DISP “INTEGRAL:”;T

Expanding the Binomial (ax+b)^n

BINOMEXP gives the coefficients of the expansion of (ax+b)^n. 

Program BINOMEXP (about 189 bytes)

10 DESTROY A,B,N,L,I,C
12 OPTION BASE 0
14 DISP ‘exapand(Ax+B)^N’ @ WAIT 1
16 INPUT “A,B,N”;A,B,N
18 DIM L(N)
20 FOR I=0 TO N
22 C=FACT(N)/(FACT(I)*FACT(N-I))  \\ FACT is the factorial function
24 L(I)=C*A^(N-I)*B^I
26 DISP “L(“; I; ”):”; L(I); “x^”; N-I  @ PAUSE
28 NEXT I
40 DISP “DONE, CHECK L.”

Synthetic Division

Divide the polynomial p(x) by (x-R).  P is the array of p(x), Q is the array representing the quotient q(x), and E is the remainder.  Hence:  P(x)/(x-R) = Q(x) + E/(x-R)

Program SYNTH (304 bytes)

10 DESTROY P,I,Q,R,E
12 OPTION BASE 0
14 DIPS “P(X)/(X-R)” @ WAIT 1
16 INPUT “DEGREE OF P(X):”;N
18 DIM P(N),Q(N)
20 FOR I=0 TO N
22 DISP “COEF OF x^”;N-1 @ PAUSE
24 INPUT P(I) @ P(I)=Q(I)
26 NEXT I
30 INPUT “R:’;R
40 FOR I=0 TO N-1
42 Q(I+1)=R*Q(I)+P(I+1)
44 NEXT I
50 E=Q(N)
60 DIM Q(N-1)
70 DISP “COEF OF Q(X)”
72 FOR I=0 TO N-1
74 DISP Q(I); “x^”; N-I-1 @ PAUSE
76 NEXT I
80 DISP “REMAIN:”; E; “/(x-“; R; “)”

Dew Point Measurement (in °F)


Program DEWPOINT (159 bytes)

10 DESTROY T,H,V,W,C,D
20 INPUT “TEMP °F”;T
22 C=(T-32)*5/9
26 INPUT “REL HUMIDITY (%):”;H
30 V=(LOG(H%1)+17.27*C/(237.3+C))/17.27
32 W=237.3*V/(1-V)
40 D=9/5*W+32
42 DISP “DEW POINT °F:”;D

// Degree symbol:  [ g ] [RUN] (CTRL) [ A ]

 This blog is property of Edward Shore, 2016


Sunday, December 2, 2012

Numeric CAS - Part 3: Synthetic Division

Synthetic Division

Goal: Divide the polynomial p(x) = a_n x^n + a_(n-1) x^(n-1) + ... + a_0 by (x - r).

p(x) ÷ (x - r) = q_n x^n + q_(n-1) x^(n-1) + ... + q_0 with remainder E

Input: A list of coefficients {a_n, a_(n-1), a_(n-2), ... , a_0} (List 1)

Output: Resulting coefficients {q_n, q_(n-1), ... , q_0, E} (List 2)

Example: (2x^3 + x - 3) ÷ (x - 3) = 2x^2 + 6x + 19 + 54 ÷ (x - 3)

List 1: {2, 0, 1, -3}, R = 3
List 2: {2, 6, 19, 54}


Casio Prizm

POLYSYN
Synthetic Division - 160 bytes

"P(X) ÷ (X-R)"
"{AnX^n,...,A0}"
"LIST:"?→List 1
"R"?→R
List 1 → List 2
For 1→K To Dim List 1 - 1
R × List 2[K] + List 1[K+1] → List 2[K+1]
Next
"LAST TERM = REMAINDER" ◢
List 2


TI-84+

POLYSYN
Synthetic Division - 143 bytes

: Disp "P(X)/(X-R)"
: Disp "{AnX^n,...,A0}"
: Input "LIST:", L1
: Input "R:", R
: L1->L2
: For(K,1,dim(L1)-1)
: R*L2(K)+L1(K+1)->L2(K+1)
: End
: Disp "LAST TERM=", "REMAINDER"
: Pause L2


HP 39gii

POLYSYN
Synthetic Division
11/23/2012

L1 and A are prompted. The resulting is a list of coefficients, with the last term the remainder

Example: (21x^2 + 42x + 144)/(x - 12) = 21x + 294 + 3672/(x-12)
Output List: {21, 294, 3672}

EXPORT POLYSYN()
BEGIN
LOCAL K,S,T;
EDITLIST(L1);
INPUT(R,"P(X)/(X-R)");
L1 → L2;
SIZE(L1)→ S;
S-1 → T;
FOR K FROM 1 TO T DO
R * L2(K) + L1(K+1) → L2(K+1);
END;
MSGBOX("LAST TERM=REMAINDER");
RETURN L2;
END;




This blog is property of Edward Shore. 2012




The MU Key on a Four Function Calculator and Programs for the DM42/HP 42S

  The MU Key on a Four Function Calculator and Programs for the DM42/HP 42S Not too long ago, I purchased this very colorful, four funct...