Call a function from within a set of odes

7 ビュー (過去 30 日間)
Sylvia
Sylvia 2015 年 9 月 4 日
コメント済み: Star Strider 2015 年 9 月 7 日
Hi, I am wondering if there is a way to use functions within a set of odes. I have a set of odes for pressure, temperature, and some other variables within the atmosphere. Within the set of odes, there are a lot of variables which I am representing with experimental correlations, e.g. heat of fusion as a function of temperature with five or six parameters. What I would like to do is write these experimental correlations as separation functions, e.g. function Lw = heatofVap(T) ..., then call these functions from within the odes, e.g. dy(5) = a*y(4) + b*y(3)*y(4) + @heatofVap(y(1))*y(3), so that it is more readable. Is this possible? Could someone give me a syntax example?

採用された回答

Star Strider
Star Strider 2015 年 9 月 4 日
編集済み: Star Strider 2015 年 9 月 4 日
From my experience with the ODE solvers, I don’t see any reason that you could not do what you want. However you don’t need the ‘@’ sign if you’re calling an external function and are not passing the function as an argument to another function. Just call it as you would any other function (for instance sin(y(1))):
dy(5) = a*y(4) + b*y(3)*y(4) + heatofVap(y(1))*y(3);
You’ve likely provided as good an example of using it as I could come up with!
EDIT — It is always good practise to vectorise your code, to be certain you are doing element-wise operations rather than matrix operations (unless you specifically intend to do matrix operations). I would change the ‘dy(5)’ and other assignments to use element-wise operations:
dy(5) = a.*y(4) + b.*y(3).*y(4) + heatofVap(y(1)).*y(3);
Note the dot (.) operator. See Array vs. Matrix Operations for details.
  2 件のコメント
Sylvia
Sylvia 2015 年 9 月 7 日
Thanks! That was quite simple.
Star Strider
Star Strider 2015 年 9 月 7 日
My pleasure!

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2015 年 9 月 4 日
Example 3 on the documentation page for ODE45 is somewhat similar to what you're trying to do. It uses INTERP1 to interpolate some sample data needed to compute the right-hand side of the ODE. In your case, instead of calling INTERP1 you'd be calling heatofVap.
  1 件のコメント
Sylvia
Sylvia 2015 年 9 月 7 日
Thank you!

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

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by