ode45を使用して毎時間微分方程式を解かせたい

26 views (last 30 days)
宇海
宇海 on 1 Sep 2023
Commented: Aiswarya on 5 Sep 2023
2次のODEをシミュレーション中に解きたいと考えています. 自分で離散化すると, 自分の技術知識不足によりかなり不安定な結果になってしまったのでMATLABのode45を使って解きたいと考えています. イメージとしては次のようなコードです.
このコードのcal_aの中でode45を使おうとしたのですが, 「関数または変数が認識されません。」というエラーになりました.
やり方の検討もつかないので教えていただきたいです.
function main
%初期値の設定
%%%%
result_b = cal_b();
while Time(i) < Time_finish
%次の時刻でのAの計算(Bの結果が必要)
[result_a]= cal_a(result_b);
%次の時刻でのBの計算
[result_b] = cal_b();
%次の時刻でのCの計算(A, Bの結果が必要)
pressure_Bubble_next = Bubble_pressure(result_a, result_b);
%値の更新
i = i+1;
end
  1 Comment
宇海
宇海 on 1 Sep 2023
cal_aの中身は次のような感じです.
function [result_a]= Rayleigh_Plesset_eq(result_b)
tspan = time_now:dt/2:time_now+de; %変数は定義済み
dydt = [y(2); f(y(1),y(2),t)]; %f(y(1),y(2),t)はy(1)とy(2)の何かしらの関数が書いてあるという意味, 時刻によって変化するパラメータがある.
[t,y] = ode45(@dydt,tspan,[R; RV]); %R,RVは時刻tでのy(1),y(2)の値
%結果として時刻t+dtでの値が欲しい.
radius_velocity = y(3,2);
radius = y(3,1);
end

Sign in to comment.

Accepted Answer

Aiswarya
Aiswarya on 4 Sep 2023
質問には英語でおえしますのでご了承ください
(Please note that I will be answering the question in English. )
Hi,
I understand that you are trying to solve second-order ODE and would like to use the MATLAB function ode45 for the same. But you are getting an error "Function or variable not recognized." in your code.
In the cal_a function, you created a variable dydt which you are trying to pass as a function handle to ode45. Instead, you should define a function which gives output as dydt and pass the function handle as input, the first input argument of ode45 is a function handle, not a variable.
Refer to this following documentation which has an example to pass the first argument to the function ode45:
For your case, create a function as follows:
function dydt = odefun(t,y,f)
dydt = [y(2); f(y(1),y(2),t)];
end
Then pass the handle as input to ode45:
[t,y] = ode45(@odefun,tspan,[R; RV]);
I hope this helps.
Regards,
Aiswarya

More Answers (1)

宇海
宇海 on 4 Sep 2023
Thank you for your reply !
(I am not a native English speaker so there may be some odd expressions. If you don't understand, please point it out.)
Just to confirm, tdoes he f in dydt = [y(2); f(y(1),y(2),t)]; means another function which I want to solve ?
f is the other end of a second-order differential equation, right?
Am I right in understanding that when you create dydt with a function called odefun, you call the function with another f?
By your help, I seem possible to solve this.
Best Regards,
  1 Comment
Aiswarya
Aiswarya on 5 Sep 2023
Yes, the function odefun is a function handle to which you are passing the values of t and y0. It's better to give f directly as expression in the dydt expression. The inputs which can be given to the odefun are timespan, y0 (initial conditions) and some other options, which you can find in the documentation :

Sign in to comment.

Categories

Find more on 常微分方程式 in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!