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.

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...