Use quadgk with multiple Inputs with Matlab Coder

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

 Accepted Answer

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 Comments

Thank you very much this perfectly works. Allthough I have another question concerning Matlab Coder. Is there a possibility to use the 'fsolve' function with Matlab Coder? Or do I have to write my own solver for a system of nonlinear equations? Thank you very much for any help. Antonia
If your goal is to generate C code to deploy to another target (one without MATLAB), then you will have to write your own solver.
If you are generating a mex file or running within Simulink (from a MATLAB Function Block), then you have the option of calling fsolve "extrinsically". However, there is a difficulty. Since you can't pass function handles back and forth into and out of a MATLAB Coder generated mex file, you can't just declare fsolve extrinsic. Instead you must write your own MATLAB function, one that will NOT be compiled, that accepts only data (no function handles), creates any anonymous functions needed, calls MATLAB's fsolve function, and returns the data. You then declare the function you wrote as the extrinsic function. But again, the application of this is narrowly focused to when you are trying to use MATLAB Coder as an acceleration tool for MATLAB, not when you are using MATLAB Coder to generate C code that you plan to deploy on some other target.
Hi Mike,
Thanks for your answer on using anonymous functions for Matlab coder. It is very helpful.
I am following the same technique for bisection method. Let's say the function equation has an equation to be solved using bisection method. I have modified the function example using persistent variables. I would expect the code to evaluate the equation with values starting from a (first approx.) and b (second approx.) and intermediate values determined by the bisection function to find the zero crossing or the root. Instead the code throws an error Undefined function or variable 'x'.
NOTE: Expected answer = - 5
Can you please help me with this?
%**********************main script**************************%
% with function handle
fun = @(x) equation(x,8,15);
root = bisection(fun,-10,-4,1e-03);
% without using funtion handle which gives the error
% equation(x,8,15);
% root = bisection(@equation,-10,-4,1e-03);
%**********************equation**************************%
function fun = equation(x,a,b)
persistent a_val
persistent b_val
if isempty(a_val)
a_val = 0;
b_val = 0;
end
if nargin >= 2
a_val = a;
end
if nargin >= 3
b_val = b;
end
fun = x^2 + a_val*x + b_val;
end
%*******************bisection method*********************%
function y = bisection(f,a,b,tol)
y = (a + b)/2;
err = abs(f(y));
while err > tol
if f(a)*f(y)<0
b = y;
else
a = y;
end
y = (a + b)/2;
err = abs(f(y));
end
end
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.
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.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!