Saturday, November 17, 2018

TI-84+ and Casio (fx-CG 50) Micropython: Simplifying Nested Radicals

TI-84+ and Casio (fx-CG 50) Micropython: Simplifying Nested Radicals

Introduction 

The following program will simplifying the following expression:

√(x + y) = √a + √b

The input is x and y, with output a and b.  The following conditions are implied:  x > y and x > 0.

To see the derivation of simplification and examples, please check out this blog entry:

https://edspi31415.blogspot.com/2018/11/simplifying-nested-radicals.html

TI 84 Plus Program:  DENEST

"EWS 2018-11-12"
Disp "√(X+Y)=√(A)+√(B)","X>Y","X>0"
Prompt X,Y
X/2+1/2*√(X^2-Y^2)→A
X/2-1/2*√(X^2-Y^2) →B
√(A)→Z
round(Z,9)→Z
Disp " "

If Y<0 font="">

Then
If fPart(Z)=0
Then 
Disp √(A),"- √(",B,")"
Else 
Disp "√(",A,")- √(",B,")"
End

Else
If fPart(Z)=0
Then 
Disp √(A),"+ √(",B,")"
Else 
Disp "√(",A,")+ √(",B,")"
End

End

Casio Micropython (fx-CG 50) Script denest.py

Input is the form of:

√(x1 * √x2 + y1 * √y2) = √a + √b

import math
print("sqrt(x+y)=")
print("sqrt(a)+sqrt(b)")
print("x>y","x>0")

print(" ")
print("x1*sqrt(x2)")
x1=float(input("x1:"))
x2=float(input("x2:"))
x=x1*math.sqrt(x2)

print(" ")
print("y1*sqrt(y2)")
y1=float(input("y1:"))
y2=float(input("y2:"))
y=y1*math.sqrt(y2)

a=x/2+math.sqrt(x**2-y**2)
b=x/2-math.sqrt(x**2-y**2)
z=math.sqrt(a)
z=round(z,10)
a=round(a,10)
b=round(b,10)

if y<0: font="">
  if z-int(z)==0:
    print(z,"-sqrt(")
    print(b,"?")
  else:
    print("sqrt(",a)
    print("-sqrt(")
    print(b,")")
else:
  if z-int(z)==0:
    print(z,"+sqrt(")
    print(b,")")
  else:
    print("sqrt(",a)
    print("+sqrt(")
    print(b,")")


Source:

Michael J. Wester, Editor.  Computer Algebra Systems: A Practical Guide John Wiley & Sons: Chichester 1999.  ISBN 978-0-471-983538

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.

Thursday, November 15, 2018

TI-84+ and Casio (fx-CG 50) Micropython: Simplifying A sin x + B cos x To r sin (x + θ)

TI-84+ and Casio (fx-CG 50) Micropython:  Simplifying  A sin x + B cos x To r sin (x + θ)

Introduction

Simplify the expression:

(I)   a * sin x + b * cos x

to an expression that involves only one trigonometric function. 

The first task is to multiply the expression by √(a^2 + b^2)/√(a^2 + b^2). (refer to Source)  This results as:

(II) a* √(a^2 + b^2)/√(a^2 + b^2) * sin x + b * √(a^2 + b^2)/√(a^2 + b^2) * cos x

Refer to the triangle diagram below and it will should become clear why it is desirable to multiply by √(a^2 + b^2)/√(a^2 + b^2). 



With:

(III)
 √(a^2 + b^2) * a/√(a^2 + b^2) * sin x +  √(a^2 + b^2) * b/√(a^2 + b^2) * cos x
=  √(a^2 + b^2) * cos θ * sin x +  √(a^2 + b^2) * sin θ * cos x
=  √(a^2 + b^2) * (cos θ * sin x +  sin θ * cos x)

With the use of the trigonometric identity:
sin(x + y) = sin x * cos y + cos x * sin y

This becomes:
(IV)
 =  √(a^2 + b^2) * sin(x + θ)

Let's make some observations.

1.  The angle θ dependents on what quadrant the point (a,b) is. 

2.  Taking into consideration of where the point (a,b) is and the expression √(a^2 + b^2), the task of finding the coefficients of reduced formula for a * sin x + b* cos x can be achieved by calculating a rectangular to polar conversion of the point (a,b). 

Hence: (IV) becomes:

(V)
 a * sin x + b * cos x =  r * sin(x + θ)

where
r = √(a^2 + b^2)  = abs(a + bi)
θ = atan(b/a) = atan2(b,a) = arg(a + bi)


TI 84 Plus Program:  SCTOSIN

"EWS 2018-11-12"
a+bi
Disp "A*sin(X)+B*cos(X)", "=R*sin(X+θ)"
Prompt A,B
abs(A+B*i)→R
angle(A+B*i)→θ
Disp R,"sin(X+", θ, ")"

Casio Micropython (fx-CG 50) Script scotosin.py

import math
print("a*sin(x)+b*cos(x)")
print("=r*sin(x+t)")
a=float(input("a:"))
b=float(input("b:"))
r=math.sqrt(a**2+b**2)
t=math.atan2(b,a)
print(" ")
print(r)
print("*sin(x+")
print(t,")")

Examples
(Radians mode assumed)

Example 1:
4 * sin x + 2 * cos x = 4.472135955 * sin(x + 0.463647609)

Input:
a = 4
b = 2

Output:
r = 4.472135955
θ = 0.463647609



Example 2:
3 * sin x - 5 * cos x = 5.803951895 * sin(x - 1.030376827)

Input:
a = 3
b = -5

Output:
r = 5.803951895
θ = -1.030376827


Source:
Dugopolski, Mark  Trigonometry Addison Wesley: Boston 2003 pp 211-212  ISBN 0-201-70338-6

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.

Tuesday, November 13, 2018

TI-84+ and Casio Micropython (fx-CG50): Solving Equations Involving Factorials (Inverse Factorial)

TI-84+ and Casio Micropython (fx-CG50):  Inverse Factorial of Integer

Introduction

The goal is to solve the equation for n:

x = n!

One way to do this is to use a variation of the gamma function which invovles an improper integral:

x = ʃ (t^ n * e^(-n) dt, 0, ∞)

(note that n! = gamma(n+1) )

Or use an approximation formula.

Another approach is to use an iterative method, which does not use calculus.  Hence, if x and n are integers then x can be written as:

x = n! + r

Where r is a remainder.  This method involves successive division.

Loop:

1.  Set n = 2  (since 1! = 1) and set t = x.  The  variable t will be working copy of x.
2.  Divide t by n and store in t, t = t / n
3.  Increase n by 1, n = n + 1
4.  If t is less than or greater than n, repeat steps 2 and 3.
5.  If t = n, then x = n!.  Done.
6.  If t ≠ 1, then do the following.  Set n = n - 1 and r = x - n!.  The answer is x = n! + r.

Example 1:    120 = n!

n = 2, t = 120/2 = 60,  60 > 3
n = 3,  t = 60/3 = 20,  20 > 4
n = 4,  t = 20/4 = 5, 5 = 5

Since 5 = 5, 120 = 5!

Example 2:   177 = n!

n = 2, t = 177/2,  88.5 > 3
n = 3, t = 88.5/3, 29.5 > 4
n = 4,  t = 29.5./4, 7.375 > 5
n = 5, t = 7.375/5, 1.475 < 6.  Stop

r = 177 - 5!  = 57

Hence:  177 = 5! + 57

TI-84 Plus Program INVFACT

"EWS 2018-11-11"
Disp "X = N! + R"
Input "X: ", X
X→T
2→N
Repeat T≤N
T/N→T 
N+1→N
End 
If X-N!=0
Then
Disp N, "!"
Else
N-1→N
X-N!→R
Disp N, "! +", R
End

Casio Micropython (fx-CG 50) Script invfact.py

import math
print("x = n! + r")
x=float(input("x: "))
t=x
n=2
while t>n:
  t=t/n
  n=n+1
f=1
for i in range (1, n+1):
  f=f*i
if x-f==0:
  print(n,"!")
else:
  n=n-1
  f=f/i
  r=int(x-f)
  print(n, "! +",r)

Examples:

24 = 4!

26 = 4! + 2

53 = 4! + 29

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.


Saturday, November 10, 2018

HP Prime: The Period of a Pseudo Random Number Generator

HP Prime:  The Period of a Pseudo Random Number Generator

Introduction

Most scientific calculators, mathematical programs, and spreadsheet programs will have random number generators that will generate numbers at random.  The most common generator is a one that produces numbers between 0 and 1.  Additional random functions will also allow the user to generate random integers and random numbers that fit the normal distribution.

It wasn't always this way.

In the 1970s and into the 1980s, scientific calculators and some computers did not have random number functions.  This facilitated a need for pseudo random number generators, hereafter noted as PRNG, which date back to the 1940s.  PRNGs used recursive functions to generate random numbers between 0 and 1.  Several examples include:

x_n+1 = frac(997 * x_n)

x_n+1 = frac(147 * x_n)

x_n+1 = frac( (π + x_n)^5 )

You select a seed, which the RPNG starts generating random numbers.  For example, using the PRNG of x_n+1 = frac(147 * x_n) with a seed of π/3 generates the random numbers:

0.938040026
0.891883822
0.106921834
0.717509598
0.473910906
0.664903182
and so on..



HP Prime Program RNGTEST

The program RNGTEST tests the period of a pseudo random number generator, given a seed. The period of a pseudo random number generator at a given seed is how many iterations it takes for the generator to return to it's initial (first) random number.  For example, if a generator's first random number is 0.1 and it takes 20 iterations for the generator to reach 0.1 again, then the period is 20.

The program sets an arbitrary maximum limit, set in the code of 50,000.  If the count reaches this limit, then the period of the generator is at least this maximum, possibly infinity.  You may change this limit as wished.  Thankfully the HP Prime's processor is fast, so the calculator can handle many interactions quickly.  Older calculators will require more time. 

The program:

rngf(x);  // random number generator

EXPORT RNGTEST(s)
BEGIN
// s = seed
// 2018-11-04 EWS
LOCAL i,k,x:=s;
// initial point
i:=rngf(x);
k:=0;
PRINT();
// loop
REPEAT 
x:=rngf(x);
PRINT(k+" , "+x);
k:=k+1;
UNTIL (x==i AND k>1) OR k>50000;
RETURN k-1;
END;

rngf(x)
BEGIN
RETURN FP(997*x);
END;

Note:  In the rngf(x) subroutine, you can insert any pseudo random number generator you want.

Testing a Simple PRNG

I had set up the program to test a simple PRNG x_n+1 = frac(997 * x_n). 

If the seed is picked with one decimal point (0.1 through 0.9), the period is only 4.  That means, the generator cycles only 4 numbers before repeating again, with the exception is 0.5, which period is only 1.  Specifically:

The seed 0.1 generates 0.1, 0.7, 0.9, 0.3, and then repeats.

The seed 0.2 generates 0.2, 0.4, 0.8, 0.6, and then repeats.

The seed 0.3 generates 0.3, 0.1, 0.7, 0.9, and then repeats.

The seed 0.4 generates 0.4, 0.8, 0.6, 0.2 and then repeats.

The seed 0.5 generates 0.5 and only 0.5.

You can see the repeating periods below. (click on the picture below)



For two decimal point seeds 0.01 to 0.99 (leaving out 0.10, 0.20, etc), from an random sample, you either get a period of 20, or if the seed ends in 5, a period of 4.

For three decimal points seeds 0.001 to 0.999 (leaving out 0.100, 0.110, etc), from a random sample, you either get a period of 100, or if the seed ends in 5, a period of 20.

For simple PRNGs, you are going to a seed with many decimal places to be able to generate different random numbers.

Source:

Shammas, Namir  "New Pseudo Random Number Generators:  Part 1" 2015.  http://www.namirshammas.com/NEW/prng1.pdf  Retrieved October 28, 2018.

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.

Monday, November 5, 2018

Simplifying Nested Radicals

Simplifying Nested Radicals

Introduction

A nested radical is a radical of the form:

√(x_0 + √(x_1 + √(x_2 + .... + √(x_k) ... )))

A expression of radicals (not necessarily square roots) that contains radicals or surds (unresolved n th roots).  Example of a nested radicals include:

√(8 +  √3)

√(8 +  2 * √3)

√(√8 +  √3)

√(√8 -  √3)

This blog entry will deal with the denesting the following:

√(x + y) and √(x - y) where x and/or y is a radical.  Let's assume that x > y and only principal roots are calculated. We're looking into a neat process that is used in computer algebra systems.

Derivation

We want to obtain the following and determine a and b. 

(I) √(x + y) = √a + √b

and

(II) √(x - y) = √a - √b


Start by squaring both sides of (I):

(√(x + y))^2 = (√a + √b)^2

(III) x + y = a + 2 * √(a*b) + b

(IV)  Set:
x = a + b
y = 2 * √(a*b)

(V)  Determine  x - y:

x - y = a + -1*2 * √(a*b) + b  (see IV)
x - y = a - 2 * √(a*b) + b
√(x - y) = √a - √b

(VI)

√(x + y) * √(x - y) = (√a + √b) * (√a - √b)
√((x + y) * (x - y)) = (√a)^2 - (√b)^2
√(x^2 - y^2) = a - b


We have the following simultaneous equations set up:

(VII)
a + b = x     (from (IV))
a - b = √(x^2 - y^2)   (from (VI))

Solving the system in (VII) for both a and b yield:

(VIII)
a = x/2 + 1/2 * √(x^2 - y^2)
b = x/2 - 1/2 * √(x^2 - y^2)

Substituting a and b back in (I) and (II):

(IX)

√(x + y) = √(x/2 + 1/2 * √(x^2 - y^2)) + √(x/2 - 1/2 * √(x^2 - y^2))

√(x - y) = √(x/2 + 1/2 * √(x^2 - y^2)) - √(x/2 - 1/2 * √(x^2 - y^2))

Examples

Example 1:  Simplify  √(8 + 2 * √15)

x = 8
y = 2 * √15

x^2 - y^2
= 8^2 - (2 * √15)^2
= 64 -  4 * 15
= 4

Then: 

√(x/2 + 1/2 * √(x^2 - y^2))
= √(4 + 1/2 * √4)
= √(4 + 1/2 * 2)
= √5    // √a

And:

√(x/2 - 1/2 * √(x^2 - y^2))
= √(4 - 1/2 * √4)
= √(4 - 1/2 * 2)
= √3     // √b

Hence:

√(8 + 2 * √15) = √5 + √3

Example 2:  Simplify  √(88 + 2 * √567)

x = 88
y = 2 * √567

x^2 - y^2
= 88^2 - (2 * √567)^2
= 7744 - 2268
= 5476

Note √5476 = 74

Then: 

√(x/2 + 1/2 * √(x^2 - y^2))
= √(44 + 1/2 * 74)
= √81
= 9        // √a

And:

√(x/2 + 1/2 * √(x^2 - y^2))
= √(44  - 1/2 * 74)
= √7      // √b

√(88 + 2 * √567) = 9 + √7


Example 3:  Simplify √(19 - 4 * √22))  (note the subtraction)

x = 19
y = 4 * √22

x^2 - y^2
= 19^2 - (4 * √22)^2
= 9

√(x/2 + 1/2 * √(x^2 - y^2))  = √11
√(x/2 + 1/2 * √(x^2 - y^2))  = √8 = 2 * √2

Hence:

√(19 - 4 * √22))  = √11 - 2 * √2


Neat algebra.

Source:

Michael J. Wester, Editor.  Computer Algebra Systems: A Practical Guide John Wiley & Sons: Chichester 1999.  ISBN 978-0-471-983538


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, November 4, 2018

HP Prime Firmware Update: Firmware 14181

HP Prime Firmware Update



The newest firmware update (version 14181) is now released.  Changes include:

* New function UVAL and UPART.  UVAL extracts the value from a unit object (example:  10 from 10_m) while UPART extracts the unit part (example 1_m from 10_m).

* The DMS key [Shift] (° ' '' )  now will work everywhere.  

* The fraction part (FP) and integer part (IP) functions will work with unit objects.

* Large matrices, strings, and other objects will now be displayed with an ellipsis (...).  To see the full object, scroll up, select the big expression and press on the soft key (Show). 


Link to the updated firmware:  ftp://ftp.hp.com/pub/calculators/Prime/



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.

Thursday, November 1, 2018

(OT) JDRF Walk - November 4, 2018

Hi everyone, I am Edward Shore, author of Eddie's Math and Calculator Blog, and I am joining Derek Theler on the JDRF walk at the Rose Bowl. 

The walk is to raise funds for research to find a cure for type 1 diabetes.  The walk will be on November 4, check in at 8:00 AM starting at 10. 

The walk is 5 kilometers which is approximately 3.11 miles. 

Hope to see you on Sunday and thank you for your support.

http://www2.jdrf.org/goto/ews

Eddie

TI-84+ and Casio (fx-CG 50) Micropython: Simplifying Nested Radicals

TI-84+ and Casio (fx-CG 50) Micropython: Simplifying Nested Radicals Introduction  The following program will simplifying the following...