why does dimension of input variable change in ode45

1 view (last 30 days)
Elysi Cochin
Elysi Cochin on 22 Apr 2021
Edited: Elysi Cochin on 23 Apr 2021
i wanted to compute ode45 with the following input
ABCD = [3453151,3880795,334228,236989;3527618,3939199,341859,245399;...
y0 = ABCD;
[t,y] = ode45(@(t,y) first_order(t,y), tspan, y0);
when i check the value of y in the function first_order its dimension is a column vector of 40x1
but i pass it as a matrix of 10x4
why does that happen
is there mistake in the ode45 line i have written
why does the dimension change

Accepted Answer

James Tursa
James Tursa on 22 Apr 2021
Edited: James Tursa on 22 Apr 2021
The dimension changes to a column vector because that is the way ode45( ) operates. Your derivative function needs to be able to accept a column state vector y, which you may want to reshape back into a matrix prior to using it. It also needs to return a column vector. E.g.,
function dydt = first_order(t,y,N)
y = reshape(y,10,4); % reshape input as matrix
dydt = dydt(:); % reshape output as column vector
Btw, you are missing the N in your function handle. E.g.,
[t,y] = ode45(@(t,y) first_order(t,y), tspan, y0);
should be
[t,y] = ode45(@(t,y) first_order(t,y,N), tspan, y0);

More Answers (0)


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by