1
$\begingroup$

Do I have to clear all my definitions whenever I experiment with different parameter values for my model? For example, I have a simple model as follows:

w[t_]:=w[t]=w[t-1]+h
w[0]=0    
h = 5

I want to do:

Table[w[t], {t, 0, 10}]

with varying h. But every time I change h, I have to clear the definitions with

Clear["Global`*"]

Otherwise, the result for w[t] remains the same. This practice is ok with this simple model. But when the model is pretty big, it really is time-consuming. I was expecting to get a new result directly by changing h. Any helpful comments in this direction?

$\endgroup$
4
  • 1
    $\begingroup$ Could you use w[t_,h_] instead of setting h afterward ? $\endgroup$ Mar 13, 2014 at 20:20
  • $\begingroup$ @WilliamBriand, thanks. I can try. But my actual model is pretty big with so many parameters. So it would be impossible to put all of them into the bracket. $\endgroup$
    – ppp
    Mar 13, 2014 at 20:31
  • $\begingroup$ @Kuba, thanks. I think that's what I did. You can see h gets value at the end. $\endgroup$
    – ppp
    Mar 13, 2014 at 21:17
  • $\begingroup$ Enclosing things in Dynamic allow for post hoc update of variables -- though it may make it considerably slower to run. $\endgroup$ Mar 13, 2014 at 21:26

1 Answer 1

1
$\begingroup$

You could just clear the DownValues you don't want.

w[t_] := w[t] = w[t - 1] + h
w[0] = 0;

Table[DownValues[w] = Drop[DownValues[w], {2, -2}]; Table[w[t], {t, 0, 3}], {h, 2, 5}]
{{0, 2, 4, 6}, {0, 3, 6, 9}, {0, 4, 8, 12}, {0, 5, 10, 15}}
$\endgroup$
1
  • $\begingroup$ Thank you so much, m_goldberg. But it seems quite complicated. In fact, I was moving over from R. With R, you just change h directly and you will have new results for w[t]. I was expecting something similar to this. It seems R is much more convenience than Mathematica for experimenting with a dynamic model with many variables and parameters. Is it true? $\endgroup$
    – ppp
    Mar 13, 2014 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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