How to use quadgk with Matlab coder?

조회 수: 1 (최근 30일)
Bjørn
Bjørn 2013년 10월 8일
댓글: Bjørn 2013년 10월 11일
According to the list here: http://www.mathworks.se/help/simulink/ug/functions-supported-for-code-generation--alphabetical-list.html, quadgk is supported for code generation. I have tried exporting the following code:
%#codegen
function a = bk_testfunc(b,c)
f = @(x) exp(-x.^2).*log(x).^2;
a = quadgk(f,b,c);
but I get the error message "This kind of expression is not supported." for the @(x) part.
Quadgk does not seem to accecpt inserting a function as the first argument without the @ symbol, like
a = quadgk(exp(x),b,c);
or
a = quadgk(exp(),b,c);
I cannot find any documentation on how to use quadgk with the Coder, which is particularly frustrating since the Coder does not seem to accept the normal (@) way of calling quadgk, and quadgk does not accept to be called without the @ symbol identifying the function to be integrated.

채택된 답변

Fred Smith
Fred Smith 2013년 10월 9일
Code generation does not support
  • Anonymous function @(x)x+2
  • Nested functions (functions with end before the end of the preceding function).
Code generation does support plain-ordinary function handles: @f
So for your example what you need is this:
function a = bk_testfunc(b,c)
a = quadgk(@f_testfunc,b,c);
end %optional end.
function fx = f_testfunc(x)
fx = exp(-x.^2).*log(x).^2;
end %optional end, needed only if you keep the preceding one.
  댓글 수: 1
Bjørn
Bjørn 2013년 10월 11일
Thank you, and thanks to everyone else who answered my question. Now it works.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2013년 10월 8일
function fx = f_testfunc(x)
fx = exp(-x.^2).*log(x).^2;
end
now in your quadgk call, try
%#function f_testfunc
a = quadgk('f_testfunc', b, c);
the %# line is likely necessary.
  댓글 수: 1
Ryan Livingston
Ryan Livingston 2013년 10월 9일
The pragma:
%#function ...
is only relevant to MATLAB Compiler rather than MATLAB Coder. It provides a hint to MATLAB Compiler that a function is required to be packaged up.

댓글을 달려면 로그인하십시오.


Bjørn
Bjørn 2013년 10월 8일
Thanks, but I still have problems. I have done the following: first file:
%#codegen
function a = bk_testfunc(b,c)
%#function f_testfunc
a = quadgk('f_testfunc',b,c);
second file:
%#codegen
function fx = f_testfunc(x)
fx = exp(-x.^2).*log(x).^2;
end
The Coder gives the following error message: "First input argument must be a function handle."
I have also tried putting both functions in the same file:
%#codegen
function a = bk_testfunc(b,c)
%#function f_testfunc
a = quadgk('f_testfunc',b,c);
function fx = f_testfunc(x)
fx = exp(-x.^2).*log(x).^2;
end
but this gives the following error: "Parse error: File: bk_testfunc.m Line: 9 Column: 1 This statement is incomplete." (line 9 is the line with the end statement).
An end statement after the first function gives the same error message as having the functions in separate files. end with semicolon after the first function gives the error "Parameter list for function "bk_testfunc" cannot be determined due to a syntax error in the function". So while I'm a little further, I still can't generate the code.
  댓글 수: 2
Ketan
Ketan 2013년 10월 9일
Hi Bjørn,
Have you tried passing a function handle for f_testfunc to quadgk using @ style syntax? IE
a = quadgk(@f_testfunc,b,c)
where 'f_testfunc' is either defined as a subfunction of bk_testfunc or defined in another file. MATLAB coder does not support anonymous functions, but it does support function handles.
Walter Roberson
Walter Roberson 2013년 10월 9일
If you put multiple functions in the same file, they they all have to have 'end' matching their "function" statement (as in your second function), or else none of them can have that matching "end". So add an "end" after your "a = " statement.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by