I am learning computer simulation and modeling in Python and jupyter. The initial population is 100. I need to find population until the time is 100 using time step 8. So, i put initial population as p0 = 100. The timestep should be 8.
i have imported
from pylab import *
%matplotlib inline
import math
then i wrote a function for finding population using Euler method
def eulerPop():
t = 0
p0 = 100
p = p0
r = 0.1
dt = 8
t_array = [0]
pop_array = [p]
while t < 100:
p += (r * p * dt)
t += dt
#print (p, t)
t_array.append(t)
pop_array.append(p)
return pop_array,t_array
then i printed the value
print(eulerPop())
it resulted right answer which are as follows
([100, 180.0, 324.0, 583.2, 1049.7600000000002, 1889.5680000000004, 3401.2224000000006, 6122.200320000002, 11019.960576000003, 19835.929036800007, 35704.67226624001, 64268.410079232024, 115683.13814261765, 208229.6486567118], [0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104])
then I wrote another function to find the exact population
def exactPop():
p0 = 100
t = 0
dt = 8
r = 0.1
p = p0
t_array = [0]
pop_array = [p]
while t < 100:
p = (p * e ** (0.1 * 8))
t += dt
#print (p, t)
t_array.append(t)
pop_array.append(p)
return pop_array,t_array
then i printed the outcomes (i got as it should be)
([100, 222.55409284924673, 495.3032424395114, 1102.3176380641598, 2453.253019710934, 5459.815003314422, 12151.041751873483, 27042.64074261525, 60184.503787208174, 133943.0764394417, 298095.79870417266, 663424.400627788, 1476478.156557726, 3285962.567444328], [0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104])
from these function we came to know that p1 = 180 using Euler method but the exact population is 222.55409284924673
hence the difference should give Absolute Error because Absolute Error = Exact - population from Euler Method
However i am not able to create a function for Absolute Error
what is wrong in the following code and how can i fix it?
def absEulerError():
t = 0
p0 = 100
p = p0
r = 0.1
dt = 8
t_array = [0]
pop_array = [p]
ep = p0
while t < 100:
p += ((ep * e ** (0.1 * 8)) - (r * p * dt))
t += dt
#print (p, t)
t_array.append(t)
pop_array.append(p)
return pop_array,t_array
Thank You