Parameterize function without anonymous or nested functions
조회 수: 1 (최근 30일)
이전 댓글 표시
I'm looking to parameterize a function for the purpose of passing it to an ODE solver e.g. ode45. I'd like to do this
a=5; b=10;
ydot = @(t,y) myodefn(t,y,a,b)
[t,y] = ode45(ydot, tspan, y0)
where
function ydot = myodefn(t,y,a,b)
ydot = a*y+t*b
However, I'm using this inside of MATLAB Coder and therefore using anonymous functions and nested functions is not an option. Right now I'm hacking around this problem using globals but that's not pretty and I suspect may be causing me performance issues e.g. I have
global a b
a=5; b=10;
[t,y] = ode45(@myodefn_withglobals, tspan, y0)
where
function ydot = myodefn_withglobals(t,y)
global a b
ydot = a*y+t*b
Is there a nicer way to parameterize myodefn other than using globals? Or, is it more likely that my performance issues are coming from elsewhere?
댓글 수: 0
답변 (1개)
Ryan Livingston
2017년 3월 17일
If you're able to upgrade, anonymous function support was added to MATLAB Coder in MATLAB R2016b:
If not, you can use an approach like:
which is similar to yours but uses persistent variables instead of globals.
Using a profiler like VTune, AMD Code Analyst, prof, gprof, or one of the profiling tools in Visual Studio. If you're profiling MEX files, you can pass the -g option to codegen to do a debug build. This will add debug symbols to the MEX file so you have good source information in the profiler. It has the downside of disabling compiler optimization so you'll be profiling code slightly different from your release build.
In MEX, you can also use tic and toc to get rudimentary timing information:
function foo
coder.extrinsic('tic','toc');
...
tic;
expensiveCode(...);
t = toc;
fprintf('Execution time for expensiveCode: %g\n',t);
...
tic;
otherExpensiveCode(...);
t = toc;
fprintf('Execution time for otherExpensiveCode: %g\n',t);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!