Message Boards Message Boards

0
|
11116 Views
|
12 Replies
|
7 Total Likes
View groups...
Share
Share this post:

how to place a symbolic DO loop within Manipulate code

Posted 10 years ago

Dear All,

I have a dynamic model in DO loop format over time t. I have a space of parameters. I want to put this DO loop in a Manipulate code in which the model is solved for different parameter values. I face a problem due to a symbolic DO loop before Manipulate command. Is there any way to efficiently integrate DO loop into a Manipulate code and see how the model behaves with different parameter values?

Attached is the model code.

Thanks.

Regards, Jones

Attachments:
POSTED BY: Tugrul Temel
12 Replies

You are looking for something more like this

model[{A_, II_, Y_, CC_}] :=
 Module[{Anew, Knew, Rnew},
  Anew = (1 + z) A;
  Rnew = a Anew CC;
  Knew = (1 - a) (Sqrt[b] Rnew) + II;
  {Anew,
   (1 + m) II,
   (1 + n) Y,
   d (Y - Knew)} ]

NestList[model, {2, .5, 5, 2}, 10]

Instead of having all the Set going on, NestList it's just a consequence of how its setup that the thing keeps changing.

Before doing the Manipulate it needs to know which variables to manipulate, one way to do this is to insert the function into the definition like this

Manipulate[
 NestList[model, {2, .5, 5, 2}, 10], {{z, 0.3}, 0, 1}, {{a, 0.2}, 0, 
  1}, {{ b, 3}, 0, 5}, { {d, 0.6}, 0, 1}, {{e, 0.95}, 0, 
  1}, {{m, 0.2}, 0, 1}, {{n, 0.4}, 0, 1}, 
 Initialization :> (model[{A_, II_, Y_, CC_}] :=
    Module[{Anew, Knew, Rnew},
     Anew = (1 + z) A;
     Rnew = a Anew CC;
     Knew = (1 - a) (Sqrt[b] Rnew) + II;
     {Anew,
      (1 + m) II,
      (1 + n) Y,
      d (Y - Knew)}
     ])]
POSTED BY: Todd Rowland
Posted 10 years ago

Yes, it is what I was after; however, the Manipulate code above does not manipulate the parameters. I run the model and change the parameters but the values of the variables concerned do not change at all. I could not see any problem with your Code. Is there a missing command?

How can you do the same operations (the Manipulate Code above) for a 2nd order or higher-order difference equations?

POSTED BY: Tugrul Temel
Posted 10 years ago

Dear Todd,

I have written a Code using DO loop, which is attached. It works just fine and I obtain the desired result. I tried to express this DO loop using NestList following your advise. I attached the two codes: one with DO loop, which works and another using NestList, which does not work. I would appreciate if you guide me in writing a correct NestList version of the DO loop.

Thanks.

Jones

Attachments:
POSTED BY: Tugrul Temel

But you really should use NestList, which is really the right way to go with these problems. Not only will you learn the best way, but your code will be simpler, cleaner, easier to fix and probably will work the first time.

Besides the documentation there are other places to learn http://reference.wolfram.com/language/tutorial/ApplyingFunctionsRepeatedly.html http://mathematica.stackexchange.com/questions/11380/nestlist-on-a-function-of-several-arguments http://www.wolfram.com/language/fast-introduction-for-programmers/built-in-functions/

POSTED BY: Todd Rowland
Posted 10 years ago

Dear Todd,

I will study NestList code as you advised. I hope I can solve my problem. Thank you for your kind support and guidance in that.

Thanks again. Jones

POSTED BY: Tugrul Temel

A better way to iterate is to use NestList doing it this way will clarify things (I think there are some issues with the Subscripts in your code).

First write a function to iterate, and give it a good name, in this case you have five variables so make a list of length 5. The formula you have depends on some global variables Alpha_1 etc which is fine to include, and it looks like one of the values needs to be computer first so

YourNameHere[{a_,e_,y_,g_,r_}]:= With[{eta=formula for eta}, {formula for alpha, eta, formula for y, formula for g, formula for r}]

then all you do is

NestList[YourNameHere, init, t]

then to plot y and eta, use Part, e.g.,

ListLinePlot[NestList[YourNameHere, init, t][[All,{3,2}]]]
POSTED BY: Todd Rowland
Posted 10 years ago

Dear Todd,

If you have a moment, you look at the attached notebook in which what I want to achieve is shown by using some given parameter values, DO loop and ListLinePlot. The model there works fine but I want to automate the model for different parameter values. That is all I want to achieve. Nested functions, which I do not know much about, are not (I think) proper because I just want to iteratively solve the model for different sets of parameter values.

Please take a look at the attached file which works just fine...I want to automate it with different values of given parameters...I thought Manipulate will work but I cannot properly use Manipulate.

Thanks for your time.

Jones

Attachments:
POSTED BY: Tugrul Temel
Posted 10 years ago

Jones,

Is that an iterative equation, where you are working toward a solution or through steps? If it is simultaneous, what is the intended function of Do[]? Are you receiving an error message that says that you are running out of memory? Could you use variable substitution to reduce it to the most simple form (since the specific econometric application is irrelevant)?

Without knowing more, I think what you may be trying to do is generate outputs for a combination of input sets. If so, there may be a way to do that without forcing Do[] and Manipulate[] together.

The third thing that occurs is that you should attempt to put Do[] within Manipulate[] as a simple test in another file and post any problems encountered in that process to clarify the issue.

Greg

Update: Tried the 3rd thing. If it can work, it will require a more complicated approach than what is below. Output wasn't correct and it paused providing a result.

Manipulate[
 Do[Print[j^3]*sign, {j, 1, 3, 1}],
 {{sign, -1}, -1, 1}]
POSTED BY: Greg
Posted 10 years ago

Dear Greg,

I use DO loop because the model is iterative: yesterday's given input level determines today's output, which in turn determines tomorrow's prices, which are used to run the model again for T periods. This chain of dependence make me use DO loop (I do not know any other way to solve this iterative problem).

Yes, in many trials the error message "maximum 1024 iterations reached" comes up. I simplified the problem and the attached short-version of the model does not give any error.

My goal is to Solve the iterative model with a set of parameter values and that is why I want to use Manipulate Code. Here are the steps to prsue:

1) given a set of parameter values, solve the model in DO loop 2) the solution will generate a time series for each endogenous variable 3) ListLinePlot the time series generated 4) Go To Step (1) and choose another set of parameter values and solve the model in DO loop and follow the steps 2 and 3.

The best way to carry out the above steps is to introduce Manipulate with which different sets of parameters can be chosen automatically and the Plot generated automatically.

That is my goal. I hope that my explanation is clear.

Regards, Tugrul

Attachments:
POSTED BY: Tugrul Temel
Posted 10 years ago

Dear Greg and Marco,

Here is the simplified code that I like to run using Manipulate with different paramater values of alpha, r, and g.

Clear[n];
n = 10;
Subscript[\[Alpha], 1] = 0.4;
Subscript[r, 1] = 1;
Subscript[g, 1\[InvisibleComma]0] = 1;

Do[{
   Subscript[k, 1\[InvisibleComma]t] = 
    Subscript[\[Alpha], 1]/(1 - Subscript[\[Alpha], 1]) Subscript[r, 
     t],
   Subscript[\[Eta], 1\[InvisibleComma]t] = 
    1/(-1 + Subscript[\[Alpha], 1]) E^(-Subscript[\[Alpha], 1]
        Subscript[g, 1\[InvisibleComma]t - 1]) (1/(
      Subscript[r, t] - 
       Subscript[r, t] Subscript[\[Alpha], 1]))^-Subscript[\[Alpha], 
      1],
   Subscript[y, 
    1\[InvisibleComma]t] = (1 - Subscript[\[Eta], 
       1\[InvisibleComma]t]) E^((1 - Subscript[\[Alpha], 
        1]) Subscript[g, 1\[InvisibleComma]t - 1]) Subscript[k, 
     1\[InvisibleComma]t]^Subscript[\[Alpha], 1],
   Subscript[g, 1\[InvisibleComma]t] = 
    Subscript[\[Alpha], 1] Subscript[g, 1\[InvisibleComma]t - 1],
   Subscript[r, t + 1] = 
    Subscript[\[Alpha], 
     1] (1 - Subscript[\[Eta], 1\[InvisibleComma]t])/(1 - 
       2 Subscript[\[Eta], 1\[InvisibleComma]t])}, {t, 1, n}
      ];
ListLinePlot[{Table[Subscript[y, 1\[InvisibleComma]t], {t, 1, n}], 
  Table[Subscript[\[Eta], 1\[InvisibleComma]t], {t, 1, n}]}, 
 AxesLabel -> {t, 
   "output, lobbying\n \!\(\*SubscriptBox[\(y\), \
\(1\[InvisibleComma]t\)]\), \!\(\*SubscriptBox[\(\[Eta]\), \(1\
\[InvisibleComma]t\)]\)"}, PlotRange -> All, 
 PlotLegends -> {"\!\(\*SubscriptBox[\(y\), \
\(1\[InvisibleComma]t\)]\)", 
   "\!\(\*SubscriptBox[\(\[Eta]\), \(1\[InvisibleComma]t\)]\)"}]
Attachments:
POSTED BY: Tugrul Temel
Posted 10 years ago

Dear Marco,

Thanks for your reply. I cleaned up the errors and the attached simple model works fine, though some of the solutions are complex solutions. In order to check whether the model works or not, I assigned an arbitrary parameter values and some initial conditions.

Attached is the model for your suggestion. Here is what I want to achieve.

Place the DO loop into a Manipulate code and then animate the model solutions for a range of parameter values. Instead of assigning a set of fixed parameters for the model solution, I like to animate the solutions for different sets of parameters.

Thanks again. Jones

Attachments:
POSTED BY: Tugrul Temel

HI,

I tried to help but your code still produces other error messages. Perhaps you could clean that up a bit, then it will be easier to help.

Also, I am not quite sure what you want to achieve, but it appears that you want to animate the thing? If so, why don't you put time in as another parameter of the Manipulate command, and when the GUI comes up you click on the little "+" symbol next to the time slider and then click the "play" button?

Cheers,

Marco

POSTED BY: Marco Thiel
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