Showing posts with label rational expressions. Show all posts
Showing posts with label rational expressions. Show all posts

Sunday, May 31, 2020

HP12C: Rational Fractions and Horner's Method

HP12C: Rational Fractions and Horner's Method

Introduction

Here are three examples on how Horner's Method can be used to quickly calculate rational fractions with polynomials.   The program code is presented for the HP 12C.

Horner's rule involves repeated factoring until the polynomial is represented as a multiplication of polynomials.  The idea is to make it easier for some scientific calculators and four-function calculators to evaluate polynomials.  Using Horner's Method for the generic cubic polynomial:

a * t^3 + b * t^2 + c * t + d
t * (a * t^2 + b * t + c) + d
t * (t * (a * t + b) + c ) + d

On an RPN keystroke calculator, such as the HP 12C a possible code would look like:

STO  t   (from the X stack)
RCL a
*
RCL b
+
RCL t
*
RCL c
+
RCL t
*
RCL d
+
RTN

For the code below, the HP 12C uses the following registers:

R0 = x

R1 = a
R2 = b
R3 = c
R4 = d
R5 = e
R6 = f
R7 = g

All but x need to be stored ahead of time before running (you can change the code to suit your needs, of course).  x is entered before pressing [ R/S ].

For all of our numerical examples, I assigned the following values:

R0 = 1.72

R1 = 6
R2 = 3
R3 = 4
R4 = 5
R5 = 3
R6 = 1
R7 = 8

Example 1

(ax + b) / (cx^2 + dx + e) = (ax + b) / (x * (cx + d) + e)

Program  (key:  key code) - 16 steps

STO 0:  44, 0
RCL 1:  45,1
  *   :  20
RCL 2:  45, 2
  +  :  40
RCL 0:  45, 0
RCL 3:  45, 3
  *  :   20
RCL 4:  45, 4
  +  :  40
RCL 0:  45, 0
  *  :  20
RCL 5:  45, 5
  +  :  40
  ÷  :  10
GTO 00:  43, 33, 00

Result with variables stored above (FIX 4):  0.5684

Example 2

(ax + b) / (cx^3 + dx^ 2 + ex + f) = (ax + b) / (x * (x * (cx + d) + e) + f)

Program  (key:  key code) - 20 steps

STO 0:  44, 0
RCL 1:  45,1
  *   :  20
RCL 2:  45, 2
  +  :  40
RCL 0:  45, 0
RCL 3:  45, 3
  *  :   20
RCL 4:  45, 4
  +  :  40
RCL 0:  45, 0
  *  :  20
RCL 5:  45, 5
  +  :  40
RCL 0:  45, 0
  *  :  20
RCL 6:  45, 6
  +  :  40
  ÷  :  10
GTO 00:  43, 33, 00

Result with variables stored above (FIX 4):  0.3225

Example 3

(ax^2 + bx + c) / (dx^3 + ex^2 + fx + g)
= (x * (ax + b) + c) / (x * (x * (dx + e) + f) + g)

Program  (key:  key code) - 24 steps

STO 0:  44, 0
RCL 1:  45,1
  *   :  20
RCL 2:  45, 2
  +  :  40
RCL 0:  45, 0
RCL 3:  45, 3
  *  :   20
RCL 4:  45, 4
  +  :  40
RCL 0:  45, 0
  *  :  20
RCL 5:  45, 5
  +  :  40
RCL 0:  45, 0
  *  :  20
RCL 6:  45, 6
  +  :  40
RCL 0:  45, 0
  *   :  20
RCL 7:  45, 7
  +  :  40
  ÷  :  10
GTO 00:  43, 33, 00

Result with variables stored above (FIX 4):  0.6111


Eddie

All original content copyright, © 2011-2020.  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, May 29, 2016

HP Prime: Basic CAS Commands for Polynomials and Rational Expressions

HP Prime:  Basic CAS Commands for Polynomials and Rational Expressions



Define the following variables:

poly:  a polynomial of the form a_n*x^n + a_n-1*x^(n-1) + …. + a_1*x + a_0
rat:  a rational function consisting of the polynomials p(x)/q(x)
var: variable

Simplification Modes

Before we start, I want to comment on simplification modes.  

Simplification Modes:

None:  No simplification is executed
Minimum:  Simple simplification of results.  Additional simplification may be desired.
Maximum:  Full simplification of results

Below is a comparison between Minimum and Maximum modes.



To change Simplification mode, press [Shift], [CAS] (CAS Settings), and select a Simplification settings.  Select the Simplification drop down box and select the desired mode.    

Note:  The following examples are executed in Maximum simplification mode.

Part Extraction:  Coefficients, Numerator, Denominator

Coefficients

coef:  [Toolbox], (CAS), 6. Polynomial, 2.  Coefficients

Syntax:
coef(poly, var):  returns all the coefficients of a polynomial in a vector
coef(poly, var, n): return the coefficient of a polynomial in a vector of the specific power x^n

Examples:
coef(2x^2 + 3x – 1, x) returns [2, 3, -1]
coef(2x^2 + 3x – 1, x, 2) returns 2
coef(2x^2 + 3x – 1, x, 1) returns 3
coef(2x^2 + 3x – 1, x, 0) returns -1

Numerator and Denominator

numer: [Toolbox], (CAS), 1. Algebra, 8. Extract, 1.  Numerator
denom: [Toolbox], (CAS), 1. Algebra, 8. Extract, 2. Denominator

Syntax:
numer(rat)
denom(rat)

Example:
f≔(2x^2-3)/(x^2+1)
numer(f) returns 2*x^2 – 3
denom(f) returns x^2 + 1
purge(f) \\ this is to erase f

Polynomial Creation

Create a symbolic polynomial with a list of coefficients

poly2symb: [Toolbox], (CAS), 6. Polynomial, 7.  Create, 2. Coefs→Poly

Syntax:

poly2symb(vector of coefficients, var)

Example:

poly2symb( [8, -1, 0, 6], t) returns 8*t^3 – t^2 + 6

The inverse operation is the symb2poly(poly), which can be accessed by [Toolbox], (CAS), 6. Polynomial, 7. Create, 1. Poly→Create. 

Create a polynomial from a list of roots

This involves a two-step processes:
1. pcoeff( vector of roots )
2. poly2symb( result from step 1, var )

Access pcoeff: [Toolbox], (CAS), 6. Polynomial, 7. Create, 3.  Roots → Coef

You can combine the two steps by typing:  poly2symb(pcoeff( vector of roots), var).  Simplification may be required

Example:

pcoeff([-2, 5, 0, 6]) returns [1, -9, 8, 60, 0]
poly2symb([1, -9, 8, 60, 0],t) returns t^4 – 9*t^3 +8*t^2 + 60*t

poly2symb( pcoeff([2, -5, 0, 6]), t) returns 
t^4 – 9*t^3 + 8*t^2 + 60*t

Degree of a Polynomial

degree: [Toolbox], (CAS), 6, Polynomial, 8. Algebra, 3. Degree

Syntax:

degree(poly)

Example:
f: = 4*x^3 – 2*x^2 + 8*x – 8
degree(f) returns 3

You can factor a polynomial by x^n where n is the degree of the polynomial by using factor_xn.

factor_xn:  [Toolbox], (CAS), 6. Polynomial, 8. Algebra, 4. Factor By Degree
Caution:  This command works when the Simplification mode is turned to Minimum or Off.    

Example:
(after turning Simplification to Minimum)
factor_xn(f) returns x^3 * (4 – 2/x + 8/x^2 – 8/x^3)
purge(f)

Partial Fraction of Rational Functions

partfrac: [Toolbox], (CAS), 1. Algebra, 7. Partial Fraction

Syntax:

partfrac(rat)

Example:

partfrac( (x^4 – 3*x^3)/(x^2 -1) ) returns 
x^2 -3*x + 1 - 1/(x-1) - 2/(x+1)

Determining the Number of Zeros (Roots)

The sturmab command determines the number of zeros giving an interval.

sturmab:  [Toolbox], (CAS), 6. Polynomial, 8. Algebra, 6.  Zero Count

Syntax:

sturmab(poly, var, min, max)

The min and max can be complex numbers.

Examples:

sturmab(x^3 – 4*x^2 + 6*x – 4, x, -5, 5) returns 1  \\ 1 real root
sturmab(x^3 – 4*x^2 + 6*x – 4, x, -5-5*i, 5+5*i) returns 3  \\ 1 real, 2 complex roots

Polynomial Functions

Path: [Toolbox], (CAS), 6, Polynomial, 9. Special, then:
4.  Hermite \\ Syntax:  hermite(n)
7.  Legendre \\ Syntax:  legendre(n)
8.  Chebyshev Tn (1st Kind)  \\ Syntax:  tchebyshev1(n)
9.  Chebyshev Un (2nd Kind)  \\ Syntax:  tchebyshev2(n)

Where n is an integer.  You can specify a variable by adding var as a second argument. (x is the default variable)

Examples:
hermite(4) returns 16*x^4 – 48*x^2 + 12
legendre(4) returns (35*x^4 – 30*x^3 + 3)/8
tchebyshev1(4) returns 8*x^4 – 8*x^2 + 1
tchebyshev2(4) returns 16*x^4 – 12*x^2 + 1

This covers some of the basic CAS commands for polynomials and rational functions.  Hope you find this helpful,

Eddie


This blog is property of Edward Shore, 2016.








My International Casio Collection (So Far)

 Pictured: Pic 1:  Casio fx-991CN X (China)  Pic 2:  Casio fx-570SPX II Iberia (Spain) Pic 3:  Casio fx-92 Collège (France) All original co...