Y at next timestep,add to array P_arr = P + Dt*dPdt # calc. # Euler's method for i in range (1, n_steps + 1): Y = Y_arr P = P_arr t = t_arr dYdt = -0.4*Y +0.02*P*Y # calculate the derivative of Y dPdt = 0.8*P - 0.01*P*P-0.1*P*Y # calculate the derivative of Y Y_arr = Y + Dt*dYdt # calc. ![]() If you’d like to experiment with different differential equations you can do that here. Now it’s time to create a for loop that will iterate every timestep and calculate the next values of P, Y and t given their differential equations. Y_arr = np.zeros(n_steps + 1) # create an array of zeros for Y P_arr = np.zeros(n_steps +1) # create an array of zeros for P t_arr = np.zeros(n_steps + 1) # create an array of zeros for t t_arr = t_start # add starttime to array Y_arr = Y_start # add initial value of Y to array P_arr = P_start # add initial value of P to array We should fill in element 0 with the assigned initial values. Then we can create empty arrays to store the calculated values at any time. import numpy as np import matplotlib.pyplot as plt Dt = 0.01 # timestep Delta t Y_start = 20 # initial Y P_start = 20 # initial P t_start = 0 # starttime t_end = 60 # endtime n_steps = int(round((t_end-t_start)/Dt)) # number of timesteps We also need a start and end time to calculate the number of steps we have to calculate. We have to determine a time step for the approximations, the values of Y and P at the beginning. Now that we’ve determined our model it’s time to write a python program to calculate the size of the populations after a given period of time.įirst, we need to import the Numpy and Matplotlib libraries. In the first model both initial populations are set to 20. For every calculation the initial populations should be given. Therefore the growth of one population depends of the number of predator animals of the other species. ![]() Y and P could be two populations of animals in the same area.
0 Comments
Leave a Reply. |