Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
3774 views
ubuntu2004
1
def generator():
2
t = var("t")
3
y,yp,ypp = mi_vars("y","y'","y''")
4
5
def cclinear():
6
# pick a,b for (D-a)(D-b)
7
a = randrange(1,5)*choice([-1,1])
8
b = randrange(5,9)*choice([-1,1])
9
return {
10
"ode": shuffled_equation(ypp,(-a-b)*yp,a*b*y),
11
"form": "linear homogeneous with constant coefficients",
12
"strategy": "using D-notation and factoring",
13
}
14
def folinear():
15
# pick n for y=kx^n
16
n = randrange(2,6)*choice([-1,1])
17
# pick coefficient
18
k = randrange(1,5)*choice([-1,1])
19
# particular solution
20
kp = randrange(1,6)*choice([-1,1])
21
m = n
22
while m==n:
23
m = randrange(1,5)
24
part_sol = kp*t^m
25
ts = n*part_sol-t*part_sol.diff()
26
return {
27
"ode": shuffled_equation(ts,t*yp,-n*y),
28
"form": "linear first-order",
29
"strategy": "solving its homogeneous form and "+\
30
"then using variation of parameters, or using an integrating factor",
31
}
32
def ccdis():
33
# pick a,b for (D+a)(D-a)
34
a = randrange(2,10)
35
# pick d(t-b) or u(t-b)
36
b = randrange(1,8)
37
dis = choice([dirac_delta(t-b),unit_step(t-b)])*choice([-1,1])*randrange(2,8)
38
return {
39
"ode": shuffled_equation(ypp,-a^2*y,dis),
40
"form": "linear constant-coefficient with a discontinuous function",
41
"strategy": "using Laplace transforms",
42
}
43
def exact():
44
terms = [
45
randrange(1,6)*choice([-1,1])*t^randrange(2,5),
46
randrange(1,6)*choice([-1,1])*y^randrange(2,5),
47
]
48
extra_terms = [
49
randrange(1,6)*choice([-1,1])*t*y,
50
randrange(1,6)*choice([-1,1])*t*y^2,
51
randrange(1,6)*choice([-1,1])*t^2*y,
52
randrange(1,6)*choice([-1,1])*t^2*y^2
53
]
54
terms.append(choice(extra_terms))
55
# pick initial values
56
ode = shuffled_equation(
57
terms[0].diff(t),
58
terms[1].diff(t),
59
terms[2].diff(t),
60
terms[0].diff(y)*yp,
61
terms[1].diff(y)*yp,
62
terms[2].diff(y)*yp,
63
)
64
return {
65
"ode": ode,
66
"form": "exact",
67
"strategy": "finding a potential function",
68
}
69
70
odes = [cclinear(),folinear(),ccdis(),exact()]
71
shuffle(odes)
72
return {
73
"odes": odes,
74
}
75