Problem with solve_ivp in parameter space

  matlab, ode, python, scipy

I am having an issue creating an example ODE given in Matlab to work with scipy’s solve_ivp. In Matlab, I’m defining a function

function dx = f(~,x,A)
dx = A*[x(1); x(2)];
end

and do (the ode in a loop of course):

A = [0 1;-1 0];
dx = A(1,1)*x + A(1,2)*y;
dy = A(2,1)*x + A(2,2)*y;
tspan=[0 100];
ic = [x0 0];
[~,x] = ode45(@(t,x) f(t,x,A),tspan,ic);

to calculate the solution which looks like this
enter image description here

, however if I define my functions in python like this

f = lambda t,x1,x2, A : np.dot(A,[[x1],[x2]])
ic = [x0,0]
solution = solve_ivp(f, [xmin, xmax], ic, method='RK45', t_eval=yval, dense_output=False, args=(A))

I get errors like

shapes (2,) and (2,1,2) not aligned: 2 (dim 0) != 1 (dim 1)

or similar, depending on what I already tried to rewrite f to. As I understand, solve_ivp expects a single value in the x0 array, while I return a 2×1 vector. It also doesn’t accept a vector as value in it’s x0 array like [[x0,0]]

Now I wonder if scipy.solve_ivp able to do the calculation like ode45 for parameter space (and how do I do it) or do I have to do the calculation otherwise?

(I checked already, that all other matrices and return values are identical with the matlab calculation.)

Source: Python Questions

LEAVE A COMMENT