Dynamic Objects

In this tutorial, we will work with dynamic variables, whose values can be modified outside of the initial evaluation.  This centers around the Dynamic command.

Dynamic

Go to this tutorial: tutorial/IntroductionToDynamic.  Copy and paste the entire content into a new notebook.  (Ctrl/Cmd-A, Ctrl/Cmd-C, Ctrl/Cmd-V)  You must evaluate each line of code as you work your way through it.  The whole class will be working up through the section “Where Should Dynamic Be Placed in an Expression?”.  Expert users should complete the entire tutorial.

At the end of each section return to this notebook and complete the corresponding comprehension question.

Comprehension Questions

1. The Fundamental Principle of Dynamic.

1a. What is happening to the output of Dynamic[x^2] when you evaluate the line x=Plot[Sin[x],{x,0,2Pi}]?  Why?

2. Dynamic and Controls

2a. Why is there a difference between Slider[Dynamic[x]] and Dynamic[Slider[x]]?

2b. Why isn’t the output for z updating here?  How can you fix it?

z=0.8

Slider[Dynamic[z]]

2c. Create three sliders that all slide together when you move any one of them.

2d. Look at the code below and understand what is happening.  Give a suggestion about what you might be able to do with such power!

Dynamic[MousePosition[]]

3. Dynamic and Other Functions

3a. Create a list of three objects that update together, where one of them is a slider for integers n between 1 and 10, one is the value that is n^2, and one is the 10*sqrt(n)

3b. Use the Column command to nicely format your answer to Question 3a.

4. Localizing Variables in Dynamic Output

4a. Explain the difference between Module and DynamicModule.  What are the benefits of using DynamicModule?  When must you use DynamicModule in place of Module?

4b. Why is the following code not updating properly?

DynamicModule[{x=.5},{Slider[x],Dynamic[Plot[Sin[10y x],{y,0,2Pi}]]}]

5. The Second Argument of Dynamic

You do not need to complete or understand the second half of this section, starting “For complete control...”

5a. Explain in your own words what the second argument of Dynamic does.

6. Where Should Dynamic Be Placed in an Expression?

6a. Explain in a sentence why the tutorial’s In[38] and In[39] behave differently.

Miscellaneous

7. Create a piece of code that keeps track of the mouse’s position and as the mouse moves around, set the variable a to be the first coordinate and the variable b to be the second coordinate.

Using Dynamic with Manipulate

Next, learn how Dynamic integrates with Manipulate by working through this tutorial: tutorial/AdvancedManipulateFunctionality