Call a function from within a set of odes

5 次查看(过去 30 天)
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.

更多回答(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.

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by