Re: "Incremental Integration"?
- To: mathgroup at smc.vnet.net
- Subject: [mg31788] Re: [mg31774] "Incremental Integration"?
- From: BobHanlon at aol.com
- Date: Sun, 2 Dec 2001 04:25:04 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
In a message dated 2001/12/1 4:30:41 AM, siegman at stanford.edu writes:
>Given a rather messy function f[x] , I want to make a Table of its integral
>
>from x0 up to a set of equally spaced points xn = x0 + n*dx with maximum
>value
>xFinal -- in other words
>
> g[x_] := NIntegrate[f[xi], {xi, x0, x}]
>
> Table[ {x, g[x]}, {x, x0, xFinal, dx}]
>
>Is Mathematica smart enough to calculate each successive value using the
>
>previous value plus integration just over the increment in x to the next
>value?
>
>If not, what would be a clean and readable and also reasonably efficient
>way to
>code this?
>
>[Note: For me anyway, there's some emphasis on the "clean and readable"
>part --
>avoiding esoteric Mathematica syntax, not taking terseness as the ultimate
>goal.]
>
f[x_] := x^2;
x0=0; xFinal=10; dx=2;
rng = Range[x0, xFinal, dx];
Transpose[{rng,
FoldList[#1+#2&,0,
NIntegrate[f[x],{x,#[[1]],#[[2]]}]& /@
Partition[rng,2,1]]}]
If readability is taken as the primary metric for code, then you will likely
end up
using predominantly procedural programming and not make effective use of
functional programming. If a complex expressions cannot be read easily,
build the
expression step-by-step and convert all of the steps (input and output) into
a merged
text cell to keep with the code. This should be done in as general a form as
is
practical. For example, evaluate, merge, and convert to text the following
Clear[x0,f]
rng = {x0,x1,x2,x3};
Partition[%,2,1]
Integrate[f[x], {x,#[[1]],#[[2]]}]& /@ %
FoldList[#1+#2&,0,%]
(* NOTE: For NIntegrate the numbers are folded and each integral is evaluated
once. *)
{rng,%}
Transpose[%]
Bob Hanlon
Chantilly, VA USA