필터 지우기
필터 지우기

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

조회 수: 16 (최근 30일)
宇海
宇海 2023년 9월 1일
댓글: Aiswarya 2023년 9월 5일
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
宇海
宇海 2023년 9월 1일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Aiswarya
Aiswarya 2023년 9월 4일
質問には英語でおえしますのでご了承ください
(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

추가 답변 (1개)

宇海
宇海 2023년 9월 4일
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
Aiswarya
Aiswarya 2023년 9월 5일
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 :

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 常微分方程式에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!