-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunc.py
82 lines (52 loc) · 1.43 KB
/
func.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import numpy as np
def rungeKutta2():
firstPopulation = 100
growthRate = 0.1
deltaT = 0.5
def GrowthFunc(b):
return growthRate*b
population = firstPopulation
results=[]
deltaP=0.0
# Process
for t in np.arange(0, 10, deltaT):
population += (deltaP*deltaT)
# a = F(tn,pn)
# b = yn+1
# c = F(tn,yn+1)
a = GrowthFunc(population)
b = population+(a*deltaT)
c = GrowthFunc(b)
deltaP = (a+c)/2
results.append((t,population,a,b,c,deltaP))
return results
def euler(firstPopulation,growthRate,MaximumPopulation):
# Input & Initialize
firstPopulation = 100.0
growthRate = 0.1
M = 500.0
deltaT = 0.5
def GrowthFunc(b):
return growthRate*b
population = firstPopulation
results=[]
deltaP=0.0
# Process
for t in np.arange(0, 10, deltaT):
population=population+(deltaP*deltaT)
growth=GrowthFunc(population)
death=growth*(population/M)
deltaP=growth-death
results.append((t,population,growth,death,deltaP))
return results
def exact():
# Input & Initialize
# Euler number
import math
e=math.e
firstPopulation=100
growthRate=0.1
endTime=10
# Process
populationT= firstPopulation * pow(e,growthRate*endTime)
return populationT