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.

I'm trying to get this code working:

a = 2;
func[t_] := Dynamic[t + a];
Plot[func[t], {t, -5, 5}]

The idea is: I'd like to have the dynamic parameter a change the graph, without the need of actually re-evaluate the Plot command after each change of the parameter a. But this code delivers no plot printed on the screen. I've tried adding an Evaluate command as well, without luck.

What am I missing of the Dynamic command? Or where did I do something wrong?

Thank you all for your answers and your help!

share|improve this question
    
I can read this question a couple of ways. It is easy to do if Plot is to be reevaluated dynamically as the current answers show -- that is, the user does not have to hit shift-enter. It is possible to do if you do not want the function Plot to be actually reevaluated every time the parameter a changes, but the result will likely be dissatisfying, unless your example func is exactly the function you want to use. –  Michael E2 Aug 5 '13 at 15:24
    
@MichaelE2 I'm intrigued by the second possibility you mention. I don't know a lot about the dynamic capabilities of Mathematica, so could you elaborate what you have in mind? –  sebhofer Aug 5 '13 at 15:50
    
@sebhofer What I had in mind was taking the Graphics output from Plot and remapping the coordinates to depend dynamically on a. It seems a bad idea to me. Since Plot makes choices depending on func, you lose that. (The sample function could be done with Graphics@Dynamic@Line[{{-5, -5+a}, {5, 5+a}}], but I assume the OP has something more complicated in mind.) I haven't been able to imagine a situation in which this second method would be a real advantage. But technically, it should be possible. –  Michael E2 Aug 5 '13 at 16:00
    
@MichaelE2 Now I see what you... that would of course be closer to the OP's original idea. –  sebhofer Aug 5 '13 at 16:12
1  
In your comments to answers provided you below keep talking about using locators. You need to put the details of what you want in your question. Readers are not psychic. –  Mike Honeychurch Aug 5 '13 at 22:11

3 Answers 3

Here's an easier way to do what I mentioned in a comment. We avoid Plot altogether and simply plot a line. You miss out on the recursive refinement that Plot offers to smooth out curves of high curvature, but in some cases that might be ok. You can even make the pllot depend on the function dynamically.

a = 2;
f = func1;
func1[t_] := t + a;
func2[t_] := Sin[a t];
Block[{a, f}, pts = Table[{t, f[t]}, {t, -5, 5, 1/10}]];

Graphics[
  GraphicsComplex[
   Dynamic @ pts,
   {Darker @ Blue, Line[Range @ Length @ pts]}],
 Frame -> True]

Mathematica graphics

Change f:

f = func2

Mathematica graphics

Change a

a = 20

Mathematica graphics

Here you see the problem. The function being plotted, Sin[20 t], oscillates too rapidly and you get sharp corners. (Set it much higher and you will miss entire periods.)


If you're interested in simple transformations depending on a parameter, then you can use Geometric Transforms. The following shows the plot of t + a, as sought in the question.

a = 2;
plot = Plot[t, {t, -5, 5}];
Dynamic@ReplacePart[plot, 1 -> Translate[plot[[1]], {0, a}]]

Mathematica graphics

share|improve this answer

Use Manipulate:

Manipulate[Plot[t+a,{t,-5,5}],{a,0,1}]

Now you can vary a between 0 and 1. If you want to plot a more complicated function you can do

f[a_, t_] := a + t;
Manipulate[Plot[f[a, t], {t, -5, 5}], {a, 0, 1}]

Edit

As a response to the OP's comment: Manipulate can also be used with a Locator, like this

Manipulate[Plot[Norm@p2*t + Norm@p1, {t, 0, 1}, PlotRange -> {0, 5}], 
{{p1, {0, 0}}, Locator}, {{p2, {0, 0}}, Locator}]
share|improve this answer
    
Thank you for your fast answer, but I'd like to not use Manipulate. I'm searching for a way of having an "invisible Locator" somewhere on the graph which modulus (distance to 0) is a, and to move the Locator and thus changing the value of a. This is, I think, not possible inside Manipulate—but I could be wrong. Could you help with this too? Thanks a lot in advance! –  Gabriel Aug 5 '13 at 13:35
2  
@user7285 Then you should state so in your question! But yes, this can be accomplished in Manipulate, see edit. –  sebhofer Aug 5 '13 at 14:50

If you want to use Dynamic try to make something like this:

Dynamic[a];
a = 0;
Slider[Dynamic[a]]
Dynamic[Plot[x + a, {x, 0, 6}]]

But, I recommend to use Manipulate, Dynamic is not a suitable tool for this.

share|improve this answer
    
Thank you a lot for your answer, this helped well. I have now this problem: when using the code here, the two Locators move together. I think, it's due to the Plot command, as the two Locators move independently if I delete that command. Where is the problem? DynamicModule[{pt1 = {1, 2}, pt2 = {4, 3}}, Dynamic@Show[ Plot[x *pt1[[1]], {x, 0, 6}, PlotRange -> 10] , Graphics [{ Locator[Dynamic[pt1]] , Locator[Dynamic[pt2]] }] ] ] –  Gabriel Aug 5 '13 at 14:42
    
This does not seem an unreasonable use of Dynamic here. There are issues with a being uninitialized at the beginning of a session. One could use Dynamic[If[NumericQ[a], Plot[x + a, {x, 0, 6}], "Initialize a"]] if for some reason one wants a to be a global variable. (By the way, the first line Dynamic[a]; is unnecessary.) –  Michael E2 Aug 5 '13 at 15:29
1  
@user7285 The Locator question probably ought to be stated as a new question. –  Michael E2 Aug 5 '13 at 15:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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