Python (TI-84 Plus CE) and Swiss Micros DM32: The Integral of y = abs(r * x + s)
The Integral of y = abs(r * x + s)
This algorithm calculates the integral of ∫ abs(r * x + s) dx, from x = a, x = b), where r and s are constants. For clarity, I am using abs to stand for absolute value instead of the customary pipe characters (|x|).
Let the function y = abs(r * x + s). Then the function can be defined as a piecewise function (without loss of generality):
y =
{ -(r * x + s), x < xc
{ (r * x + s), x ≤ xc
The point x = xc is the critical point because it is the root (zero) of this function:
abs(r * x + s) = 0
Because abs(0) = 0:
r * x + s = 0
r * x = -s
x = -s/r
and:
-(r * x + s) = 0
r * x + s = 0
x = -s/r
Let the critical point xc = -s/r
Taking the indefinite integral of y(x) yields:
∫ y(x) dx =
{ -r * x^2 ÷ 2 – s * x + C, x < xc
{ r * x^2 ÷ 2 + s * x + C, x ≥ xc
and C is an arbitrary integration constant.
Let f(x) = r * x^2 ÷ 2 + s * x and find the definite integral from x = a to x = b.
Case 1: a ≥ xc and b ≥ xc, where both a and b are greater than the critical point. This is the simplest case.
∫ ( r * x + s dx, x = a to x = b)
= (r * b^2 ÷ 2 + s * b) - (r * a^2 ÷ 2 + s * a)
= f(b) – f(a)
Case 2: a < xc and b < xc, both a and b are less than the critical point.
∫ ( r * x + s dx, x = a to x = b)
= -(r * b^2 ÷ 2 + s * b) - -(r * a^2 ÷ 2 + s * a)
= -(r * b^2 ÷ 2 + s * b) + (r * a^2 ÷ 2 + s * a)
= (-r * b^2 ÷ 2 - s * b) + (r * a^2 ÷ 2 + s * a)
= -f(b) + f(a)
= -(f(b) - f(a))
Combining cases 1 and 2, the area can be calculated as:
area = abs(f(b) – f(a))
with (a – xc) * (b – xc) ≥ 0
Case 3: a < xc and b ≥ xc
∫ ( r * x + s dx, x = a to x = b)
= ∫ ( -(r * x + s) dx, x = a to x = xc) + ∫ ( r * x + s dx, x = xc to b)
= -(r * xc^2 ÷ 2 + s *xc) + (r * a^2 ÷ 2 + s * a) + (r * b^2 ÷ 2 + s * b) – (r * xc^2 ÷ 2 + s * xc)
= -f(xc) + f(a) + f(b) – f(xc)
= f(a) – 2 * f(xc) + f(b)
Since area must be positive: abs(f(a) – 2 * f(xc) + f(b)).
Consequently: (a – xc) * (b – xc) < 0.
In summary:
Let xc = -r/s
If (a – xc) * (b – xc) ≥ 0: area = abs(f(b) – f(a))
Else if (a – xc) * (b – xc) < 0: area = abs(f(a) – 2 * f(xc) + f(b))
where f(x) = r * x^2 ÷ 2 + s * x
Please note: ∫ abs(r * x + s) dx ≠ abs(a * x^2 ÷ b * x)
TI-84 Plus CE Python Edition: abslin1.py
Programmed with TI-84 Plus CE Python, but can be used on any calculator with Python since only the math module is used.
#
Math Calculations
from math import *
# Python
Version
# 2026-01-05 EWS
print("integral of
abs(rx+s)")
r=eval(input("r? "))
s=eval(input("s?
"))
a=eval(input("lower limit? "))
b=eval(input("upper
limit? "))
# critical point
c=-s/r
#
integral
f=lambda x:r*x**2/2+s*x
f0=f(c)
f1=f(a)
f2=f(b)
if
(a-c)*(b-c)>=0:
t=abs(f2-f1)
else:
t=abs(f1-2*f0+f2)
print("area = ",str(t))
Swiss Micros DM32 Program: asblin
Three labels are used: A (172 bytes), Z (20 bytes), Y (17 bytes), total 209 bytes
Text strings can be eliminated.
A01 LBL A
A02 SF 10
A03 “AREA ABS(RX +S)”
A04 INPUT R
A05 INPUT S
A06 x<>y
A07 ÷
A08 +/-
A09 STO C
A10 XEQ Y
A11 STO D
A12 “LOW=A HIGH=B”
A13 INPUT A
A14 XEQ Y
A15 STO E
A16 INPUT B
A17 XEQ Y
A18 STO F
A19 RCL B
A20 RCL- C
A21 RCL A
A22 RCL- C
A23 ×
A24 x≥0?
A25 GTO Z
A26 RCL E
A27 RCL D
A28 2
A29 ×
A30 -
A31 RCL+ F
A32 ABS
A33 STO Z
A34 CF 10
A35 RTN
Z01 LBL Z
Z02 RCL E
Z03 RCL- F
Z04 ABS
Z05 STO Z
Z06 CF 10
Z07 RTN
Y01 LBL Y (Note: f(x) = r*x^2 ÷ 2 + s*x)
Y02 ENTER
Y03 x^2
Y04 RCL× R
Y05 2
Y06 ÷
Y07 x<>y
Y08 RCL× S
Y09 +
Y10 RTN
Examples
Example 1:
y = abs(4 * x + 3)
r = 4, s = 3, xc = -0.75
Lower Limit (a) |
Higher Limit (b) |
Area |
-4 |
5 |
87.25 |
-4 |
-1 |
21 |
0 |
5 |
65 |
Example 2:
y = abs(-3 * x + 6)
r = -3, s = 6, xc = 2
Lower Limit (a) |
Higher Limit (b) |
Area |
-5 |
5 |
87 |
3 |
5 |
12 |
-5 |
1 |
72 |
Hope you find this helpful and have a great day,
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.
