Showing posts with label cubic equation. Show all posts
Showing posts with label cubic equation. Show all posts

Sunday, February 15, 2026

Casio fx-3650P vs. Casio fx-3650P II: Programs


Here are four programs that are done with both the fx-3650P and fx-3650P II to illustrate some programming similarities and differences.


When we start a new program with the fx-3650P II, we are prompted to the choose the appropriate mode at the beginning:


COMP (main calculation with real numbers)

CMPLX (complex numbers, primarily complex number arithmetic)

SD (single-derivation mode, single-variable statistics)

REG (regression mode, two-variable statistics)

BASE (base conversions and Boolean logic)


Left:  Casio fx-3650P (2002), Right:  fx-3650P II (2013)


Program 1: Exponential Distribution


This program calculates:


PDF: e^(-X ÷ A)

CDF (lower tail): 1 - e^(-X ÷ A)


where:

A = 1/λ parameter (i.e. average waiting time, time for an event to occur/fail/succeed)

X = time parameter


fx-3650P, 25 bytes:


? → A : ? → X :

e(-X ÷ A) ÷ A ◢

1 – e(-X ÷ A)


fx-3650P II, 25 bytes:


? → A : ? → X :

e^(-X ÷ A) ÷ A ◢

1 – e^(-X ÷ A)


Example 1:

Inputs: A = 100, X = 50

Outputs: 6.065306597E-3 (*10^-3), 0.39346934


Example 2:

Inputs: A = 50, X = 3

Outputs: 0.01883529, 0.058235466


Program 2: Using Newton’s Method to get the real root of the cubic equation that is closest to zero:


x^3 + A * x^2 + B * x + C = 0


Using Newton’s Method:


x1 = x0 – f(x0) / f’(x0) [ let y = f(x0) / f’(x0) ]


x1 = x0 - (x^3 + A * x^2 + B * x + C) / (3 * x^2 + 2 * A * x + B)



fx-3650P, 72 bytes:


? → A : ? → B : ? → C : 0 → X :

Lbl 0 :

(X³ + AX² + BX + C) ÷ (3X² + 2AX + B) → Y :

√(Y²) → D :

X – Y → X :

D ≥ 1E-8 ⇒ Goto 0 : X


√(Y²) returns Abs(Y) in Comp mode.

D is set up to be the control variable, where

D = abs((x^3 + A * x^2 + B * x + C) / (3 * x^2 + 2 * A * x + B))



fx-3650PII, 67 bytes:


? → A : ? → B : ? → C : 0 → X : 1 → Y :

While Abs(Y) ≥ 1E-8 :

(X³ + AX² + BX + C) ÷ (3X² + 2AX + B) → Y :

X – Y → X :

WhileEnd: X


We could have used the derivative function (d/dx) but typing out the derivative took less room.


Example 1: X³ + 2X² – 5X + 4 = 0

Inputs: A = 2, B = -5, C = 4

Output: -3.663076827


Example 2: X³ – 3X² + 9X + 6 = 0

Inputs: A = -3, B = 9, C = 6

Output: -0.5481911635



Program 3: Sum of the polynomial: Σ((AX + B)^C, X = 1 to Y)


fx-3650P, 57 bytes:


0 → M : ? → A : ? → B : ? → C : ? → Y : 1 → X :

Lbl 0 :

(AX + B)^C M+ : X + 1 → X : X > Y ⇒ Goto 1 : Goto 0 :

Lbl 1 : M



fx-3650PII, 41 bytes:


0 → M : ? → A : ? → B : ? → C : ? → Y : 1 → X :

For 1 → X To Y : (AX + B)^(C) M+ : Next : M



Example 1: Σ((5X – 3)^2, X = 1 to 8)

Inputs: A = 5, B = -3, C = 2, Y = 8

Output: 4092



Example 2: Σ((2X + 1)^3, X = 1 to 10)

Inputs: A = 2, B = 1, C = 3, Y = 10

Output: 29160


Program 4: Complex Roots of Unity


x^n = 1


where:

x = cos(2 * m * π / n) + i * sin(2 * m * π / n) = r * e^(2 * m * i / n)

m = 0 to n – 1, i = j = √-1


fx-3650P, 47 bytes:


Switch to complex mode. The CMPLX indicator will be displayed but not shown as a step.


Rad : ? → A : 0 → B :

Lbl 0 :

cos(2 B π ÷ A) + i × sin(2 B π ÷ A) ◢

1 + B → B : A > B ⇒ Goto 0 : 1


We must have an else condition when the jump command is used, that is why the second one is there. Also, that one “indicates” the end.


fx-3650PII, 34 bytes:


Rad : ? → A :

For 0 → B To A :

cos(2 B π ÷ A) + i × sin(2 B π ÷ A) ◢

Next : 1


The second one “indicates” the end and can be omitted.


In complex mode, the real and imaginary parts are displayed on part at a time. A settle R←→I indicator displays at the right hand side of the screen. Switch between seeing the parts by pressing [ SHIFT ] [ EXE ].


Example 1: x^3 = 1

Input: A = 3

Outputs:

1 + 0 i

-0.5 + 0.866025403 i

-0.5 - 0.866025403 i


Example 2: x^5 = 1

Input: A = 5

Outputs:

1 + 0 i

0.309016994 + 0.951056516 i

-0.809016994 + 0.587785252 i

-0.809016994 - 0.587785252 i

0.309016994 - 0.951056516 i


Eddie


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

Thursday, July 31, 2014

Roots of the Cubic Equation - Pythonista 2.7

Roots of the Cubic Equation - Pythonista 2.7

Script:



# Cubic Equations: 2014-07-30 EWS
# need the root
import math
print('ax^3+bx^2+cx+d=0','a!=0')
a=float(input('a='))
b=float(input('b='))
c=float(input('c='))
d=float(input('d='))
# is a root 1?
test=a+b+c+d
if test==0:
r=1
if test!=0:
# first root by Newton's Method
xn=1
x1=xn-(a*xn**3+b*xn**2+c*xn+d)/(3*a*xn**2+2*b*xn+c)
while math.fabs(xn-x1)>1e-13:
xn=x1
x1=xn-(a*xn**3+b*xn**2+c*xn+d)/(3*a*xn**2+2*b*xn+c)
r=x1
print('r=',r)
# second and third roots
ap=a
bp=a*r+b
cp=a*r**2+b*r+c
disc=bp**2-4*ap*cp
if disc<0:
real=-bp/(2.*ap)
imag=math.sqrt(math.fabs(disc))/(2.*ap)
print(r)
print(real,'+/-',imag,'i')
else:
s=(-bp+math.sqrt(disc))/(2.*a)
t=(-bp-math.sqrt(disc))/(2.*a)
print(r)
print(s)
print(t)



Examples:

x^3 - 4*x^2 - 17*x + 60 = 0
a = 1, b = -4, c = -17, d = 60
Roots: 3, 4, 5

2*x^3 + x^2 - 2*x + 1 = 0
a = 2, b = 1, c = -2, d = 1
Roots (approximately): -1.43756, 0.46878 ± 0.35784i

This blog is property of Edward Shore. 2014

First Look: HP 16C Collector's Edition

 First Look: HP 16C Collector's Edition I just got the HP 16C Collector's Edition.   This is the famous HP 16C that specializes in c...