numerical-mooc / assignment-bank

Contribute alternative assignments for Numerical Methods with Python
MIT License
14 stars 75 forks source link

Added new ODE assignment:Predator Prey Model #3

Closed dsrivallabha closed 9 years ago

dsrivallabha commented 10 years ago

This new assignment in ODEs demonstrates use of Python to solve Predator Prey Model, also known as Lotka Volterra Equation.

IanHawke commented 10 years ago

A few comments:

  1. Is there a particular reason for including $t$ in the state vectors here? The ODEs don't directly depend on time, so it's not needed.
  2. You'll need a %matplotlib inline early on or the plotting calls will make it freeze.
  3. This might be a place to use widgets to explore the parameter space rapidly.
dsrivallabha commented 9 years ago

Hello Sir, thank you for the comments. Here is what I think.

  1. The first plot to usually check in case of this model is the variation of population of both species over time. So, I included time as a dummy variable in the vector. I am not sure how to plot a graph for variation of a single quantity with index in Python.
  2. "%matplotlib inline" is not working on my machine with Ubuntu 12.04 + ipython notebook + firefox browser right for the beginning. So, I have been using plt.show(). Will include this line in the notebook.
  3. I do not fully understand the third comment. Would you like the code to test various values of the four parameters and see how the populations interact? If that is what is required, I will add that.
gforsyth commented 9 years ago

Hi @dsrivallabha ,

The version of IPython in the Ubuntu 12.04 repositories is pretty old. You can update it by installing pip and then upgrading to the latest version.

sudo apt-get install python-pip
sudo pip install --upgrade pip
sudo pip install --upgrade ipython[notebook]
IanHawke commented 9 years ago
  1. This is more a question of style; for evenly spaced points you can do time = numpy.linspace(0.0, T, N) to get the time vector, which reduces the size of the state vector.
  2. Odd; ok.
  3. Rather than changing the parameter values by hand and then rerunning and replotting, there's some interactive widgets in the notebook that simplify matters. You need to ensure you're running a reasonably recent ipython notebook (recommend > version 2.0). Have a look at eg https://github.com/IanHawke/RiemannPython/blob/master/Lesson_03_Characteristics.ipynb (http://nbviewer.ipython.org/github/IanHawke/RiemannPython/blob/master/Lesson_03_Characteristics.ipynb)
dsrivallabha commented 9 years ago

Hi @gforsyth,

I tried doing what you suggested. I was able to install and upgrade pip. I was unable to upgrade ipython notebook. It required a gcc compiler that is not currently installed. If I try to open ipython notebook now, it says tornado >= 3.1.0 is required which is not installed. So, ipythone notebook doesn't work now and I am trying to go back to the earlier state.

gforsyth commented 9 years ago

Sorry about that! You can do

sudo apt-get install build-essential

And that will fix the compiler errors.

dsrivallabha commented 9 years ago

Even the above command did not work. I installed latest version of Tornado, meddled with pyzmq and finally got the notebook working. The process of installation on Ubuntu seems to be slightly complicated. Anyway, will follow up the earlier comments by Prof. Ian Hawke.

dsrivallabha commented 9 years ago

Added changes 1 & 2 suggested by Prof. Ian Hawke. I am unable to view any widgets suggested in 3 by Prof Hawke. I may not incorporate them. This will be the final commit for this assignment unless there are any changes to logic. Please let me know your thoughts.

IanHawke commented 9 years ago

As a logical set of exercises it makes sense to me.

Some minor coding points;

  1. Certain floats should be explicitly coded as such in case of python 2 integer division problems (eg gamma, delta, x0, y0, t0, constants in RK4 algorithm)
  2. numpydoc style documentation in the RK4 algorithm is my strong preference
  3. IPython notebook sectioning would make it a little clearer.

O/w, +1.

dsrivallabha commented 9 years ago

Hello Sir, Made the following changes you suggested.

  1. Defined parameters and other variables as floats explicitly.
  2. Updated documentation for RK4 scheme.
  3. Added sections to notebook. Hope the assignment is in better shape now.
IanHawke commented 9 years ago

Looks good to me. Any comments from eg @labarba or @gforsyth ?

gforsyth commented 9 years ago

Looks good. I have one suggestion.

Consider changing the phase plot to add these options:

plt.plot(x_euler, y_euler, '-->', markevery=5, label = 'phase plot')

It just adds arrow pointers so that it's easier to tell where things begin and end.

labarba commented 9 years ago

@dsrivallabha That is super! Do add a note at the end with your authorship: the copyright of your writings are yours, of course, but we encourage you to liberate your work with a Creative Commons attribution license (CC-BY 4.0). If you agree, then, you should add your name and the CC-BY note somewhere. Nice contribution to the Assignment Bank!

dsrivallabha commented 9 years ago
  1. Added arrow pointers to phase plot.
  2. Added my name and CC license at the end of the note. The assignment is in its final shape now.

Thank you @IanHawke @labarba @gforsyth for your valuable suggestions.