Message Boards Message Boards

How do I calculate terms in a recursive relationship of multiple variables?

Posted 11 years ago
I have written code that determines three variables that are related to each other.  I want to step through these calculations for "n" iterations, but don't know how to do this in multiple variables.

The variables (and their dependencies) are:
Torque (as a function of angle)
Angle (as a function of torque, previous angle, and angular velocity)
Angular Velocity (as a function of torque and previous angular velocity)

I calculate these values in the order above (torque, angle, angular velocity) pulling the dependent data from a list for each variable that I append the new data to at the end of each iteration.

Whenever I try to set up a looping structure to perform this calculation multiple times, I get recursion errors.  If anyone has some ideas, I'd really appreciate it.  Sample code would be even better!

Thanks,

Matt
POSTED BY: Matt Aggleton
Dear Matt,

I am not sure what exactly you are looking for but here's an example of a so-called SIR system that models the spreading of a disease in a population. Three interrelated variables (susceptibles, infected, recovered) are iterated.
 (*equations*)
 sus[i_] := sus[i] = sus[i - 1] - \[Rho] sus[i - 1] inf[i - 1];
 inf[i_] := inf[i] = inf[i - 1] + \[Rho] sus[i - 1] inf[i - 1] - \[Lambda] inf[i - 1];
 rec[i_] := rec[i] = rec[i - 1] + \[Lambda] inf[i - 1];
 
 (*initial conditions and parameters*)
 sus[1] = 0.95; inf[1] = 0.05; rec[1] = 0; \[Rho] = 0.2; \[Lambda] = 0.1;
 
 (*time course*)
tcourse = Table[{sus[i], inf[i], rec[i]}, {i, 1, 100}] // AbsoluteTiming

(*Plot*)
ListPlot[Transpose[tcourse]]

Of course there are other ways such as this one (not that easy to read):
 (*Parameters*)
 c = 0.1; b = 0.2;
 
 Manipulate[results = {{0.05, 0.95, 0.0}}; 
 (*Iterations*)
 For[i = 0, i <= 100, i++, 
  AppendTo[results, {(1 - c) results[[-1, 1]] + b*results[[-1, 1]] results[[-1, 2]], results[[-1, 2]]*(1 - b results[[-1, 1]]), results[[-1, 3]] + c results[[-1, 1]] }]]; 
 (*Plotting*)
  ListPlot[Transpose[results]],
{{c, 0.1}, 0.0, 0.9}, {{b, 0.2}, 0.0, 0.9}]

Note that in the second code the "variables" are in a different order. I hope this helps a bit.

M.
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