Use quadgk with multiple Inputs with Matlab Coder

조회 수: 3 (최근 30일)
Antonia Lichtenegger
Antonia Lichtenegger 2014년 2월 17일
댓글: Tunga Rangaswamy 2019년 10월 16일
Hi I want to create a C file out of my Matlab file. In my Matab file I have to make some integrations, that is way I am using the quadgk function. My problem is now, that the Matlab Coder for generating the C Code doesn't allow function handels.
I want to integrate the function
function val=integrand1(x,c,d) val=x*c+d; end
by the follwing term
Integral = quadgk('integrand1',z(1),z(2),-1,z(2))
So the intergal goes from z(1) to z(2) and for the parameters c and d I want to use the values -1 and z(2). If I do so the Matlab Coder gives the errow ??? First input argument must be a function handle.
But how can I use quadgk instead? Thank you very much for any help.
Best regards Antonia

채택된 답변

Mike Hosea
Mike Hosea 2014년 2월 20일
편집: Mike Hosea 2014년 2월 20일
I don't have a lot of time to type this. Hopefully if it isn't clear I can come back and add some detail.
The way we normally do this in MATLAB is not with a string to represent the integrand. That is obsolete. The standard way would be integrate the anonymous function @(x)integrand1(x,c,d), where c and d would be defined before, e.g.
quadgk(@(x)integrand1(x,-1,z(2)),z(1),z(2))
Unfortunately, this won't work in MATLAB Coder because MATLAB Coder doesn't support anonymous functions. So you will have to do something a little more complicated using persistent variables, something like
function val = integrand(x,c,d)
persistent c_saved,d_saved
if isempty(c_saved)
c_saved = 0;
d_saved = 0;
end
if nargin >= 2
c_saved = c;
end
if nargin >= 3
d_saved = d;
end
val = x*c_saved + d_saved;
Then to use it, you just call it first with your desired c and d.
integrand1(0,-1,z(2));
integral = quadgk(@integrand1,z(1),z(2));
I apologize if I have made a typo or some other boneheaded error above. I don't have time to test it, but we test QUADGK for code generation using this basic approach, so I know it can work. -- Mike
  댓글 수: 5
Steven Lord
Steven Lord 2019년 10월 16일
This isn't really related to the original question. You should ask it as a separate question rather than as a comment to the accepted answer of a question that's more than 5 years old.
Tunga Rangaswamy
Tunga Rangaswamy 2019년 10월 16일
Thanks Steven!
My question is also based on anonymous function and MATLAB coder which seemed aligned to the previous question. However, I have asked this a separate question now and I am looking forward to an answer.

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

추가 답변 (0개)

카테고리

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