Here's a program I made for estimating a double integral over a general region. It's pretty accurate, but it's VERY, VERY slow. (Getting an accurate enough result takes about 30 seconds)
Here's the code; the lines are indented just for readability.
ClrHome Input "Surface: ",Str0 Str0→Y₈ Input "y-Bound 1: ",Str0 Str0→Y₉ Input "y-Bound 2: ",Str0 Str0→Y₀ Input "X1: ",A Input "X2: ",B Input "Num of intv: ",N (B-A)/N→D 0→V For(K,0,N-1 A+KD+.5D→X V+fnInt(abs(Y₀-Y₉),X,A+KD,A+(K+1)D)fnInt(Y₈,Y,Y₀(X),Y₉(X))/(Y₀(X)-Y₉(X→V End DelVar DDelVar XDelVar YDelVar Str0 Disp " Disp "Volume in R: ",V
Str0: Arbitrary (input) Y₈: Equation of surface f(x,y) Y₉: Equation of one bound of region R Y₀: Equation of other bound of region R A: x-value of left bound of region R (input) B: x-value of right bound of region R (input) N: Number of Δx intervals (input) D: Size of Δx interval X: x-value of midpoint of Δx interval V: Volume under the surface f(x,y) bound by the region R (output)
Basically, how I'm tackling it is I'm taking a slice of the region R on the x-y plane (Δx is the width) and finding its area (
fnInt(abs(Y₀-Y₉),X,A+KD,A+(K+1)D)). Then I find the x-value that's the middle of that slice, and I find the average z-value along that x (
fnInt(Y₈,Y,Y₀(X),Y₉(X))/(Y₀(X)-Y₉(X))), which I assume to be a reasonable estimate for the height of that entire slice of volume. Then I run a
For( loop a bunch of times and add up all of the slices together to find the total volume.
But the problem is that I need lots of intervals (about 50-100) to get an accurate enough answer, which takes a ridiculous amount of time.
I already tried to optimize it a little by getting around all the
expr('s I used to have, but it's still very very slow.
Any suggestions on optimizing?