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 Kommentare

Torsten
Torsten am 17 Apr. 2024
Bearbeitet: Torsten am 17 Apr. 2024
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
Lennart Göppinger am 17 Apr. 2024
First of all thank you for your quick reply.
You're right, that scheme is wrong. It should have been the rows of Matrix_2.
So for state_dot_1 it is the first row of matrix_2 multiplied by the column vector, for state_dot_2 it is the 2nd row of Matrix_2 multiplied by the column vector and so on. This would then give us 3 equations for 3 states with the Cell_array containing these 3 equations. How do I feed that Cell_array to the ODE solver ?
Sorry for the confusion.
James Tursa
James Tursa am 17 Apr. 2024
Bearbeitet: James Tursa am 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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Torsten
Torsten am 17 Apr. 2024
Verschoben: Torsten am 17 Apr. 2024

0 Stimmen

Use state_dot as the function that you pass to the integrator.
I followed your coding - the problem setup could have been much easier.
%column selector
select_column = @(M, col_index) M(:,col_index);
select_row = @(M,row_index)M(row_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);
%select the rows
M2_r1 = @(state) select_row(Matrix_2(state),1);
M2_r2 = @(state) select_row(Matrix_2(state),2);
M2_r3 = @(state) select_row(Matrix_2(state),3);
%differential equations
state_dot_1 = @(state) M2_r1(state) * M1_c1(state);
state_dot_2 = @(state) M2_r2(state) * M1_c2(state);
state_dot_3 = @(state) M2_r3(state) * M1_c3(state);
state_dot = @(~,state)[state_dot_1(state);state_dot_2(state);state_dot_3(state)];
state_dot(0,[1 2 3])
ans = 3x1
1 2 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Weitere Antworten (0)

Kategorien

Mehr zu Numerical Integration and Differential Equations finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by