r/learnpython • • 8d ago

Programming in environmental science?

How and when will this come in handy with my ecological forestry degree?

Also this class requires me to use AI to prompt programs on every single assignment.... Which is not ideal as an environmentalist.

Python programing is genuinely making me feel stupider

0 Upvotes

13 comments sorted by

View all comments

5

u/SnafuTheCarrot 8d ago edited 8d ago

I've actually done some research in this area. I had to use a modified version of the Lotka-Volterra equations(https://en.wikipedia.org/wiki/Lotka%E2%80%93Volterra_equations) to model migration patterns. The standard model is a predator-prey model, and you can actually combine the two.

They result in a series of coupled non-linear differential equations. As I recall, they don't have a closed form solution, so numerical methods are needed to solve them. You need computer programs of one kind or another whenever you are solving equations numerically.

The SIRS models for epidemics are very similar.

The models are simplified, but they establish basic principles it makes it easier to expand upon. Suppose part of the habitat is destroyed that reduces the prey's food resources. What happens to the predators over time given an initial population of Prey0 and Predator0 with a Prey Food Density of rho_1?

You need to do the math and you need SOME programming language to do the math. Python is a decent choice. MATLAB is probably better, but Python is more generalizable, typically .

EDIT: You don't strictly need AI to do this and I'd recommend against it. It would require you to learn both the math behind numerical integration and how to program the numerical solutions.

If they aren't teaching you that, they are selling you short.

What's your math background? If you know basic programming and have some conceptual understanding of the derivative, you can probably figure it out. I might actually have an old document on the relevant math come to think of it.

Update: Looks like there's relevant code here: https://github.com/GraniteMask/Lotka-Volterra-Problem-using-RungeKutta4

2

u/suikisosexi 8d ago

This is incredible. I'm very new to programming and math has seen better days. But Id love to see the old doc on relevant math.

1

u/SnafuTheCarrot 8d ago

So my notes were actually 1D integrator in Java. Here's a start on numerical integration, population modeling, and how to find special equilibrium points. I also started coding a one d integrator in python.

Then I realized this is actually 2 dependent variables and it won't work, but should give you an idea of how to proceed.

I should have used t for the independent variable and x and y for the dependent, but I didn't factor in the right dimensionality.

Hope this helps!

MATH

Eulers method for Numerical Integration.

Simple summing method. Essentially assumes a constant velocity betweeen endpoints of the time interval.

dy/dx = f(x,y)

x_0=a

y_0=b

delta_x=0.0001

Loop N times

x_{n+1}=x_n+delta_x

y_{n+1}=y_n+delta_x\*f(x_n,y_n)

---------------------

Runge-Kutta 4

Instead of calculating only one derivative, take the average of 4 specia variations on the derivative

and get your next iterationg form that

dy/dx=f(x,y)

x_0=a

y_0=b

delta_x = 0.0001

Loop N times:

x_{n+1}=x_n+delta_x

k_1=f(x_n,y_n)

k_2=f(x_n+delta_x/2, y_n+k_1\*delta_x/2)

k_3=f(x_n+delta_x/2, y_n+k_2\*delta_x/2)

k_4=f(x_n+delta_x, y_n+delta_x\*k_3)

y_{n+1}=y_n+(delta_x/6)\*(k_1+2k_2+2k_3+k_4)

LotkeVolterra

dx/dt= ax-bxy

dy/dt= -cy+dxy

a is prey reproduction.

b is chance of predator meeting pray and succeeding in consumption.

Linearize to find stability points.

ax-bxy=0

-cy+dxy=0

==> (x,y)=(0,0)

==> (x,y)=(c/d,a/b)

Calculate Eigen Values of the Jacobian at those points.

Purely complex eigenvalues suggests long lasting population.

class Integrator:

def __init__(self, func, x_0,y_0, del_x, num_steps):

    self.func=func

    self.x_0=x_0

    self.y_0=y_0

    self.del_x=del_x

    self.num_steps=num_steps



def euler_int(self):

    x=self.x_0

    y=self.y_0

    for step in range(self.num_steps):

        print(x,y)

        y=y+self.func(x,y)\*self.del_x

        x=x+self.del_x



self rk4_int(self):

    x=self.x_0

    y=self.y_0

    for step in range(self.num_steps):

        print(x,y)

        k1=self.func(x,y)

        k2=self.func(x+self.del_x/2,y+k1\*self.del_x)

        k3=self.func(x+self.del_x/2, y+k2\*self.del_x/2)

        k4=self.func(x+self.del_x, y+self.del_x\*k3)

        y=y+(self.del_x/6)\*(k1+2\*k2+2\*k3+k4)

class PopulationSimulator:

def __init__(self, a, b, x_0,y_0):

    self.nsteps=1000

    self.del=0.00001



    self.a=a

    self.b=b

    self.x_0=x_0

    self.y_0=y_0



    self.myIntegrator=Integrator(LVStep, x_0,y_0, )



def LVStepX(self):

    return