Showing posts with label Simpson’s Rule. Show all posts
Showing posts with label Simpson’s Rule. Show all posts

Saturday, November 15, 2025

Basic (TI-81) vs. Python (TI-84 Plus CE Python): First and Second Derivative and Integral by Simpson’s Rule

Basic vs. Python: First and Second Derivative and Integral by Simpson’s Rule



Calculators Used


Basic: TI-81

Python: TI-84 Plus CE Python



Task


Estimate the numerical first derivative by the Nearby Secant Line Method:


f'(x) = lim (f(x + h) - f(x - h)) / (2 * h) as h → 0



Estimate the numerical second derivative by:


f''(x) = lim (f(x + h) - 2 * f(x) + f(x - h)) / h^2 as h → 0



Estimate the definite integral by Simpson’s rule:


∫( Y1 dx, x = a to x = b) ≈

(b - a)/(3 * n) + (f(a) + 4 * f1 + 2 * f2 + 4 * f3 + 2 * f4 + ... + 2 * f_n-2 + 4 * f_n-1 + f(b))


n must be an even integer.

fi = f(a + i * (b – a) / n)


BASIC: TI-81


First let’s look at the TI-Basic version with the classic TI-81:


First Order Derivative  (Nearby Secant Line Method)


f'(x) ≈ (f(x + h) - f(x - h)) / (2 * h)


DER1  (61 bytes)

Function

Rad

Disp "D/DX Y1"

1E-4 -> H

Disp "X?"

Input A

A+H -> X

Y1 -> D

A-H -> X

(D-Y1)/(2H)->D

Disp D



Second Order Derivative


f''(x) ≈ (f(x + h) - 2 * f(x) + f(x - h)) / h^2


DER2  (75 bytes)

Function

Rad

Disp "D²/DX² Y1"

1E-4 -> H

Disp "X?"

Input A

A+H -> X

Y1 -> D

A -> X

D-2*Y1 -> D

A-H -> X

(D+Y1)/(H^2) -> D

Disp D


INTGY1  (134 bytes)

Function 

Rad

Disp "SIMPSONS RULE"

Disp "A,B,N?"

Input A

Input B

Input N

A -> X

Y1 -> S

B -> X

S+Y1 -> S

1 -> I

Lbl 1

A+I*(B-A)/N -> X

S+2*Y1 -> S

If Fpart (I/2)≠0

S+2*Y1 -> S

IS>(I,N-1)

Goto 1

S*(B-A)/(3*N) -> S

Disp S


Examples


f(x) = 3*x^3 + 4*x – 1


Y1 = 3 * X³ + 4 * X – 1

f’(2.4): DER1 returns: 55.84

f’’(2.4): DER2 returns: 43.2


∫( 3 * X³ + 4 * X – 1 dX, 0.5, 1.5):

INTGY1 returns with N = 40 returns 6.75


Notes for BASIC


Before running the programs, store the function to the variable Y1. Calling Y1 evaluates the function of the value stored in variable X. Evaluate Y1 at X: [2nd], (Y-VARS), 1


The TI-81 MUST be in Function mode to evaluate Y1, unless an error occurs. The programs set the angle mode to Radians. The value of h is set to a "default" value of 10^-4. We want h to be small, but not too small. A value of h too small can run the risk of floating point errors.


In the integral program:


* I/2 - Ipart (I/2) is Fpart(I/2), but the long way. Python does not have a built-in fractional part function.


* IS>(I,N-1) translates to:


I=I+1

If I>N-1

Goto 1


IS> is the increment (by 1) and skip, a command in TI-Calculator Basic.


In general BASIC languages, evaluation Y1 may take a different approach, depending on the calculator, pocket computer, or computer:


General Basic:


### ...

### X = g(A)

### GOSUB ***

### ...


*** Y = <evaluate f(X)>

*** RETURN


Another approach on a calculator: Use another program


Prog <main>

...

g(A) -> X

Prog "FX" (see below, answer is stored in Y)

...


Prog "FX" (or any name of your choosing)

<evaulate f(X)> -> Y

<end program, an implied Return is executed here>



PYTHON: TI-84 Plus CE Python Edition


The code presented here uses the math module and built-in functions, and should work with any calculator with Python.


DERINTG.py


from math import *

# derivatives and integrals

# 2025-06-01


print("MATH MODULE IS IMPORTED")


# first derivative function

def der1(fx,x,h=1E-3):

  # fx is a string

  # h is optional (def 1E-3)

  # set up lambda

  f=eval("lambda x:"+fx)

  d=(f(x+h)-f(x-h))/(2*h)

  return d


# second derivative function

def der2(fx,x,h=1E-3):

  # fx is a string

  # h is optional (def 1E-3)

  # set up lambda

  f=eval("lambda x:"+fx)

  d=(f(x+h)-2*f(x)+f(x-h))/(h**2)

  return d


# integrals by simpsons rule

def intg(fx,a,b,n=64):

  # fx is a string

  # n is even, optional (def 64)

  # set up lambda

  f=eval("lambda x:"+fx)

  s=f(a)+f(b)

  for i in range(1,n):

    w=f(a+i*(b-a)/n)

    s+=(2*w) if (i/2-int(i/2)==0) else (4*w)

  s*=(b-a)/(3*n)

  return s


I decided to make a script of three functions.


Python uses binary arithmetic, so be prepared to possibly see answers such as 2.999999999999 and 5.00000000000001. The code leaves the answer in floating point form. Feel free to use the round function and/or format specifiers as you see fit.


f(x) = 3*x^3 + 4*x – 1


In Python:

f(x) = 3*x**3+4*x-1


Examples


Store the string to s:

s=”3*x**3+4*x-1”

der1(s,2.4) returns 55.84000299999303

der2(s,2.4) returns 43.19999999324864

intg(s,0.5,1.5) returns 6.75



I hope you find this useful. Enjoy,


Eddie


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

Monday, September 4, 2017

Retro Review: Texas Instruments TI-80

TI-80


TI-80 (left), TI-84 Plus CE (right)
Look how thin the TI-80 is
Retro Review: Texas Instruments TI-80

First, thank you Nano for the TI-80 (along with giving me a pair of slide rules and an astronomy poster)!  Much appreciated!
  
Essentials

Company:  Texas Instruments
Years:  1995
Type:  Graphing, Programming
Memory:  7,034 bytes
Operating System: Algebraic
Memory Registers: 27 (A-Z, θ)
Screen:  Monochrome

Batteries:  2 CR2032 batteries

Graphing Modes:  Function (4), Parametric (3).  Table included. 

Regressions:  6: Linear (ax + b), Quadratic, Linear (a + bx), Logarithmic, Exponential, Power

Lists: Up to 99 entries per lists, 6 lists available (L1 through L6)

Matrices: none

Complex Numbers:  none

Keyboard

The keyboard is what one would expect on a Texas Instruments graphing calculator: nice and responsive. 

Screen

The screen is small.  Not kidding.  The screen is only 48 x 64 pixels big, accompanying 8 lines of 16 characters.  That means that the font is small.  What is wild is that the pi symbol (Ï€) does not conform to the rest of the font, and is twice as long as the rest of the characters.

The screen is still bigger than the mini-graphing calculators such as the Casio fx-6300g or the Hewlett Packard HP-9g.

Is the TI-80 a simplified TI-81?

For the most part, no.  Sure, the TI-80 does not have matrices and hyperbolic functions (sinh, cosh, etc) like the TI-81.  However, the TI-80 has fractions (see the next section), integer division and remainder function, random integer, a complementary table mode, and lists.  The number of stat plots increased to 3, which they don’t have to depend on the statistics mode.

As far as programming memory, the TI-80 beats the TI-81: 7,034 bytes to 2,400 bytes.  Also, you can go beyond 37 programs for the TI-80, as the names are not restricted to one character.

Fractions

The TI-80 has a dedicated fraction menu, which allow users to convert between improper and proper form, as well as conversion between fraction and decimal approximation.  The Manual Simplification mode allows fractions to not be automatically simplified on calculation. 

To enter fractions, the format is:  A _ B / C
Note that the slash is bold.  Merely pressing the division key will not register the fraction.

To separate the whole part from the fraction, press [ 2nd ] [ + ] (UNIT_).

To separate the numerator from the denominator, press [ 2nd ] [ ÷ ] (b/c).

Example:  Enter 2 3/4
Keystrokes:  2 [ 2nd ] [ + ] 3 [ 2nd ] [ ÷ ] 4

According to Datamath (http://www.datamath.org/Graphing/TI-80.htm ), the TI-80 would get replaced with the TI-73 in 1998.  This may mean that the TI-80 became the base for the TI-73 series (TI-73, TI-73 explorer).

Lists

The TI-80 allows for 6 lists, each with a 99 element capacity.  Arithmetic can be operated on two same-sized lists, on an element-by-element functions.  Lists functions include sorting, dimension, minimum, maximum, sum of the elements, product of the elements, and sequence generation.

Programming

Programming is fairly basic for the TI-81.  Commands:
If-Then-Else-End Structure (IF, THEN, ELSE, END)
Quick if structure
For-End structure (no IS>, DS< this time) (FOR, END)
Labels:  one character and local labels (LBL, GOTO)
Subroutines (PRGM_, RETURN)
Drawing commands include points, shading (three types, general, Y<, Y>)

Since the only built-in calculus function of the TI-80 is numerical derivation (NDERIV), two programs for Newton’s Method and Simpson’s Rule are presented below.

TI-80 Program:  SOLVEY1  (Newton’s Method)

80 bytes
The equation is stored in Y1.  The program solves for X in Y1(X) = 0

INPUT “GUESS:”, X
LBL 0
X-Y1/NDERIV(Y1,X,X)→N
IF ABS (X-N)>1E-10
THEN
N→X
GOTO 0
END
N→X
DISP “X = “, X

Example: X^2-3X+1, guess X = 3
Result:  X = 2.618033989

TI-80 Program: SIMPY1 (Integral, Simpson’s Rule)

140 bytes
The equation is stored in Y1.  The program calculates ∫(Y1,X,A,B)

RADIAN
INPUT “A:”,A
INPUT “B:”,B
INPUT “N (EVEN):”,N
(B-A)/N→H
0→T
FOR(I,1,N-1)
A+IH→X
T+2*Y1→T
IF FPART(I/2)≠0
2*Y1+T→T
END
(T+Y1(A)+Y1(B))H/3→T
DISP “INTEGRAL:”,T

Example: X^2-3X+1, with A = 0 to B = 5 and N = 10
Result:  X = 9.166666667

Final Verdict

The TI-80 is a nice introductory calculator, and thanks to programming a lot can be done with it.  I wish the screen was bigger and degree/degrees-minutes-seconds conversions were available, but other than that, it was a great calculator which provides a lot of features (maybe not as intimidating as more advanced calculators). 

It is a nice calculator to add to the collection, and I thank you Nano immensely. 

Eddie


This blog is property of Edward Shore, 2017.

DM42 and HP 42S: Quadratic Equation, Characteristic Polynomial, and Eigenvalues

DM42 and HP 42S: Quadratic Equation, Characteristic Polynomial, and Eigenvalues The programs are listed for the Swiss Micros DM42 an...