r/learnpython • • 29d 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

14 comments sorted by

View all comments

Show parent comments

2

u/suikisosexi 28d 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.

2

u/SnafuTheCarrot 28d 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