Call a function from within a set of odes
조회 수: 3 (최근 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?
댓글 수: 0
채택된 답변
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);
추가 답변 (1개)
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 Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!