John
John

Reputation: 53

Using scipy solve_ivp for equation with initial conditions

I am having a hard time getting used to scipy's solve_ivp. So let's say we have an ordinary linear differential equation of second order, spring for example (y'' = -k**2*y). Conditions are when the spring is at position 0 (time 0) speed is v0. How can I use initial conditions to solve it?

y'' = -k**2*y  # First this needs to be modified into first order equation

.

def function1(t, y, k):  #original function
return y[1], -k**2*y[1]

function2 = lambda t, y: function1(t, y, k = 10)  #function with only t and y

t = np.linspace(0, 100, 1000)

solution = solve_ivp(function2, (0, 100), (0, 0), t_eval = t)

solution.y[0]

Upvotes: 5

Views: 6485

Answers (1)

Lutz Lehmann
Lutz Lehmann

Reputation: 25992

If you want to encode

y'' = -k**2*y  

as a first-order system, you should use

def function1(t, y, k):  #original function
    return y[1], -k**2*y[0]

The code in the question encodes y'' = -k**2*y'.

Upvotes: 2

Related Questions