Saturday, August 31, 2024

TI 30Xa Algorithm: The Sigmoid Function and its Integral

 TI 30Xa Algorithm: The Sigmoid Function and its Integral



The Sigmoid Function


The sigmoid function is defined as:


S = 1 / (1 + e^(-x))

If we multiply the last equation by e^(x) / e^(x), we get:

S = e^(x) / (e^(x) + 1) = e^(x) / (1 + e^(x))


The sigmoid function takes functions from the real numbers and maps them to the interval (0,1). If x → -∞, S → 0. If x → +∞, S → 1.


The inverse of the sigmoid function is the logit function, from which we can derive:


S = 1 / (1 + e^(-x))

1 / S = 1 + e^(-x)

1 / S – 1 = e^(-x)

ln( 1 / S – 1 ) = -x

ln( (1 – S) / S ) = -x (Note: 1 / S – 1 = 1 / S – S / S = (1 – S) / S )

-ln( (1 – S) / S ) = x

ln( S / (1 – S)) = x (Note: For any x, -ln(x) = ln(x^(-1)) = ln(1 / x))


The rest of the blog will focus on the sigmoid function.


The Integral of the Sigmoid Function


Funding the area of the curve under the sigmoid function is pretty straight forward.


∫ 1 / (1 + e^(-x)) dx


= ∫ e^(x) / (e^(x) + 1) dx


Let u = e^(x) + 1. The du = e^(x) dx and:


∫ du / u


= ln | u | + C

= ln (e^(x) +1) + C (since e^(x) + 1 > 0 for all real x)


The definite integral (area) can be calculated as:


x = b

∫ e^(x) / (e^(x) + 1) dx

x = a


= ln (1 + e^b) – ln (1 + e^a)


What if we are given the area, A (capital A), from negative infinity to a value x? Here we are finding the lower tail area.


t = x

∫ e^(t) / (e^(t) + 1) dx = A

t = -∞


ln(1 + e^(x)) – ln(1 + e^(-∞)) = A

Note that e^(-∞) → 0 as t → -∞

We can estimate that:

ln(1 + e^(x)) – ln( 1 ) = A

ln(1 + e^(x)) – 0 = A

1 + e^(x) = e^(A)

e^(x) = e^(A) – 1

x = ln(e^(A) – 1)



TI-30Xa Algorithms


Sigmoid function:

S = 1 / (1 + e^(-x)) = e^(x) / (e^(x) + 1)


Algorithm:

[ ( ] x [ +/- ] [ 2nd ] [ LN ] {e^x} [ + ] 1 [ ) ] [ 1/x ] [ = ]


Example:

S(1.5):

[ ( ] 1.5 [ +/- ] [ 2nd ] [ LN ] {e^x} [ + ] 1 [ ) ] [ 1/x ] [ = ]

Result: 0.817574476



Integral of the sigmoid function:

x = b

∫ e^(x) / (e^(x) + 1) dx

x = a


Algorithm:

[ ( ] b [ 2nd ] [ LN ] {e^x} [ + ] 1 [ ) ] [ LN ]

[ - ] [ ( ] a [ 2nd ] [ LN ] {e^x} [ + ] 1 [ ) ] [ LN ] [ = ]


Example:

x = 3

∫ e^(x) / (e^(x) + 1) dx

x = 0


(a = 0, b = 3)

[ ( ] 3 [ 2nd ] [ LN ] {e^x} [ + ] 1 [ ) ] [ LN ]

[ - ] [ ( ] 0 [ 2nd ] [ LN ] {e^x} [ + ] 1 [ ) ] [ LN ] [ = ]

Result: 2.355440171



Find x given lower tail area:

x = ln(e^(A) – 1)


Algorithm:

[ ( ] A [ 2nd ] [ LN ] {e^x} [ - ] 1 [ ) ] [ LN ] [ = ]


Example:

Area: A = 0.5


[ ( ] 0.5 [ 2nd ] [ LN ] {e^x} [ - ] 1 [ ) ] [ LN ] [ = ]

Result: -0.4327521296



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.

Sunday, August 25, 2024

Spotlight: NEWYES GY991 MS

 Spotlight: NEWYES GY991 MS



This is going to be a quick spotlight on the NEWYES GY-991 MS, a clone of the Casio fx-991 MS made in China.













Features


Model: GY-991 MS

Company: NewYes

Modeled after: Casio fx-991 MS

Number of Functions: 401

Display: Two Lines

Battery: Solar, with backup battery L1154F (equivalent LR44)

Price: circa $11 - $12


The GY-991 MS is a two line scientific calculator with many functions including:

* numerical integration

* numerical derivative

* complex arithmetic (R<>I indicator, with ⅈ and ∠ to indicate imaginary and angle parts)

* 40 scientific calculators (circa 2008 values)

* 40 U.S./International unit conversions

* Polar to Rectangular conversion (x stored in E, y stored in F)

* Rectangular to Polar conversion (r stored in E, θ stored in F)


The display has two lines: the top is the calculation while the bottom line is the result. The calculator is algebraic, so you can type expressions as they are written.


The GY-991MS is similar to the Casio fx-991 MS in functionality.


Modes (by repeated presses of the [ MODE ] key):

COMP: Regular

CMPLX: Complex Mode

SD: One-variable statistics

REG: Two-variable statistics

BASE: base conversions and Boolean logic. The four bases available are decimal, binary, octal, and hexadecimal. The Boolean logic functions (and, or, xor, xnor, not, neg, with suffixes) are accessed through the repeated presses of the [ x^-1 ] button.

EQN:

Unknowns: 2 or 3: Simultaneous linear equations of 2 or 3 variables

Degree: 2 or 3: Quadratic or cubic polynomial solver

MAT: Matrix mode, 3 matrices (A, B, C) up to 3 x 3. Operations available: Det (determinant), Trn (transpose), Inverse through the [ x^-1 ]

VCT: Vector mode, 3 vectors (A, B, C), 2D or 3D. Operations available: dot product (through the VCT menu), cross product ([ × ] key), norm ([SHIFT] [ ) ] (Abs))

Angle modes: Deg, Rad, Gra (degrees, radians, gradients)

Display modes: Fix, Sci, Norm

Disp mode: Engineering mode on and off, fraction display, dot/comma decimal separator


Six regression models are available: linear, logarithmic, exponential, power, inverse, quadratic


The keys are plastic, but feel serviceable and responsive. The calculations are comparable. If you are not planning to take classes or take a high-stakes certification exam, it may be an off-brand model to try. It is also a good, inexpensive calculator to use.


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, August 24, 2024

Casio fx-CG50 and Python: Directional Derivative for f(x,y)

Casio fx-CG50 and Python: Directional Derivative for f(x,y)


Introduction


Problem:

Given the following:

* A function of two variables, f(x, y)

* An evaluation point, (x0, y0)

* A directional vector v = < v1, v2 >


Find the directional derivative at the point (x0, y0).


Steps:

1. Determine the gradient of f, ∇f. (∇ is the nabla symbol.) The gradient is a vector of partial derivatives, ∇f = < ∂f / ∂x, ∂f / ∂y >.

2. Evaluate the gradient at the point (x0, y0).

3. Determine the unit vector of the directional vector. Let the norm of | v | = √(v1^2 + v2^2), and the unit vector is determined as u = < v1 / | v |, v2 / | v | > = < u1, u2 >.

4. Take the dot product of the gradient and unit vector: ∇f ⋅ u


The Casio basic code allows for more than one evaluation point.



Casio fx-CG 50 Program: DIRDERIV


Program DIRDERIV

304 bytes


“DIRECTION DERIV.”

Rad

1×10^-4 → H

“NO QUOTES NEEDED”

“F(X,Y)”? → fn1

“DIR VECTOR”? → Vct V

UnitV(Vct V) → Vct U


Lbl 1

“X0”? → R

“Y0”? → S

R + H → X

S → Y

fn1 → A

R – H → X

(A – fn1) ÷ (2 × H) → A

R → X

S + H → Y

(B – fn1) ÷ (2 × H) → B

DotP( [ [ A, B ] ] , Vct U) → D

“[ D _| DX, D _| DIR ] = “ ◢

[ [ A, B, D ] ]

Menu “AGAIN?”, “YES”, 1, “NO” , 0

Lbl 0

“THANK YOU.”



Function memory variables is found through the [ OPTN ] menu. In Run-Matrix Mode, function memory can only be accessed when the Input/Output setup is set at Linear.


Get the _|, the fraction category, by pressing the fraction key, [ []/[] ].



Python Script: dirderv.py


Programmed on the Casio fx-CG50. Only module that is used is the math module. The function f(x,y) is defined in the subroutine in the script.


Code:


from math import *


# define f(x,y) here

def f(x,y):

  return x*sin(y) (or any function you want)


h = 0.0001

print(“Directional Deriv.”)

print(“Directional Vector: “)

vx = eval(input(“vx? “))

vy = eval(input(“vy? “))

n = sqrt(vx**2+vy**2)

ux = vx/n

uy = vy/n


print(“Point (x0,y0)”)

x0 = eval(input(“x0? “))

y0 = eval(input(“y0? “))


a = (f(x0+h,y0) – f(x0-h,y0))/(2 * h)

b = (f(x0,y0+h) – f(x0,y0-h))/(2 * h)

d = a*ux + b*uy


print(“dx=”,a)

print(“dy=”,b)

print(“dir.=”,d)


Examples


Example 1:

f(x,y) = x^3 + y^2

at (x0, y0) = (3, 2)

Directional Vector = < 1, 1 >



Norm: √(1^2 + 1^2) = √2

The unit vector: < 1/√2, 1/√2 >


Partial Derivatives:

∂f / ∂x = 3 * x^2, Value = 1

∂f / ∂y = 2 * y, Value = 1


Directional Derivative:

∇f * u ≈ 21.9203122


Example 2:

f(x,y) = x * sin y

at (x0, y0) = (0.5, -0.2)

Directional Vector = < -π, 0 >



Norm: √((-π)^2 + 0^2) = π

The unit vector: < -1, 0 >


Partial Derivatives:

∂f / ∂x = sin(y), Value ≈ -0.1986693308

∂f / ∂y = x * cos(y), Value ≈ 0.4900332881


Directional Derivative:

∇f * u ≈ 0.1986693308


Source


Dawkins, Paul “Section 13.7: Directional Derivatives”. Paul’s Online Notes. Last Modified November 16, 2022. Accessed July 4, 2024. https://tutorial.math.lamar.edu/classes/calciii/directionalderiv.aspx



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.

Wednesday, August 21, 2024

Swiss Micros DM32 Update: Official Firmware

Swiss Micros DM32 Update:  Official Firmware


The Swiss Micros DM32 is no longer in Beta Phase as the first Official firmware is released.  

 

Link:  https://technical.swissmicros.com/dm32/firmware/


And the official paper DM32 manual is on sale for 49 Swiss Francs:  https://www.swissmicros.com/products


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, August 18, 2024

Casio fx-CG50: Intersection of Two Planes

Casio fx-CG50: Intersection of Two Planes



Introduction


The program INTPLACE calculates a possible line:


r = V + t * D


where V = constant vector, D = directional vector, r = linear vector


The planes are have the equations:


A * X + B * Y + C * Z = D

E * X + F * Y + G * Z = H


The directional vector is calculated by the cross product of vectors:


[ A, B, C ] × [ E, F, G ]

= < B * F – E * C, -(A * F – C * D), A * E – B * D >


A system of equations is solved for two of the variables, with the third variable, often z, designated an arbitrary value, often 0.



Casio fx-CG 50 Program: INTPLACE

Bytes: 312


Due to the mixed use of vectors and matrices on the calculator, I have the program take inputs and store them in variables A through H, so we have the greatest flexibility.


The planes have the equations:


A * X + B * Y + C * Z = D

E * X + F * Y + G * Z = H


There is a for loop to simulate a small wait cycle. There is no Wait command in Casio basic.


Code:


For 1 → I To 25

Locate 1, 2, “INTERSECT 2 PLANES”

Blue Locate 1, 4, “AX+BY+CZ=D”

Red Locate 1, 5, “EX+FY+GZ=H”

Next


ClrText

A”? → A

B”? → B

C”? → C

D”? → D

E”? → E

F”? → F

G”? → G

H”? → H

CrossP([[A,B,C],[[E,F,G]]) → Vct D

[[A,B][E,F]]^-1 × [[D][H]] → VctH

Mat V [1,1] → I

Mat V [2, 1] → J

[[I, J, 0]] → Mat V


ClrText

Vct V + T × Vct D”

0 ≤ T ≤ 1” ◢

Vct V” ◢

Vct V ◢

Vct D” ◢

Vct D



The program assumes that z-component of the calculated directional vector (Vct D) is nonzero. The z-component of the constant vector (Vct V) is assigned to be 0.


Example


3 * X – 5 * Y + Z = 4

X – 2 * Y + Z = 0


A = 3

B = -5

C = 1

D = 4

E = 1

F = -2

G = 1

H = 0


Constant Vector:

Vct V = < 8, 4, 0 >


Directional Vector:

Vct D = < -3, -2, -1 >


A line that intersects plane is:


r(t) = < 8, 4, 0 > + t * < -3, -2, -1 >

for 0 ≤ t ≤ 1


Source


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




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, August 17, 2024

TI-84 Plus CE Python: Diophantine Books Problem and Circles Inscribed in Squares

TI-84 Plus CE Python: Diophantine Books Problem and Circles Inscribed in Squares



Introduction


The two programs presented today are Python versions from the Idea Book (Ahl – see Source), where the original programs were posted in BASIC designed for the Texas Instruments computers in the 1980s. The Python versions were programmed with the TI-84 Plus CE Python (and should work on the TI-83 Premium Python Edition). Unfortunately, the Circle and Squares program may not work on the TI-82 Advanced Python as that calculator does not have any graphics modules.



Diophantine Books Problem


A book store studies the sales of three popular books: A, B, and C, each with prices PA, PB, and PC. We have the total books sold, the sales amount, and the prices of the books. How many of each books were sold?


The program solve the Diophantine system of equations:


A + B + C = N

PA * A + PB * B + PC * C = S


N = number of books sold

S = total sales

A, B, and C are positive integers


The original BASIC program covered a specific case, where in this version has the user enter the price of up to three books, total sales, and number of books sold. The code in Python also counts the number of solutions.



Code: books.py


# Math Calculations

from math import *


# Browns Books, pg 43

# TI HOME IDEA BOOK, 1983

# Translated to Python



print("Browns Books","\na+b+c=n","\npa*a+pb*b+pc*c=s")

print("books: a, b, c","\nbook prices: pa, pb, pc")

n=int(input("# books sold? "))

s=eval(input("total sales? "))

pa=eval(input("price of book a? "))

pb=eval(input("price of book b? "))

pc=eval(input("price of book c? "))


print("Prices: ","\n",pa,"\t\t",pb,"\t\t",pc)

print("-----------------")


# prices

i=0


for a in range(1,n+1):

  for b in range(1,n+1):

    for c in range(1,n+1):

      if a+b+c==n and pa*a+pb*b+pc*c==s:

      print(a,"\t\t",b,"\t\t",c)

      i+=1


print("\n",i," solutions")





Circles In Squares




The program cirsqu.py calculates four areas:


The area of a square with a side of length of 2 * r.

The area of the inscribed circle with radius r.

The trapped area, which is the area of the square not taken up by the circle.

A corner of the area of the square not taken up by the circle, which is 1 / 4 of the trapped area.


The original program only printed the trapped area and the corner area. In addition, this Python code uses TI-specific modules, ti_system and ti_draw.


The ti_system module is used to temporarily stop execution with the disp_wait() command. Execution continues with by pressing the [ clear ] key.


The ti_draw module is used to draw the square, the circle, and the axis. Both the square and circle are centered at (0,0).


The code uses a scaling routine based on the radius of the circle, so that squares look like squares and circles look like circles. The TI-84 Plus CE Python graphics screen is 320 x 220 pixels. Setting up the window (xmin, xmax, ymin, and ymax) using the ratio will accomplish this task. Here are sampling of viewing windows to try:


xmin = -8, xmax = 8, ymin = -5.25, ymax = 5.25


xmin = -16, xmax = 16, ymin = -10.5, ymax = 10.5


xmin = -32, xmax = 32, ymin = -21, ymax = 21


About the fill_rect and draw_rect commands. The syntax is (x, y, width, height). The coordinates x and y are supposed to be the upper left hand corner, however the manual is incorrect in practice. The correct corner to use is the lower left hand corner. The OS that was used is 5.8.1.0012 (same number for Python).


Code: cirsqu.py


from math import *

from ti_system import *

from ti_draw import *


print("Math Module Activated")

print("Circle inside a Square")

print("circle of radius r")

print("square of side r")

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


ac=pi*r**2

asq=(2*r)**2

ad=asq-ac


# ti system

disp_clr()


print("Radius: ",r)

print("Areas: \nsquare:",asq)

print("circle: ",ac)

print("trapped: ",ad)

print("1/4 trapped: ",ad/4)

print("\nPress [clear] to contiue.")

# ti module ti_system

disp_wait()


# use ti module ti_draw

clear()

# text factor

f=1

# window set up

if r<=5.25:

  x=8

  y=5.25

else:

  y=5.25

  while y<r:

    y*=2

    f*=2

  x=y*8/5.25


set_window(-x,x,-y,y)


# draw axis

set_color(235,235,235)

set_pen("thin","dotted")

draw_line(-x,0,x,0)

draw_line(0,-y,0,y)



# draw shapes

set_color(25,125,255)

set_pen("thin","solid")

# fill_rect anchors at the LOWER-left hand corner (manual error)

fill_rect(-r,-r,2*r,2*r)

set_color(230,130,0)

fill_circle(0,0,r)

set_color(0,0,0)

draw_text(-x,y-2*f,str(y))

draw_text(x-4*f,-y,str(x))

show_draw()




Source


Ahl, David H. The Texas Instruments Home Computer Idea Book. Creative Computing Press. Morris Plains, New Jersey. 1983. pg. 34-35, 105-106 ISBN 0-916688-51-8



Enjoy!


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.