Reputation: 304
I am studying python for what concerns ode numerical integration, in particular I found the scipy.integrate function solve_ipv
. I tried the example shown in the scipy.integrate.solve_ipv page but there could be a mistake in the code related to the Lotka Volterra example:
def lotkavolterra(t, z, a, b, c, d):
x, y = z
return [a*x - b*x*y, -c*y + d*x*y]
sol = solve_ivp(lotkavolterra, [0, 15], [10, 5], args=(1.5, 1, 3, 1))
t = np.linspace(0, 15, 300)
z = sol.sol(t)
import matplotlib.pyplot as plt
plt.plot(t, z.T)
plt.xlabel('t')
plt.legend(['x', 'y'], shadow=True)
plt.title('Lotka-Volterra System')
plt.show()
sol.sol(t)
has no meaning in this code. What should we write? Maybe a tuple z = sol.t, sol.y
?
It is also clear that len(sol.y[0])=57
and len(sol.y[1])=57
while t
has 300 elements. For this reason coupling their values for a plot can be a problem.
In the page there is also a plot of what we would obtain if the code run.
I don't think it's important but I am using python3.
EDIT: I did not insert dense_output=True
in solv_ipv()
Upvotes: 5
Views: 5712
Reputation: 25992
In the solver call
sol = solve_ivp(lotkavolterra, [0, 15], [10, 5], args=(1.5, 1, 3, 1),
dense_output=True)
the last option dense_output=True
is responsible for adding the sol
function to the solution "bunch" object. This function implements the method-specific piecewise polynomial interpolation that in the literature is called "dense output". Thus the next two lines make complete sense, as z = sol.sol(t)
contains a sample for any point in t
.
This option does not change the sequence of internal nodes and step sizes. sol.t
and sol.y
contain the same values with or without that option. There is even no additional computation involved, as the stepper computes this interpolation polynomial for every step. This is used even without the dense output option in the event mechanism. Only the memory use is increased when the individual interpolation polynomials are stored after each step and assembled in the dense output function.
To avoid the confusion of sol.sol
, some have taken res
for "result" or similar as the variable name for the solver return value, so that the interpolation function is accessed as res.sol(t)
.
Upvotes: 8