Tushar and all,
This has grabbed me because we get the "area under a curve" question
fairly frequently. Yes, we can toss this into VBA, but I like the idea
of keeping it in the wks. This is an open invitation for folks to come
towards some sort of open "recipe" that most anyone could follow (ok,
that's a stretch, perhaps)
Just noticed that for your posted implementation (simpson's) to work as
intended, it is important that nbrVals be an even integer. So adjusting
your statement to something like
Nbrvals =Sheet1!$F$1+MOD(Sheet1!$F$1,2)
would be safer.
With that in mind, and adapting your ideas to my preference of keeping
the evaluated function "in the open" in the worksheet, we end up with
this:
==========
"Area under a curve"
To use Excel for evaluating the integral of, say, 2+3*(Ln(x))^0.6 using
Simpson's Rule (also see notes below):
1) Enter some labels:
In cell.....enter
A1 "X_1"
A2 "X_n"
A3 "NbrPanels"
2) Set some values:
In cell.....enter
B1 1
B2 2.5
B3 1000
3) Define some names:
Select A1:B3, then choose Insert->Name->Create. Make sure that only the
"Left Column" box is selected. If it isn't, you might have entered text
instead of numbers in the right column, or mis-selected the range. Press
OK.
4) Choose Insert->Names->Define
Enter each of the following names and their definitions, pressing Add
with each entry (you can copy and paste these):
EPanels =NbrPanels+MOD(NbrPanels,2)
delta =(X_n-X_1)/EPanels
Steps =ROW(INDIRECT("1:"&EPanels+1))-1
EvalPts =X_1+delta*Steps
SimpWts =IF(MOD(Steps,EPanels)=0,1,IF(MOD(Steps,2)=1,4,2))*delta/3
(optional) If interested in a trapezoidal approximation, define
TrapWts =IF(MOD(Steps,EPanels)=0,0.5*delta,delta)
5) Close the Define Names box, and in, say, cell D1, array-enter
=SUM(SimpWts*(2+3*LN(EvalPts)^0.6))
That is, type in the function, and hold ctl-shift when pressing Enter.
In general, ctrl-shift-enter =SUM(SimpWts*f(EvalPts))
where f() is a legitimate Excel expression that yields a scalar numeric
value.
To use the trapezoidal method, substitute in the above expression
TrapWts for SimpWts.
Notes:
(1) With this implementation, an odd number for NbrPanels doesn't cut it
(for Simpson's rule), so there will be no improvement moving from an odd
number to the next integer (odd ones are automatically changed to the
next even, internally).
(2) If you have "jumps" in your function, break it up at the points
where those occur, and add the pieces.
(3) If you want to know the are under the curve that Excel draws for
*data points* where the line isn't smoothed (i.e., there are straight
lines joining successive points... right-click [or ctrl-click] the
series, choose "Format Data Series",select the Patterns tab, deselect
"Smoothed line"):
(a) Suppose the "x values" are in a column range Xpts, the "y values"
in column range Ypts; both should have the same number of values.
(b) Insert->Name->Define "Pnls" to be =COUNT(Xrng)-1.
(c) Enter the function
=SUMPRODUCT((OFFSET(Xrng,1,0,Pnls)-OFFSET(Xrng,0,0,Pnls))*(OFFSET(Yrng,1,
0,Pnls)+OFFSET(Yrng,0,0,Pnls)))*0.5
(d) if, say, Xrng is a Row range, adjust the above OFFSETs to
OFFSET(Xrng,1,0,0,Pnls) and OFFSET(Xrng,0,0,0,Pnls)
(4) If you want to know the area under a smoothed graph of data points,
refer to the SplineIntegrate function at
http://groups.google.com/[email protected]
Excel apparently uses Bezier smoothing; to know that are under *that*
smooth, well, I dunno (yet).
(5) For other ideas and "visuals", see Tushar Mehta's post at
http://www.mrexcel.com/board2/viewtopic.php?t=59869
and Bernard Liengme's page:
http://www.stfx.ca/people/bliengme/ExcelTips/AreaUnderCurve.htm
For some tech info, refer to "Numerical Recipes in C" or such.
(6) Don't forget that there are sophisticated packages that you can call
from Excel to handle integration, such as Matlab, Maple and Mathematica.
=======
Do you (or anyone!!!) see any room for improvement here? If so, could
you please correct/edit as you see fit and repost (I trust you). As far
as I can tell, this framework allows one to toss in wts for most any
Newton-Cotes formula, so it's pretty flexible, though I don't see why
one would bother with more than the two listed here, with the possible
exception of Boole's (aka Bode) method.
TIA
Dave B
Tushar Mehta said:
If you want to see how to use Simpson's Rule using named formulas see
my post in
http://www.mrexcel.com/board2/viewtopic.php?t=59869.
Implementing the trapezoidal rule with a variant of David's suggestion
that uses only named formulas:
Suppose you have the function as a *literal* (not as a formula) in B1.
Suppose the min. and max. x values are in D1 and D2 respectively.
Finally, suppose the number of slices you want is in F1.
Then, with the named formulas below, =TrapezoidalRlst in a cell will
give you the result.
myFx =Sheet1!$B$1
XMax =Sheet1!$D$2
XMin =Sheet1!$D$1
Nbrvals =Sheet1!$F$1
delta =(XMax-XMin)/Nbrvals
incrSteps =(ROW(OFFSET(Sheet1!$A$1,0,0,Nbrvals+1,1))-1)
X =XMin+incrSteps*delta
YVals =EVALUATE(myFx&"+x*0")
MultFacts =IF(MOD(incrSteps,Nbrvals)=0,0.5,1)
TrapezoidalRslt =SUMPRODUCT(MultFacts,YVals)*delta