필터 지우기
필터 지우기

Call a function from within a set of odes

조회 수: 2 (최근 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.

카테고리

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