0
$\begingroup$

I have generated two time series, S[t] and P[t] as follows:

In[2990]:= Table[S[t], {t, 1, 50}]

Out[2990]= {0.5, 0.5, 2.253, 2.7614, 2.87762, 4.05318, 5.03183, 5.42801, 6.32663,
7.43033, 8.14412, 8.98434, 10.0983, 11.0536, 11.9744, 13.0988, 14.2194, 15.2811,
16.4588, 17.7028, 18.9201, 20.1964, 21.5544, 22.9261, 24.3338, 25.8165, 27.3415,
28.9023, 30.5282, 32.2123, 33.9413, 35.7309, 37.5859, 39.4971, 41.4699, 43.5124,
45.6209, 47.7963, 50.0454, 52.3689, 54.7668, 57.2436, 59.8024, 62.4438, 65.1712,
67.988, 70.8963, 73.8987, 76.9988, 80.1996}

In[3057]:= Table[P[t], {t, 1, 50}]

Out[3057]= {0.75, 0.75, 0.75, 0.75, 2.22, 2.26635, 2.31409, 4.72832, 5.47929, 5.71359,
7.38173, 8.77145, 9.38649, 10.687, 12.2584, 13.3072, 14.5348, 16.1313, 17.5145,
18.8577, 20.4782, 22.0947, 23.6365, 25.3391, 27.1332, 28.8952, 30.7413, 32.7011,
34.6829, 36.7181, 38.8585, 41.0603, 43.3149, 45.6623, 48.0927, 50.5888, 53.1718,
55.8482, 58.6059, 61.4525, 64.3987, 67.44, 70.5779, 73.8214, 77.1718, 80.6293,
84.2004, 87.8892, 91.6969, 95.6281}

Now, these two time series are related to each other by a lag, L[t]. Thus, S[t]==P[t-L[t]]. Time series of L[t] is not known and I have to find it from given S[t] and P[t].

My code for this is:

Do[{Do[If[S[t] > P[i], k = i], {i, 1, t}],Print[t-k]},{t,5,50}]

The list of numbers printed out, i.e. t-k, is exactly what I expected. So I would like to define L[t_] := t - k. But if I substitute this for Print[t-k] in the code and Print the resulting list of numbers, I get a different result. Is there any way to define this function?

$\endgroup$

2 Answers 2

2
$\begingroup$

If you insist on using the Do construct, simply:

ClearAll[l]

(* cover undefined values *)
l[_] = Infinity

Do[{Do[If[S[t] > P[i], k = i], {i, 1, t}], l[t] = t - k}, {t, 5, 50}]

l /@ Range[5,50]

(* {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5} *)

Note I used lowercase l - it is a seriously bad idea to use uppercase initials, you risk name-clash.

$\endgroup$
1
  • $\begingroup$ great! Now it works, thank you so much!! You saved me! $\endgroup$
    – ppp
    Mar 17, 2014 at 3:38
1
$\begingroup$

This piece,

Do[If[S[t] > P[i], k[t_] := i], {i, 1, t}]

does not have a bound on the iterator. Mathematica does not know what t is.

How about define $k$ dynamically?

k[t_] := k[t]=Module[{i=1}, While[S[t] <= P[i],i++]; i];

Is this what you intend?

$\endgroup$
9
  • $\begingroup$ thanks so much. I have slightly changed my original code, reflecting what you pointed out, i.e. identifying the range of t. But there is still some problem unresolved. (k and i are not part of the model but just arbitrary notations.) $\endgroup$
    – ppp
    Mar 16, 2014 at 18:48
  • $\begingroup$ Hm, ok, well, I am not entirely sure what info you got, and what is unknown... $\endgroup$ Mar 16, 2014 at 18:51
  • $\begingroup$ actually, I have revised my question reflected your comments and also some of the improvements I have made. Is it clearer? $\endgroup$
    – ppp
    Mar 16, 2014 at 19:04
  • $\begingroup$ Oh, you want ListPlot[L/@Range[5,100]] $\endgroup$ Mar 16, 2014 at 19:13
  • $\begingroup$ But I'm not sure how to code L in the first place. L[t] should be t-k. So I tried L[t_]:=t-k. But it doesn't work. $\endgroup$
    – ppp
    Mar 16, 2014 at 19:22

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.