Sunday, June 5, 2016

HP Prime and TI-84 Plus CE: Trapezoidal Rule

HP Prime and TI-84 Plus CE:  Trapezoidal Rule



Caption:  The Trapeziodal Rule applied on ∫ X^2*e^X dx from X = 1 to 4
Actual value:  543.2632185

The program TRAPRULE approximates the integral of f(X)

∫ f(X) dX = h/2 * ( f(a) + 2*Σf(x_I) + f(b) )

Where:
a = the lower limit
b = the upper limit
n = the number of intervals
h = (b – a)/n
x_I = a + h*I where I is from 1 to n-1



HP Prime:  TRAPRULE
EXPORT TRAPRULE()
BEGIN
// EWS 2016-06-05
HAngle:=0; // Radians
LOCAL f;

INPUT({{f,[8]},A,B,N},
"Trapezoidal Rule",
{"f(X)=","Low:","High:",
"Intervals:"});
H:=(B-A)/N;
X:=A; T:=EVAL(f);
X:=B; T:=EVAL(f)+T;
FOR I FROM 1 TO N-1 DO
X:=A+I*H; T:=2*EVAL(f)+T;
END;
T:=T*H/2;
RETURN T;
END;


TI-84 Plus CE Program: TRAPRULE
Radian:Func
Input "LOW:",A
Input "HIGH:",B
Input "INTERVAL:",N
(B-A)/N→H
A→X:Y₁→T
B→X:Y₁+T→T
For(I,1,N-1)
A+I*H→X
2*Y₁+T→T
End
T*H/2→T
Disp "INTEGRAL=",T

Examples:

Example 1:

∫ cos^2 X dX from X = 0 to X = π, n = 14
Approximation:  1.570796327

Example 2:

∫ X^2 + 3*X – 6 dX from X = 1 to X = 3, n = 14
Approximation:  8.673469388

Eddie

Source:

Burden, Richard L. and Faires, J. Douglas.  “Numerical Analysis” 8th Ed.  Thompson Brooks/Cole:  Belmont, CA. 2005

This blog is property of Edward Shore, 2016.