# How can I implement this recurrence equation in Mathematica?

I asked a similar question yesterday but today I realized that I was implementing the wrong function. I have tried changing the implementation but I can't get it to work.

Problem:

There is a river which runs straight along the x-axis, distance from shore to shore is 100 meters (along the y-axis). Like this:

A boat is travelling from one shore to the other. The boat is affected by the water. The equation for the boat's position is given by the equation: $$r_{i+1}=r_{i}+V_{i}{\Delta}t$$

Where $$V_{i}=(\frac{1}{100}*D(t), 0.40)$$

D is the distance to the shore at the current time, so $D_{max}=50$. ${\Delta}t$ is the difference in time.

My two failed implementations:

V = {1/100*r[i - 1, dt], 0};
r[i_, dt_] := r[i, dt] = r[i - 1, dt] + V*dt;
r[0, dt_] = {0, 0};
TravelPath = Table[r[i, 5], {i, 1, 50}];
For[i = 0, i < 10, i++, Print[r[i, 5]]]
ListLinePlot[TravelPath]

**\$RecursionLimit::reclim: Recursion depth of 1024 exceeded.**


The question I asked yesterday: How can I express this recurrence function in Mathematica?

## 1 Answer

r[i_, dt_] := r[i, dt] = r[i - 1, dt] + v[i, dt]*dt;
r[0, dt_] = {0, 0};
v[i_, dt_] := {1/100*(Min[#, 100 - #] &@Last@r[i - 1, dt]), 0.4};

ListPlot[Table[r[n, 2.5], {n, 1, 100}]]


An even better implementation (similar to the answer of kguler to your previous question):

f[Δt_] :=
NestWhileList[# + {1/100*(Min[Last@#, 100 - Last@#]), 0.4}*Δt &, {0, 0}, Last@# < 100 &]

ListPlot[f[1]]


• Wow! Thank you! If it's not too much to ask, do you mind explaining how/why it works? – Lightvvind Mar 8 '15 at 19:53
• @Lightvvind If you could be more specific, I'm willing to explain it a little bit. – Karsten 7. Mar 8 '15 at 20:33