Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #9918 > unrolled thread

Wrapping NDSolve within a function

Started bybbeckage <Brian.Beckage@uvm.edu>
First post2012-05-02 09:45 +0000
Last post2012-05-03 08:37 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Wrapping NDSolve within a function bbeckage <Brian.Beckage@uvm.edu> - 2012-05-02 09:45 +0000
    Re: Wrapping NDSolve within a function "Nasser M. Abbasi" <nma@12000.org> - 2012-05-03 08:37 +0000

#9918 — Wrapping NDSolve within a function

Frombbeckage <Brian.Beckage@uvm.edu>
Date2012-05-02 09:45 +0000
SubjectWrapping NDSolve within a function
Message-ID<jnqvn6$j5n$1@smc.vnet.net>
When I try to return the interpolating function produced by NDSolve from 
within a function, the object returned has an unexpected $7360 appended, 
e.g.,

out = predState[1.5, 0.4, 0.2, 0.1, 0.0002, 0.0099]

(where predState is defined further below) results in

=
{{g$7360->InterpolatingFunction[{{0.,1000.}},<>],p$7360->InterpolatingFunction[{{0.,1000.}},<>],q$7360->InterpolatingFunction[{{0.,1000.}},<>]}}

Note the g$7360 rather than just g.  If NDSolve is not wrapped within 
the function, it returns a plain 'g', i.e., g->InterpolatingFunction.... 
 The appended $7360 makes it difficult to use the interpolating function 
as I can't reference it within other functions as the integer changes 
with each function call, e.g., g$7360[10] /. out, then g$7370[10] /. 
out, rather than being able to access it using the expected g[10]/.out.

Why  is this $7360 appended to g?  How can NDSolve be wrapped in a 
function, but be made to return a plain g?

Thanks for your help.

Best wishes,
Brian



predState[rg_, rp_, rh_, Mg_, Mp_, Mh_] :=
 Module[{parmList3woC, parmListND3woC, A, B, Rb, Rc, g, p, q, solND},
  parmListND3woC = {A -> Mg/rg, B -> Mp/rg, C -> Mh/rg, Rb -> rp/rg,
    Rc -> rh/rg};
  solND =
   NDSolve[{
     g'[t] == g[t]*(1 - g[t] - p[t] - q[t]) - A*g[t]*g[t] /. parmListND3woC,
     p'[t] == Rb*p[t]*(1 - p[t] - q[t]) - B*p[t]*g[t] /. parmListND3woC,
     q'[t] == Rc*q[t]*(1 - q[t]) - C*q[t]*g[t] /. parmListND3woC, g[0] == 0.5,
      p[0] == 0.5, q[0] == 0.5}, {g, p, q}, {t, 0, 1000}];
  Return[solND]
  ]







[toc] | [next] | [standalone]


#9924

From"Nasser M. Abbasi" <nma@12000.org>
Date2012-05-03 08:37 +0000
Message-ID<jntg3e$17$1@smc.vnet.net>
In reply to#9918
On 5/2/2012 4:45 AM, bbeckage wrote:
> When I try to return the interpolating function produced by NDSolve from
> within a function, the object returned has an unexpected $7360 appended,
> e.g.,
>

Symbols defined inside Modules will have $nnnn appended to their names.

Rules of thumbs I use: Do not return symbols defined inside Module
back to the caller. To do what you want, pass these symbols along
with the other call parameters. This way, these symbols will be used
inside the Module.

Also, no need to use Return. By default, the last expression value
is the one returned from a Module. Just make sure there is no
";" at the end of the expression.

----------------------------------------------------------
predState[rg_, rp_, rh_, Mg_, Mp_, Mh_, g_, p_, q_] :=
   Module[{parmList3woC, parmListND3woC, A, B, Rb, Rc, solND},

    parmListND3woC = {A -> Mg/rg, B -> Mp/rg, C -> Mh/rg, Rb -> rp/rg, Rc -> rh/rg};
  
     NDSolve[{Derivative[1][g][t] == g[t]*(1 - g[t] - p[t] - q[t]) - A*g[t]*g[t] /.
        parmListND3woC, Derivative[1][p][t] == Rb*p[t]*(1 - p[t] - q[t]) -
          B*p[t]*g[t] /. parmListND3woC, Derivative[1][q][t] ==
         Rc*q[t]*(1 - q[t]) - C*q[t]*g[t] /. parmListND3woC, g[0] == 0.5,
       p[0] == 0.5, q[0] == 0.5}, {g, p, q}, {t, 0, 1000}]

]

out=predState[1.5,0.4,0.2,0.1,0.0002,0.0099,g,p,q]
-------------------------------------------------------

{{g->InterpolatingFunction[{{0.,1000.}},<>],
p->InterpolatingFunction[{{0.,1000.}},<>],
q->InterpolatingFunction[{{0.,1000.}},<>]}}


--Nasser


[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web