0
$\begingroup$

Sorry for the bad title, I wasn't sure how to ask this specific question.

So for a (extra credit) homework assignment, I wrote a python program for my differential equations class that should model the following problem:

A group of immigrants are crossing borders. At each border crossing, each immigrant flips a coin to decide whether to pass through or stay in the country they are in. How many are left at N border?

I modelled it like this: $$N'+0.5N = 0 , N(0)=1000$$ Solving the linear ODE: $$N=1000e^{-0.5x}$$

So that should be the equation of the line, right? We were supposed to model it by flipping 50 coins, but I said nope to that and wrote 90 lines of python, that goes something like this:

start with 1000 immigrants
for each border, subtract a binomial random number of them
#for example, 1000-(np.random.binomial()) = approx 500
repeat 10 times
average y-values and take natural log
do linear regression on the y-values
print out equation and chart

The resulting chart looks something like this: Chart of Data, Fit line, and expected line

This result is unexpected. The grey lines are the data, the red line is the line of best fit (Approximately $y=1050e^{-0.7x}$) and the blue line is the expected curve. ($y=1000e^{-0.5x}$) I have tried my linear regression step a number of different ways, (including scipy's exponential fit feature) but I always get a $b$ value (where $y=ae^{bx}$) of -0.68 to -0.73 or so.

What's up with that? Is it my model or something wrong with my code?

edit: the code I'm working with is here in case someone would like to view it: https://github.com/isademigod/populationproblem

$\endgroup$
4
  • $\begingroup$ I don't think so. In English, the ODE can be read as "the number of people on the next crossing is half of the people on the current crossing, starting with 1000". Why would it be -1.5? $\endgroup$ Commented Sep 23, 2021 at 20:16
  • $\begingroup$ Oh, then it should be $N(x+1)=0.5 N(x)$ with $N(0)=1000$. Which leads to $N(x)=1000 \cdot 2^{-x}$? Note that $1/2 = e^{-\ln(2)}$ and $\ln 2 \approx 0.69$. $\endgroup$
    – angryavian
    Commented Sep 23, 2021 at 20:18
  • $\begingroup$ I'm sorry, I guess my DE skills aren't what I thought they were. How did you get that answer? I took $N(x+1)=0.5 N(x)$, subtract $N(x)$ from both sides to get $N(x+1)-N(x) =-0.5 N(x)$, read that as $N'=-0.5N$, form it as a linear DE like $N'+0.5N=0$, use $e^{0.5x}$ as my integrating factor, and that gives me $N=ce^{-0.5x}$. I got the same answer solving it as a seperable DE. $\endgroup$ Commented Sep 23, 2021 at 20:49
  • $\begingroup$ No, it does not work that way. Think of the continuous case more like that on every step of the way, a decision is made with some small probability that accumulates from station to station to $50\%$. Then $N'=aN$ with $e^a=0.5$ or $a=-\ln2$. $\endgroup$ Commented Sep 24, 2021 at 5:23

1 Answer 1

0
$\begingroup$

As noticed in the comments, for a discrete solution, we have that

  • $N(x+1)=N(x)-\frac12 N(x)=\frac12 N(x)$
  • $N(0)=1000$

that is

$$N(x)=\frac{1000}{2^x}$$

indeed

  • $N(1)=\frac12 N(0)$
  • $N(2)=\frac12 N(1)=\frac1{2^2}N(0)$
  • $\ldots$
  • $N(x)=\frac12 N(x-1)=\frac1{2^x}N(0)$

Edit

If we solve it in the continuous case, we have $$N(x+1)-N(x)=-0.5N(x) \to N'+0.5N=0$$ with $N(0)=1000$ which leads to $$N(x)=1000\cdot e^{0.5x}$$

$\endgroup$
3
  • $\begingroup$ What do you call this method? In my whole 4 weeks of DE class, I haven't solved an equation like that. What would the actual DE look like? Because the way my classmates laid it out it was $N′+0.5N=0$, which I thought to solve as a linear DE. $\endgroup$ Commented Sep 23, 2021 at 21:05
  • $\begingroup$ @CoryCox This is a solution for a discrete difference problem. If we solve it in the continuous case indeed we have $$N(x+1)-N(x)=-0.5N(x) \to N'+0.5N=0$$ with $N(0)=1000$ which leads to $$N(x)=1000\cdot e^{0.5x}$$ $\endgroup$
    – user
    Commented Sep 23, 2021 at 21:10
  • $\begingroup$ Well would you look at that! I was wondering if this solution was beyond the scope of my class, and: From my textbook: "In this course we shall be concerned only with continuous-time systems". Thanks for the help. Is there a resource you can recommend for me to get a crash course in discrete systems? $\endgroup$ Commented Sep 23, 2021 at 21:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .