Message Boards Message Boards

1
|
8099 Views
|
4 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Recursive model, how can I use if statements?

Posted 11 years ago
Hello,
I have a question about how to make an If[condition,then,else] statements for recursive expressions. I want to model a population that can't be negative so that when it hits 0 or a negative value, it assumes the value of zero for all subsequent time points. This is a bit tricky as my variables depend on each other. 


Right, so here is more information about the model that I've created (I really don't mean to start a discussion about Polio or Syria, it's just a school project and I'm sorry if its offensive. I know Polio is not the biggest problem, I just thought it would be interesting)

The question: To look at the spread of a disease and see which parameters are the most important to minimize the number of infected people. 
Polio (P)== P*S*G +(Mp)+P(1-d-h) Immunized (Imm) == P*h+S*v+Imm+Mimm Susceptable (S) == S*(1-v-P*G)+Ms +b
Where the parameters are: G = rate of infection from a polio infected indiv to a susceptable indiv.d = death from polioh = people with polio who get healthy and turn into immune healthy individuals v = rate of effective vaccination from susceptable people to immune peopleb = births, all babies are born susceptable to polioM = immigration to syria - emmigration from sytria (this can also include death from other causes such as war)    -> the M have subscripts as they are different for each variable. 


Here is my set of equations where P, Imm, and S are variables to describe the groups in my population. t is time and the rest are parameters that I can set and manipulate. 

 P[t_, G_, Mp_, d_, h_, v_, Ms_, Mimm_, b_] :=
  P[t, G, Mp, d, h, v, Ms, Mimm, b] =
   P[t - 1, G, Mp, d, h, v, Ms, Mimm, b]*
     S[t - 1, G, Mp, d, G, v, Ms, Mimm, b] *G + Mp +
    P[t - 1, G, Mp, d, h, v, Ms, Mimm, b] (1 - d - h)
 
 
 Imm[t_, G_, Mp_, d_, h_, v_, Ms_, Mimm_, b_] :=
  P[t - 1, G, Mp, d, h, v, Ms, Mimm, b]*h +
  S[t - 1, G, Mp, d, G, v, Ms, Mimm, b] *v +
  Imm[t - 1, G, Mp, d, h, v, Ms, Mimm, b] + Mimm


S[t_, G_, Mp_, d_, h_, v_, Ms_, Mimm_, b_] :=
S[t, G, Mp, d, G, v, Ms, Mimm, b] =
  S[t - 1, G, Mp, d, G, v, Ms, Mimm,
     b] (1 - v - P[t - 1, G, Mp, d, h, v, Ms, Mimm, b]*G) + Ms + b

I thought it would work if I had written this (with an If statement modifying each definition)
P[t_, G_, Mp_, d_, h_, v_, Ms_, Mimm_, b_] :=
If[P[t-1, G, Mp, d, h, v, Ms, Mimm, b]>0, P[t, G, Mp, d, h, v, Ms, Mimm, b] =
  P[t - 1, G, Mp, d, h, v, Ms, Mimm, b]*
    S[t - 1, G, Mp, d, G, v, Ms, Mimm, b] *G + Mp +
   P[t - 1, G, Mp, d, h, v, Ms, Mimm, b] (1 - d - h), P[t, G, Mp, d, h, v, Ms, Mimm, b]=0]
But I get an error where the "recursion depth was exceeded".

I think there is an easy way of doing this, but I can't seem to find it. Any help would be appreciated, thank you!
-M
POSTED BY: Maria Kay
4 Replies
Posted 11 years ago
I put up my actual model, I'll try to come up with a simpler case of the same issue
POSTED BY: Maria Kay
This looks very interesting, so, like Nasser, I'd be interested in learning what you are actually modeling, and what is the underlying science and math.

But irrelevant of that, could you reflect your problem in an *exemplarily* simple case of single variable, simple equation and some trouble If statement? It does not have to relate to your model, but only to your programming issue. It is important though that in that example everything would be defined - so unknown things like r' will not appear.
POSTED BY: Sam Carrettie
Posted 11 years ago
I put up the model I'm actually working with (I thought it would be a bit more to look at but maybe it's easier to fix). -- woops

Sorry to have misunderstood, I'll give more information then, thanks. 
POSTED BY: Maria Kay
What is
r'
It will also help if you post the actual model you are trying to model (i.e. the actual math)

Please post the mathematical model itself. Not just the code. That is what I meant. Code is not the model.

If you just post code, then someone has to figure the mathematical model by reverse engineering your code, and if your code has a bug, they will end up with the wrong model, and it will also take more time.

If you post the Mathematical model, a link is fine, someone might be able to show how to implement it much better than what you have as well, it is also easier to understand the model by looking at a math equation than code.
POSTED BY: Nasser M. Abbasi
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