Re: Problems with Module
- To: mathgroup at smc.vnet.net
- Subject: [mg112078] Re: Problems with Module
- From: John Fultz <jfultz at wolfram.com>
- Date: Sun, 29 Aug 2010 02:50:20 -0400 (EDT)
Actually, in my previous explanation is off slightly because, in fact, using Module does tend to "work" because it forces the Module variables to leak out of the Module scope. For example, In[1]:== Module[{x==3},Dynamic[x]] Out[1]== 3 In[2]:== InputForm[%] Out[2]//InputForm== Dynamic[x$627] It's still really bad form, will not properly persist between Mathematica sessions, and I would never use such a technique in production code. But I was wrong to suggest that it wouldn't appear to work after Shift+Enter evaluation for this reason. You can send me the notebook if you like, but I'm extremely busy right now, and it may take me a while to respond. Sincerely, John Fultz jfultz at wolfram.com User Interface Group Wolfram Research, Inc. On Sat, 28 Aug 2010 07:03:32 -0400 (EDT), Docente Sergio Miguel Terrazas Porras wrote: > Hi John: > > Thank you for responding. > > The code does work for me with the empty list of local variables. > Mathematica 7.0.1== > , Windows 7. > > Now, the code with the list of local variables actually shows the > trajectory and both vectors, unitario[t] and du[t] for t == 0. But as soon > as I move the slider, things go wrong, as it tries to evaluate > Derivative[1] ["a number"] of something. > > As for your comments on Manipulate, I as a rule define all my functions > inside Manipulate, but as I was very frustated, I was just trying > everything (I actually saved the notebook with the name Frustating.nb). > > It was actually by accident that I forgot to fill in the list of local > variables. When it worked I was relieved. I then noticed my ommision and > proceeded to fill the list. And then it didn't work! > > Could I send you the actual notebooks? > I have a feeling that there is a problem with the cpoy-paste deal. > > Thanks again ! > > Sergio Terrazas > > John Fultz wrote: > Actually, this does not work for me if I empty the Module list of > variables. > There might be something wrong with the example you intended to post as > you > copied it here. But I can make a generic point without seeing specific, > functioning code. > > Manipulate uses Dynamic internally. Dynamic works by deferring the > evaluation > of its contents until they're displayed onscreen (i.e., well after the > time you > press Shift+Enter and most other evaluation would happen). By the time > the > Manipulate is displayed onscreen, the local variables for the Module have > already gone out of scope. > > A Module *inside* of Manipulate would work fine, since it will be > evaluated as > part of the display. > > Also, by having function definitions outside of the Manipulate, you > guarantee > that, even if things did work, they won't work when you quit and restart > Mathematica and reload the file, until you reevaluate all of your defining > functions. A good, self-contained Manipulate will define its own > functions, > often doing so either explicitly with the Initialization option or > implicitly > with the SaveDefinitions option (both of which are clearly > documented...I'll > leave it to you to read up on them). > > Sincerely, > > John Fultz > jfultz at wolfram.com > User Interface Group > Wolfram Research, Inc. > > Sergio Terrazas wrote: > The following is a slight modification of a code that works fine. But this > one does not work. > However, if I empty the list of local variables in Module, Then it works! > Could some of you enlighten me? > > f[t_]:==4 Sin[t];g[t_]:==2 Cos[t];a==0;b==2\[Pi]; > DerVectConst[f,g,a,b] > > DerVectConst[f_,g_,a_,b_]:==Module[{r,vel,acel,unitario,derivadadeu,du}, > $Assumptions==t\[Element]Reals; > > r[t_]=={f[t],g[t]}; > vel[t_]==(r^\[Prime])[t]; > acel[t_]==(r^\[DoublePrime])[t]; > unitario[t_]==acel[t]/Norm[acel[t]]; > derivadadeu[t_]==(unitario^\[Prime])[t]; > du[t_]==derivadadeu[t]//FullSimplify; > > Manipulate[ > > f1==Graphics[{Thick,Blue,Arrow[{r[t],r[t]+du[t]}]}]; > f2==Graphics[{Thick,Green,Arrow[{r[t],r[t]+unitario[t]}]}]; > g1==ParametricPlot[r[t],{t,a,b},AspectRatio->Automatic,PlotStyle->{Thick, == > Red}]; > txt==Graphics[{Text[Style["\!\(\*OverscriptBox[\"u\", > \"^\"]\)",18,Bold],(r[t]+1.1unitario[t])],Text[Style["\!\(\* > OverscriptBox[== > \"u\", > \"^\"]\)^\[Prime]",18,Bold],(r[t]+1.1du[t])]}]; > punto==Graphics[{PointSize[.02],Point[r[t]]}]; > Show[g1,f1,f2,txt,punto,PlotRange->All] > ,{t,a,b}] > ]