Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

I have a problem in defining the local variables.

I have this code:

Manipulate[
 Grid[{

   {Grid[{
      {k, Cos[k] // N},
      {a = Dynamic[p], y = Cos[a], Sin[y]}
      }]},

   {Grid[{{
       Graphics[{

         {Gray, Line[RandomReal[{-1, 1}, {1000, 2}]]}, {Red, 
          Disk[a, 0.05]}}, PlotRange -> {{-1, 1}, {-1, 1}}, 
        ImageSize -> 500

        ]}}
     ]}}], {{p, {0, 0}}, Locator}, {k, 1, 10, 1}]

if you notice that for certain value of k, p is dynamically located and then a is set to be equal to a, but when calculating Cos[a], this expression is not calculated and neither sin[y].

How to make the dynamic variables and all of its dependents only the variables who changes. I tried Dynamic but only the dynamic variable is changing not its dependents. can you please suggest some way to make the dynamic variable and all of its dependents changing?enter image description here

share|improve this question

marked as duplicate by Kuba, rasher, belisarius, Yves Klett, Oleksandr R. Apr 24 at 9:24

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
It's not clear what you are trying to accomplish, e.g.is the random pattern supposed to change when you move the k slider? –  Simon Woods Apr 23 at 18:57
2  
I think it works if you change two things: remove the Dynamic from around p, and change the k iterator to {k,1,10} (i.e., remove the integer restriction). Now it evaluates the Cos and the Sin[Cos[]] terms when you move the red dot around. –  bill s Apr 23 at 19:33
    
... or use y = Cos @@ a, Sin @ y instead of y=Cos[a],Sin[y] (to remove the Head Dynamic from a)? Alternatively, you can use y=Cos[Setting@a] to get at the value of a. –  kguler Apr 23 at 21:45
    
There is no point in inner Dynamic since the whole Manipulate body is wrapped with this. The problem is also that Dynamic[5] is not a number. –  Kuba Apr 23 at 22:05
    
Hi all, the problem I faced in this case is I don't want the manipulate to re-evaluate every thing when I change the manipulate parameters. if you see I plotted a gray lines so that not to change when I change some the manipulate parameters. if I remove the dynamic function then every thing will be re-evaluated form scratch which I don't want. –  Algohi Apr 26 at 0:50

1 Answer 1

Your code can be made to work as you expect to by simplifying it.

With[{imgSize = 300, nPts = 100}, 
  Manipulate[
    Grid[{
      {k, Cos[1. k], ""},
      {p, y = Cos[p], Sin[y]}, 
      {Graphics[{
         {Gray, Line[RandomReal[{-1, 1}, {nPts, 2}]]}, 
         {Red, Disk[p, 0.05]}}, 
         PlotRange -> {{-1, 1}, {-1, 1}}, 
         ImageSize -> imgSize], 
       SpanFromLeft, SpanFromLeft}
    }],
    {{p, {0., 0.}}, Locator},
    {y, None},
    {k, 1, 10, 1}]]

Your variable a served no useful purpose, so I removed it.

Localizing y was effected by introducing an invisible control -- {y, None}. This is the normal (i.e., idiomatic) way of introducing a localized variable into a Manipulate expression.

Reducing the nested Grid expressions to one Grid did nothing to fix your problem. All those unnecessary nested Grid offended my sense of code aesthetics, so they had to go.

mess

share|improve this answer
    
Thanks for the answer but if you notice in your code, if you change any manipulate parameters then every thing will be calculated and re-evaluated which I don't. –  Algohi Apr 26 at 0:51
    
I have come up with solution to the problem which is basically I have to rap every thing I want only to be calculated with Dynamic function. so the solution is as follows:Manipulate[ Grid[{{Grid[{{k, Cos[k] // N}, {Dynamic[{a = p, y = Cos[a], Sin[y]}]}}]}, {Grid[{{Graphics[{{Gray, Line[RandomReal[{-1, 1}, {1000, 2}]]}, Dynamic[{Red, Disk[a, 0.05]}]}, PlotRange -> {{-1, 1}, {-1, 1}}, ImageSize -> 500]}}]}}], {{p, {0, 0}}, Locator}, {k, 1, 10, 1}] –  Algohi Apr 26 at 0:57
    
So what every I change the Manipulate locator parameter then only some expressions in the manipulate are recalculated. No every thing re-evaluated when changing the locator. however when I change the k parameter then every thing will be recalculated. The question remains because it is not logic to rap every things you want to be calculated with Dynamic function epically when the code is long. I am still asking if there is any other easy want to tell Manipulate to evaluate only expressions ( not all expression) that depend on specific Manipulate parameter. –  Algohi Apr 26 at 1:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.