Generate function handle for odesolver dynamically
古いコメントを表示
Hello,
for a course we should simulate a motorcade where each driver chooses his speed depending on the driver in front of him. The difficulty is that the number of cars should be changed dynamically, which makes it hard to define the function handle for the odesolver.
I already tried some options:
Overwriting the existing equation with the next car attached:
equation = @(t,y) [y(2); 20-y(2)]; %differential eqaution for first car
equation = @(t,y) [equation(t,y); y(4); equationForAcceleration] %adding the second car
This doesn't work because it doesn't copy the first eqaution, just references to it.
Wiritng the equations into a cell and then converting it:
equation{1} = @(t,y) y(2); %speed of first car
equation{2} = @(t,y) 20-y(2); %acceleration of first car
equation{3} = @(t,y) y(4); %speed of second car
equation{4} = @(t,y) equationForAcceleration; %acceleration of second car
eq = cell2mat(equation);
This results in: Error using cat Nonscalar arrays of function handles are not allowed; use cell arrays instead.
I also tried str2func but this only works with scalar strings.
How can I make this work? Except integrating for the first car, using the solution of the first car to integrate the second car and so on and so forth... Or is this the only option anyways?
Thanks
2 件のコメント
Torsten
2023 年 7 月 7 日
Depending on what will it be necessary to change the number of cars under consideration (the number of equations to be solved) ? Will this happen during the integration process ?
Valentin Benke
2023 年 7 月 7 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Ordinary Differential Equations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!