Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Using a variable set elsewhere within a function that has

  • To: mathgroup at smc.vnet.net
  • Subject: [mg98090] Re: Using a variable set elsewhere within a function that has
  • From: Raffy <raffy at mac.com>
  • Date: Sun, 29 Mar 2009 03:23:09 -0500 (EST)
  • References: <gqkuru$488$1@smc.vnet.net>

On Mar 28, 3:41 am, Daan Gerla <d... at gerla.nl> wrote:
> Hello Mathematica users,
>
> I am trying to use a variable defined elsewhere within Manipulate. I
> have assigned an expression to the variable that involves another
> variable. That second variable should not be assigned to anything
> outside of manipulate, however, within Manipulate, I want that second
> variable to change as I drag a slider and anything that uses the first
> variable to change accordingly.  I am not quite sure I am saying this
> right, but this example should make clear what I mean:
>
> ode = x'[t] == r x[t] - x[t]^2;
> Manipulate[
>  ode,
>  {r, 0.1, 1}
>  ]
>
> I do not get any warnings or errors, but the result is not as you might
> expect: in the output the equation is displayed without a value for r
> and the slider does nothing.
>
> Manipulate[
>  Evaluate[ode],
>  {r, 0.1, 1}
>  ]
>
> does what you expect. The output contains a value for r which changes as
> the slider is dragged. Now I want to use ode for something else.
>
> Manipulate[
>  Plot[x[t] /. NDSolve[{Evaluate[ode], x[0] == 0.1}, x, {t, 0, 10}],=
 {t,
> 0, 10}],
>  {r, 0, 1}
>  ]
>
> Here I get errors:
>
> NDSolve::dsvar: 0.0002042857142857143` cannot be used as a variable. >>
> ReplaceAll::reps: {NDSolve[{(x^\[Prime])[0.000204286]==r
> x[0.000204286]-x[<<1>>]^2,x[0]==0.1},x,<<1>>]} is neither a list of
> replacement rules nor a valid dispatch table, and so cannot be used for
> replacing. >>
> [...]
>
> It looks like it takes a number for t when it should not, and not a
> number for r when it should.
>
> What I want is of course a plot of x vs. t that changes as I change r by
> dragging the slider. I've tried many things, including changing the
> attributes of Manipulate, but no good results. I am not even sure what
> the problem exactly is...
>
> How do I solve this problem? What is the best way to do what I want to do=
?
>
> Regards,
>
> Daan

I'm not a manipulate guru but a quick fix would be:

Manipulate[
 f = NDSolve[{x'[t] == r x[t] - x[t]^2, x[0] == 1/10},
     x, {t, 0, 10}][[1, 1, 2]];
 Plot[f[t], {t, 0, 10}],
 {r, 0, 1}
 ]


  • Prev by Date: Re: Option instead of a function argument: is it possible?
  • Next by Date: Re: Option instead of a function argument: is it possible?
  • Previous by thread: Strange behaviour
  • Next by thread: Default Lighting Colors and Positions