Re: How to avoid repeated calculation in NDSolve ?
- To: mathgroup at smc.vnet.net
- Subject: [mg117008] Re: How to avoid repeated calculation in NDSolve ?
- From: Oliver Ruebenkoenig <ruebenko at wolfram.com>
- Date: Mon, 7 Mar 2011 05:49:12 -0500 (EST)
- References: <201103061042.FAA15467@smc.vnet.net>
On Sun, 6 Mar 2011, t. shim wrote:
> Hi,
>
> As the samplke list below, the rhs of equations in NDSolve may have
> many repeated appearance of the same expression such as (\[Mu][t]^2 +
> \[Nu][t]^2) in the denominator.
> In Fortran, we first write r = (\[Mu][t]^2 + \[Nu][t]^2) and in what
> follows use r insted of (\[Mu][t]^2 + \[Nu][t]^2).
> Tell me if such can be applied also in mathematica.
>
> Regards, t. shim
>
> NDSolve[
> {p\[Mu]'[t] =
> 1/2 (2 \[Mu][
> t] (1/2 (
> p\[Mu][t]^2 + p\[Nu][t]^2-4)/(\[Mu][t]^2 + \[Nu][t]^2) -
> (1 - \[Gamma])/2 (\[Mu][t] p\[Mu][t] - \[Nu][t] p\[Nu][t])^2/ (\[Mu]
> [t]^2 + \[Nu][t]^2)^2) +
> (1 - \[Gamma]) 1/(\[Mu][t]^2 + \[Nu][t]^2)^2 \[Nu][t] (\[Mu][t] p\[Mu]
> [t] -
> \[Nu][t] p\[Nu][t]) (\[Nu][t] p\[Mu][t] + \[Mu][t] p\[Nu][t])),
> p\[Mu]'[t] = (mu, nu interchanged)
> ...
>
>
Hi,
if you look at, say,
test = {p\[Mu]'[t] =
1/2 (2 \[Mu][
t] (1/2 (p\[Mu][t]^2 + p\[Nu][t]^2 -
4)/(\[Mu][t]^2 + \[Nu][t]^2) - (1 - \[Gamma])/
2 (\[Mu][t] p\[Mu][t] - \[Nu][t] p\[Nu][t])^2/(\[Mu][
t]^2 + \[Nu][t]^2)^2) + (1 - \[Gamma]) 1/(\[Mu][
t]^2 + \[Nu][t]^2)^2 \[Nu][
t] (\[Mu][t] p\[Mu][t] - \[Nu][t] p\[Nu][t]) (\[Nu][t] p\[Mu][
t] + \[Mu][t] p\[Nu][t]))}
and then at
Experimental`OptimizeExpression[test]
you see the optimized expr. that NDSolve uses. So, NDSolve does this
automatically.
Oliver
- References:
- How to avoid repeated calculation in NDSolve ?
- From: "t. shim" <tokuzoshimada3@gmail.com>
- How to avoid repeated calculation in NDSolve ?