MATLAB: Solving ode45 with mutliple classes

cell arraysode45

I have many cells A and B, and the dimension that should pass the ode45 function is 2by2 for both of them. I want to apply the ode for every class. I have around 58 classes, and they each structure 2by2 matrix.
A= cellfun(@(n,m) -inv(n)*m, L,R, 'UniformOutput',false);
B= cellfun(@(n) inv(n), L, 'UniformOutput',false);
Vp = sqrt(2)*120;
n =60;
h=0.000167; % step size
t = 0:h:0.1; % the range of t
x = zeros(2,length(t)); % Calculates upto x(360)
K = [1;0];
u = K*Vp;
%this is State Space Model (Euler's Method)
dx = arrayfun(@(t,x) cellfun(@(n,m) n*x+ m*u*sin(377*t+pi/2),A,B),'UniformOutput',false);
for i = 1:(length(t)-1)
t(i+1) = t(i)+h;
x(:,i+1) = x(:,i)+ h*dx(t(i),x(:,i));
end
%for ODE45
tspan = [0 0.167];
iniCon = zeros(2,11);
[t,x] = ode45(@myode, tspan, iniCon);
function dx = myode(t,x)
A= cellfun(@(n,m) -inv(n)*m, L,R, 'UniformOutput',false);
B= cellfun(@(n) inv(n), L, 'UniformOutput',false);
Vp = 120*sqrt(2);
K = [Vp;0];
u = K*sin(377.*t+(pi/2));
dx = cellfun(@(n,m) n.*x+ m.*u*sin(377*t+pi/2),A,B,'UniformOutput',false);
end

Best Answer

You are trying to return a cell from your odefun. ode45 requires that you return a numeric column vector with the same number of elements as the initial conditions has.
If you want to run multiple ode at the same time, you can pass in an arbitrary size numeric array as the initial conditions if that helps you think about the data. The odefun will receive it as a column vector no matter the original shape. Inside the odefun you can immediately reshape as needed. You can num2cell to build cell arrays. You can cellfun to process them. But at the end you need turn the results back into a column vector.
Also you need to pass A and B into your function by using @(t, x) odefun(t, x, A, B)