Skip to content

Commit be02b64

Browse files
committed
make sure ivp output is floating point
1 parent 1925d5a commit be02b64

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

fncbook/chapter06.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ def euler(du_dt, tspan, u0, n):
1313
a, b = tspan
1414
h = (b - a) / n
1515
t = np.linspace(a, b, n+1)
16-
u = np.tile(np.array(u0), (n+1, 1))
16+
u = np.zeros((n+1, len(np.atleast_1d(u0))))
17+
u[0] = u0
1718
for i in range(n):
1819
u[i+1] = u[i] + h * du_dt(t[i], u[i])
1920

@@ -33,7 +34,8 @@ def ie2(du_dt, tspan, u0, n):
3334
t = np.linspace(a, b, n + 1)
3435

3536
# Initialize output.
36-
u = np.tile(np.array(u0), (n+1, 1))
37+
u = np.zeros((n+1, len(np.atleast_1d(u0))))
38+
u[0] = u0
3739

3840
# Time stepping.
3941
for i in range(n):
@@ -56,8 +58,9 @@ def rk4(du_dt, tspan, u0, n):
5658
t = np.linspace(a, b, n + 1)
5759

5860
# Initialize output.
59-
u = np.tile(np.array(u0), (n+1, 1))
60-
61+
u = np.zeros((n+1, len(np.atleast_1d(u0))))
62+
u[0] = u0
63+
6164
# Time stepping.
6265
for i in range(n):
6366
k1 = h * du_dt(t[i], u[i])
@@ -78,7 +81,7 @@ def rk23(du_dt, tspan, u0, tol):
7881
"""
7982
# Initialize for the first time step.
8083
t = [tspan[0]]
81-
u = np.array([u0])
84+
u = np.array([np.atleast_1d(u0)])
8285
i = 0
8386
h = 0.5 * tol ** (1 / 3)
8487
s1 = du_dt(t[0], u[0])
@@ -132,7 +135,7 @@ def ab4(du_dt, tspan, u0, n):
132135

133136
# Find starting values by RK4.
134137
ts, us = rk4(du_dt, [a, a + (k - 1) * h], u0, k - 1)
135-
u = np.tile(np.array(u0), (n+1, 1))
138+
u = np.zeros((n+1, len(np.atleast_1d(u0))))
136139
u[:k] = us[:k].T
137140

138141
# Compute history of u' values, from newest to oldest.
@@ -158,7 +161,8 @@ def am2(du_dt, tspan, u0, n):
158161
t = np.linspace(a, b, n + 1)
159162

160163
# Initialize output.
161-
u = np.tile(np.array(u0), (n+1, 1))
164+
u = np.zeros((n+1, len(np.atleast_1d(u0))))
165+
u[0] = u0
162166

163167
# Time stepping.
164168
for i in range(n):

0 commit comments

Comments
 (0)