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, September 28, 2024

TI-84 CE and TI-86: Matrix Row Statistics

TI-84 CE and TI-86: Matrix Row Statistics


Introduction


The program MROWST will take a matrix and a desired row and calculate three statistics points:


* Sum of the row

* Mean of the row

* Difference Vector: row – mean for each element


TI-84 CE Program MROWST


Matrices used: [ J ] (entry matrix), [ I ] (difference vector)


Input “MATRIX? “, [ J ]

Input “ROW? “, R

dim([ J ])

Ans(2) → D

0 → S

For(I, 1, D)

S + [ J ](R, I) → S

End

S / D → M

{1, D} → dim([ I ])

For(I, 1, D)

[ J ](R, I) – M → [ I ](1, I)

End

ClrHome

Disp “SUM: “ + toString(S)

Disp “MEAN: “ + toString(M)

Disp “DIFF VECTOR:”

Pause [ I ]


TI-86 Program MROWST


Matrices used: MJ (entry matrix), MD (difference vector), mone (vector of ones)


Input “Matrix? “, MJ

Input “Row? “, R

dim MJ

Ans(2) → D

D → dim mone

Fill(1, mone)

dot(mone, MJ(R)) → S

S / dim mone → M

MJ(R) - (M * mone) → MD

ClLCD

Disp “Sum:”, S

Disp “Mean:”, M

Disp “Diff Vector:”

Pause MD


Example


Matrix:

[ [ -9, -4, -1, -9, 4 ]

[ -9, -7, -4, -4, 9 ]

[ 7, 1, -9, -7, 8 ] ]


Row 1:

Sum: -1

Mean: -0.2

Difference Vector: [ 9.2, -3.8, -0.8, -8.8, 4.2 ]


Row 2:

Sum: -15

Mean: -3

Difference Vector: [ -6, -4, -1, -1, 12 ]


Row 3:

Sum: 0

Mean: 0

Difference Vector: [ 7, 1, -9, -7, 8 ]



Source:

Stuerke, Cecil. “Demonstration of Principal Component Analysis on TI-86” IEEE 2008



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.

Sunday, September 15, 2024

Numworks: Allowing Repeated Calculations in Python

Numworks: Allowing Repeated Calculations in Python



Introduction


Say we want the user to repeat a calculation or a routine for as long as the user wants. At the end of each calculation, the user is asked if they want another calculation. The user enters one of two possibilities:


1 for an additional calculation

0 for no more calculation


A flag variable is set up with a default value of 1. As long as this value remains at 1, the calculation repeats.


Scrip structure (I named this variable rptflag, but it can be any name):


rptflag=1


while rptflag!=0:

  …

  < insert inputs and show results >

  …

  print(“Again? No = 0, Yes = 1”)

  rptflag=eval(input())


Entering 0 for the Again? Question exits the loop. We need the eval (evaluate) or int (input) because leaving the input command alone defaults the input to a string.


For the sample scripts below, the escape velocity off the surface of planets and other celestial objects are calculated.


Repeated Loop: General


This script does not require any special modules.


Script: rptdemo1 (We can use this on any calculator or platform)


from math import *


# repeat demonstration 

# set up repeat flag

rptflag=1


while rptflag!=0:

  print("ESCAPE VELOCITY")

  m=eval(input("Mass (kg)? "))

  r=eval(input("Radius (m)? "))

  v=sqrt(1.33486e-10*m/r)

  print("Velocity: ",v,"\nm/s")

  print("Again? no=0, 1=yes")

  rptflag=eval(input())




Repeated Loop: Numworks – using the keyboard


Instead of having the user enter 0 or 1, the user presses the keys 0 or 1. To allow the user key presses, use the Ion module. KEY_ZERO is for the 0 key and KEY_ONE is for the 1 key.


To make the screen look presentable, I’m clearing the screen. This is accomplished by the use of the Kandinsky module. The use of the Kandinsky module was suggest to me on Reddit by Feeling_Walrus3033, and I give credit and gratitude for the suggestion.


The line:


fill_rect(0,0,320,240,(255,255,255)) creates a blank white screen.


Clearing the screen this way will call for a way to display text on the screen. Unfortunately the print() command won’t do it. Kandinsky comes to the rescue with the draw_string command. The syntax for the draw_string command is:


draw_string(“text” or variable containing the string, x pixel, y pixel, [text color], [background color])


Colors are optional. The default is black text on white background.


Keep in mind that using this method will require more memory.


Script: rptdemo2 (Specific to Numworks, for other platforms and calculator, check your specific manual)


from math import *

from ion import *

from kandinsky import *

# repeat demonstration 


# set up keys

def key():

  while True:

    if keydown(KEY_ZERO):

      return 0

    if keydown(KEY_ONE):

      return 1


# set up repeat flag

rptflag=1


while rptflag==1:

  print("ESCAPE VELOCITY")

  m=eval(input("Mass (kg)? "))

  r=eval(input("Radius (m)? "))

  v=sqrt(1.33486e-10*m/r)

  

  # build strings

  s1="Velocity: "+str(v)+" m/s"

  s2="Again? no=0, 1=yes"

  fill_rect(0,0,320,240,(255,255,255))

  draw_string(s1,0,0)

  draw_string(s2,0,20)

  rptflag=key()


draw_string("DONE",0,40,(255,0,0))



Sample Data to Try



Mass (kg)

Radius (m)

Escape Velocity (m/s)

Earth

5.972168E24

6378.137E3

11179.88618486758

Jupiter

1.8982E27

71492E3

59533.32250562

Sun (Solar System)

1.9885E30

6.957E8

617688.6988877566


E: [ ×10^x ] button, shown as “e”



Note: I will be in Nashville for the 2024 HP Handheld Conference on September 21-22, 2024. The next scheduled post will be on September 28, 2024.


Take care,


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.



Saturday, September 14, 2024

HP Prime CAS: Riemann-Louiville Integral vs Taking the Indefinite Integral Twice

HP Prime CAS: Riemann-Louiville Integral vs Taking the Indefinite Integral Twice



Introduction


The Riemann-Louiville Integral takes the integral of the function f(x) of any positive order v. The integral is defined as:


c_D_x^(-v) = 1 / Γ(v) * ∫( (x – t) * f(t) dt, t = c, t = x)


where:

c = a real constant, which can be zero

f(x) = function of x

t = dummy variable of integration

‘v = order where v >0


If v=1, this is the regular integral. However, the value of v can be a positive non-integer. If v=2, then the Riemann-Louiville integral is a result if you integrate the function twice.


This is one of the formulas on determining indefinite integrals of various orders.



HP Prime CAS Function: dblint


Double Integral of f(x) which takes the integral of f(x) twice. The variable x is used in the function.


dblint(f):= ∫∫ f dx dx


HP Prime CAS Function: rlint


The Riemann-Liouville Integral of f(x). The input has the variable x as the independent variable. The result of the function returns t as the independent variable.


rlint(f,c,v):=(∫(t–x)^(v–1)*f,x,c,t)) / Gamma(v)


Note that the variables x and t are switched to allow the input to be a function of x.


Notes


This was programmed on the CAS page in the format:


func(var) := function


I was not able to use the Program Editor mode at time of programming (July 30, 2024). (Beta Firmware 15048)


Examples



Double Integral: dblint

RLI, v = 2: rlint with c = 0

f(x) = x^m, m>0

x^(m+2) / (m^2 + 3*m + 2)

t^(m+2) / (t^2 + 3*t + 2)

f(x) = a*x + b

(a*x^3 + 3*b*x^2) / 6

(a*t^3 + 3*b*t^2) / 6

f(x) = e^x

e^x

-t + e^t – 1

f(x) = cos x

-cos x

-cos t + 1


Taking the derivative twice will return us back to the original function. Note that the indefinite integral function assumes that the added constant is zero ( ∫ f(x) dx = F(x) + C ).



Source


Kimeu, Joseph M., "Fractional Calculus: Definitions and Applications" (2009).Masters Theses & Specialist Projects. Paper 115. http://digitalcommons.wku.edu/theses/115



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.

Sunday, September 8, 2024

HP Prime and Numworks: Plotting a Parametric Line of Motion

 HP Prime and Numworks: Plotting a Parametric Line of Motion



Plotting the Position of Motion


This program draws a 2D motion plot from an initial starting point (x, y) given initial velocity and acceleration. The rate and direction of both velocity and acceleration are assumed to be constant.

x(t) = ax * t^2 / 2 + vx * t + x0

y(t) = ay * t^2 / 2 + vy * t + y0

where:

t = number of seconds.

ax = acceleration in the x direction

ay = acceleration in the y direction

vx = initial velocity in the x direction

vy = initial velocity in the y direction



The HP Prime PPL program PLOTMOTION displays a traceable curve with a table of values.

The Numworks python script plotmtn.py uses math and matplot.pyplot modules. A scatter plot is laid on top of the path plot. The plot begins at the initial point where it is marked green.



HP Prime Program: PLOTMOTION



EXPORT PLOTMOTION()

BEGIN

// EWS 2024-07-22



LOCAL ch;

ch:=INPUT({C,D,A,B,V,U,N},

"Motion Plot Per Second",

{"x0:","y0:","vx:","vy:","ax:","ay:","n:"},

{"initial x position",

"initial y position",

"initial velocity x direction",

"initial velocity y direction",

"acceleration x direction",

"acceleration y direction",

"number of seconds"});



// user presses cancel

IF ch==0 THEN

KILL;

END;



// user presses OK

STARTAPP("Parametric");



'V*T^2/2+A*T+C'▶X1;

'U*T^2/2+B*T+D'▶Y1;

CHECK(1);



Parametric.Tmin:=0;

Parametric.Tmax:=N;

Parametric.Tstep:=1;



// table and plot

STARTVIEW(10);

STARTVIEW(9);

END;





Numworks Python Code: plotmtn.py


from math import *
from matplotlib.pyplot import *

# 2024-07-23 EWS

print("Motion Plot per second from (0,0)")
c=eval(input("init. x position? "))
d=eval(input("init. y position? "))
a=eval(input("init. x velocity? "))
b=eval(input("init. y velocity? "))
v=eval(input("acceleration x? "))
u=eval(input("acceleration y? "))
n=int(input("number of seconds? "))

x=[v*t**2/2+a*t+c for t in range(n)]
y=[u*t**2/2+b*t+d for t in range(n)]

x0=min(x)-1
x1=max(x)+1
y0=min(y)-1
y1=max(y)+1
axis((x0,x1,y0,y1))
text(x0,y1-1,"Motion Plot")
plot(x,y,'gray')
scatter(x,y,color='blue',marker="h")
scatter(x[0],y[0],color='green',marker="h")
show()





Source

Tremblay, Christopher. Mathematics for Game Developers. Thomson Course Technology. Boston, MA. 2004. ISBN 1-59200-038-X.


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.