필터 지우기
필터 지우기

Code generation for event functions and ODE solvers

조회 수: 6 (최근 30일)
Daniel Lynch
Daniel Lynch 2022년 9월 2일
답변: David Fink 2022년 9월 6일
Does MATLAB support code generation for event functions used with ODE solvers like ode45?
From experimenting with MATLAB Coder and from looking at the Extended Capabilities section of the documentation on odeset, I'm inclined to think event functions are currently unsupported, but I'm not positive, because this is my first experience using MATLAB Coder.
When checking my entry-point program (a simulation of a hybrid mechanical system, written as a finite-state machine) for issues with MATLAB Coder, an error arises from the following line of code:
options = odeset('Events', @(t,x) terminal_events(t,x,params));
where params is a struct passed to the entry-point function. The error reported by MATLAB Coder for this line is "All inputs must be constant."
Thank you for any clarification you can provide.

채택된 답변

David Fink
David Fink 2022년 9월 6일
All odeset inputs must be constant, but the anonymous function (which stores the value of params when the anonymous function is constructed) is not.
You can work around this limitation by storing params somewhere other than the anonymous function. For example, a global or persistent variable.
Here's an example of how you could use a global:
function out = xEntryPoint(params)
global g
g = params;
f = @myFun;
coder.const(f); % verifies that f is constant
out = f();
end
function out = myFun
global g
out = g;
end
Generate code and verify it works via:
global g;
g = struct('f', 0);
codegen xEntryPoint -args g
s.f = 4;
xEntryPoint_mex(s)

추가 답변 (1개)

Guru Kumaresan
Guru Kumaresan 2022년 9월 6일
Hello,
It is possible to generate code for event functions but it is necessary that all inputs to the “odesetinputs must be constant.

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by