       Re: Using a variable set elsewhere within Manipulate

• To: mathgroup at smc.vnet.net
• Subject: [mg98159] Re: Using a variable set elsewhere within Manipulate
• From: Daan Gerla <daan at gerla.nl>
• Date: Wed, 1 Apr 2009 05:54:33 -0500 (EST)

```Hello Mathematica users,

This message reports back on the answers I got on my previous message
"[mg98019] Using a variable set elsewhere within a function that has
attribute HoldAll", but where "HoldAll" got cut off from the subject line.

The problem was that I wanted to use a variable assigned to a
differential equation within Manipulate to dynamically solve it as a
parameter in the equation is tweaked. I want to the write the equation
outside of Manipulate and use it both within and outside of Manipulate,
without having to copy-paste it.

This does not work:

ode = x'[t] == r x[t] - x[t] ^2;
Manipulate[
sol = NDSolve[
{ode, x == 0.1},
x,
{t, 0, 10}
];
Plot[x[t] /. sol, {t, 0, 10}, PlotRange -> {0, 1}],
{r, 0.1, 1}]

First I thought it did not work because of the HoldAll attribute of
Manipulate, because I can get some similar bits of code working by
wrapping the first argument of Manipulate in Evaluate. For instance

x = y;
Manipulate[x, {y, 0, 1}]

does not work, but then

Manipulate[Evaluate[x], {y, 0, 1}]

does.

An easier way of understanding that the the dynamic ode solving above
does not work is by seeing that r in the assignment of ode is not the
same r as in the last argument of Manipulate. Two approaches were
suggested to me.

David (besides giving a nice example of how to use Dynamic instead of
Manipulate) made ode a function of r:

ode =.; sol =.
ode[r_] := x'[t] == r x[t] - x[t] ^2;
Manipulate[
sol = NDSolve[
{ode[s], x == 0.1},
x,
{t, 0, 10}
];
Plot[x[t] /. sol, {t, 0, 10}, PlotRange -> {0, 1}],
{s, 0.1, 1}]

Jens used replacement:

ode =.; sol =.
ode = x'[t] == r x[t] - x[t] ^2;
Manipulate[
sol = NDSolve[
{ode /. r -> s, x == 0.1},
x,
{t, 0, 10}
];
Plot[x[t] /. sol, {t, 0, 10}],
{s, 0.1, 1}]

Both work. However, the actual ODE's I want to solve involve many
parameters, all of which I want to manipulate. Writing ode1, ode2 etc.
as functions of the parameters doesn't look nice, so that is why I
prefer Jens' solution with replacement.

I am still looking for a nice way to get a list of rules set defined
elsewhere to work within Manipulate! ;-)

Thank you, David, Jens and Raffy.

Regards,

Daan

```

• Prev by Date: selecting a column in a grid
• Next by Date: Re: Graphics and DisplayFunction
• Previous by thread: Re: selecting a column in a grid
• Next by thread: Re: Graphics and DisplayFunction