How do I feed ODE45 a cell array ?
Show older comments
I am trying to solve an ODE which consists of function handles. In order to formulate the equation of state vector derivative of the System of 1st order ODEs, I collect these function handles in a cell array. It is not possible to store them in a normal array as far as I know. How do I feed the cell Array, which contains all the function handles to the ODE45 solver or do it differently?
Scheme of the problem:
%create cell and state array
state = @(x,y,z) [x;y;z];
Cell_array = cell(3);
%column selector
select_column = @(M, col_index) M(:,col_index);
%Define Matrix Functions, here 2 examples of Matrices
Matrix_1 = @(state) [1,1,state(1); 1,1,sin(state(2));1,1,state(3)];
Matrix_2 = @(state) [state(1),0,0; 0,state(2),0; 0,0, state(2)];
%select the columns
M1_c1 = @(state) select_column(Matrix_1(state),1);
M1_c2 = @(state) select_column(Matrix_1(state),2);
M1_c3 = @(state) select_column(Matrix_1(state),3);
%differential equations
state_dot_1 = @(state) Matrix_2(state) * M1_c1(state);
state_dot_2 = @(state) Matrix_2(state) * M1_c2(state);
state_dot_3 = @(state) Matrix_2(state) * M1_c3(state);
%fill cell array with differential equations
Cell_array{1} = state_dot_1;
Cell_array{2} = state_dot_2;
Cell_array{3} = state_dot_3;
I would now like to feed that cell_array to an ODE solver. This doesn't work. But using a normal array doesn't work either for these function handles. Does anyone know a work around ? I'm new to matlab and programming in general.
3 Comments
Maybe you could tell us first which ODE system you are trying to solve.
You have 3 unknown state variables, but state_dot_1, state_dot_2 and state_dot_3 together give 9 equations.
Or do you want to call the ODE integrator three times - once for each state_dot_i ?
Lennart Göppinger
on 17 Apr 2024
James Tursa
on 17 Apr 2024
Edited: James Tursa
on 17 Apr 2024
@Lennart Göppinger I'm not sure I completely follow this. Why can't you create a function handle that unwraps the cell array function handles appropriately given the state input to create the derivative output, and feed that to the ODE integrator? Notionally, something like
f = @(t,y) [Cell_array{1}(y);Cell_array{2}(y);Cell_array{3}(y)]
... or whatever variation of this that produces a column vector output.
Accepted Answer
More Answers (0)
Categories
Find more on Numerical Integration and Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!