How to get calculated variables from ode function?

Hi to all
I solve ODE for location and velocity of fluid, using ode23s embeded function. The solution is good. My question is how can I extract variables, which are calculated inside the ode23s? For example, how can I exytract fluid acceleration, a?
Thanks in advance.

回答 (1 件)

Jan
Jan 2022 年 11 月 29 日
編集済み: Jan 2022 年 11 月 30 日

0 投票

The function to be integrated computes these values. You have all required inputs as output of ODE23S already. Use them as inputs to your function. Convert the function such, that it accepts matrices as inputs.
tspan = [0, 10];
y0 = [0, 0];
[T, Y] = ode23s(@fcn, tspan, y0);
Z = fcn(T.', Y.').';
plot(T, Y); hold('on');
plot(T, Z(:, 2), 'co');
function dy = fcn(t, y)
dy = [y(2, :); ...
sin(t)]; % [EDITED, T -> t]
end

3 件のコメント

Alex
Alex 2022 年 11 月 30 日
Hi
thanks for answering, your code is not running, following error is obtained:
Index in position 1 exceeds array bounds. Index must not exceed 1.
Error in untitled>fcn (line 10)
dy = [y(2, :); ...
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode23s (line 121)
= odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in untitled (line 7)
[T, Y] = ode23s(@fcn, tspan, y0);
Bjorn Gustavsson
Bjorn Gustavsson 2022 年 11 月 30 日
Because @Jan gave you an "example differential equation" in fcn, implying (according to my interpretation) that you should modify the system of ODEs you have that you use ode23s to integrate. One point to keep track of here is that you need to take proper care of the orientation of the inputs such that you get a column-array for dy when calling the function at one instance in time with a 1-D array for y, but an [ny x nt] array when you call it with an array of times and a 2-D array for y.
Jan
Jan 2022 年 11 月 30 日
@Alex: I've fixed the typo "T" to "t" and my example does run. So if your code (not mine!) produces an error, post this code. Then the readers can suggest a solution.

サインインしてコメントする。

カテゴリ

タグ

質問済み:

2022 年 11 月 29 日

コメント済み:

Jan
2022 年 11 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by