Message Boards Message Boards

0
|
2312 Views
|
9 Replies
|
9 Total Likes
View groups...
Share
Share this post:

Manipulate sliders don't seem to be working?

Posted 1 year ago

I am trying to plot a function that has the form T=f(r,t,s(t)) in the domain (0,0.05). If I input the numerical values of t and s(t), and then plot T as a function of r, Mathematica gives me the graph of T as a function of r.

Plot[T, {r, 0, 0.05}]

But when I try to use the Manipulate function to see how the plot of T as a function of r varies for different numerical values of t and s(t),

Manipulate[Plot[T, {r, 0, R}],
 {{t, 15(*h*)*60*60}, 0, 24(*h*)*60*60},
 {{s, 0.0225}, 0, 0.05}]

I get an output with sliders for t and s(t), but moving the sliders do not change the output. What am I missing here?

Here is the notebook:

POSTED BY: Safi Ahmed
9 Replies
Posted 1 year ago

Yeah, I was struggling with tracing all the evaluation of expressions. Appreciated learning how functional principles work. This was really helpful. Thanks!

POSTED BY: Safi Ahmed
Posted 1 year ago

Yes, I see that the variables s and t inside Manipulate were local to the Manipulate function. This was helpful! Thanks!

I seem to have run into another problem! I see that now that I have defined T as a function T[r_ , s_ ,t_ ] := f(r,s,t), I am not getting an appropriate output when I input say T[a,b,c], T[1,2,3], etc. Mathematica is returning the same expression f(r,s,t) and not f(a,b,c) or f(1,2,3).

POSTED BY: Safi Ahmed
Posted 1 year ago

Short answer, replace

T[r_, s_, t_] := \[Theta] (T0 - Tm) + Tm

with

T[r_, s_, t_] = \[Theta] (T0 - Tm) + Tm

Explanation

SetDelayed (:=) holds the right hand-side unevaluated when it creates the DownValues. Set (=) does not, allowing the right hand side to be evaluated before it creates the DownValues. Let's walk through it, starting with

T[r_, s_, t_] := \[Theta] (T0 - Tm) + Tm

The DownValues looks like this

{HoldPattern[T[r_, s_, t_]] :> \[Theta] (T0 - Tm) + Tm}

Now let's evaluate a T with some arguments:

T[a, b, c]
(* All arguments get evaluated first, and they have no replacement rules, so we move on to T. *)
(* There's only one DownValues to check, and it does match, so we do a replacement *)
\[Theta] (T0 - Tm) + Tm
(* Now we evaluate this new expression, looking at OwnValues for each symbol... *)

The expression at this point is very complicated, so I won't reproduce it, but it does contain r, s, and t. But r, s, and t are just symbols, and they have no OwnValues, so they do not undergo further replacement. I think you're expecting that r should be replaced with a, s with b, and t with c. The problem is that a, b, and c were only necessary to do the first DownValues replacement. The evaluator was doing something like this

\[Theta] (T0 - Tm) + Tm /. {r -> a, s -> b, t -> c}

There are no occurrences of r, s, or t to replace, so nothing happened, and it moved on the next evaluation step.

When you evaluate T[r_, s_, t_] := ..., you are not setting up a procedure that has local variables defined on a stack. You are simply setting up a replacement rule so that if the pattern on the left is matched, it will be replaced with the (unevaluated) expression on the right. When you later evaluate T[1, 2, 3], every r in the right hand side gets replaced with 1, but this does not mean that there is some local variable r that has been bound to the value 1.

Now, if you had used Set instead,

T[r_, s_, t_] = \[Theta] (T0 - Tm) + Tm

then \[Theta], T0, and Tm would have been evaluated before updating the DownValues. Eventually, that leads to an expression involving r, s, and t. So, future evaluation of T[1,2,3] will result in the evaluator effectively doing <complicated expression> /. {r->1,s->2,t`->3}.

In practice, I find that using Set for creating DownValues can be a bit tricky, especially during the exploratory/experimental phase of development. I tend to do a bunch of little tests which end up polluting the Global context with a bunch of symbols, and every once in awhile I carelessly use one of these symbols in a Set expression both as argument pattern and in the right hand side. Then I get unexpected results trying to use the function I so defined. SetDelayed avoids this problem. Once everything has settled down, I could go back and replace gratuitous uses of SetDelayed with Set, but I tend not to.

POSTED BY: Eric Rimbey
Posted 1 year ago

POSTED BY: Eric Rimbey
Posted 1 year ago

Hmm, looks like you'll need to download the notebook to see the actual behavior of the Manipulate expressions.

POSTED BY: Eric Rimbey
Posted 1 year ago

In a Manipulate like this,

Manipulate[Plot[T, {r, 0, R}],
 {{t, 15(*h*)*60*60}, 0, 24(*h*)*60*60},
 {{s, 0.0225}, 0, 0.05}]

the s and t variables need to be part of the expression in the body of the Manipulate. What you have in the body, Plot[T, {r, 0, R}], is an expression, it's not a predefined procedure that Mathematica will reference. Mathematica needs to "remember" that expression so that it can recalculate it when s or t change, so it can't just automatically compute it (which, if it did, would presumably expose the s and t as you seem to expect). So, you change s and Mathematica looks at Plot[T, {r, 0, R}] and says, "nothing to do here", because it doesn't see any s in that expression.

On a quick scan, I didn't even see where T was defined, but whatever the expression is that uses s and t, that's the expression you want to plot.

Also, you mention that T depends on t and s(t), but you set up the Manipulate with s and t as independent variables. I'm not sure what you expect to happen here. It seems to me that you should only need t. Whatever the function s is can be used in your definition of T by just applying it to t.

POSTED BY: Eric Rimbey
Posted 1 year ago

Also, you mention that T depends on t and s(t), but you set up the Manipulate with s and t as independent variables. I'm not sure what you expect to happen here.

Yes, presently it is set as independant variables. I have only a graph and not an expression for this function, I was just manually inputting the values of s and t. But maybe I should approximate an expression for s. I will see how mathmematica can give me an expression if I give it a set of points (s,t). Thanks for this point.

POSTED BY: Safi Ahmed
Posted 1 year ago

Mathematica needs to "remember" that expression so that it can recalculate it when s or t change, so it can't just automatically compute it [...] So, you change s and Mathematica looks at Plot[T, {r, 0, R}] and says, "nothing to do here", because it doesn't see any s in that expression.

The expression for T is defined few lines above the plot. Even though I have set values for s and t below that line, I understand that Manipulate should still treat T as an expression T=f(r,t,s(t)). And therefore it should change the numerical value of T if I input a different set of t and s(t). So s and t should exist in the body of Manipulate.

POSTED BY: Safi Ahmed
Posted 1 year ago

No, that’s not how it works. Mathematica will fetch the DownValues/OwnValues of T when needed. It won’t speculatively look ahead to see if those DownValues/OwnValues might in turn depend on something before deciding to evaluate T. Also, in the Manipulate expression, s and t are local, so if T was defined outside of the Manipulate, those s and t variables won’t match anyway.

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract