Using fsolve in MATLAB FUNCTION BLOCK in Simulink

34 views (last 30 days)
Hi,
I am using a S-function builder to read the data from my arduino. The buf0 gives me the reading of the sensor. Then, I would like to send the buf0 as u to the MATLAB FUNCTION BLOCK, and the output is y. However, the output of y gives me 0 all the time, which is not changing with the input u.
function y = fcn(u)
F=@(x)[10*exp(-3*10^(-4)*x)+23*exp(-6*10^(-6)*x)-u];
y = fsolve(F,0,optimoptions('fsolve','Algorithm','levenberg-marquardt'));
I have tried to use MATLAB FUNCTION BLOCK with y = u and it works. So, i believe the problem is with the fsolve function.
Therefore, I would like to ask how to use fsolve in MATLAB FUNCTION BLOCK in Simulink.
Thank you.
  1 Comment
gdz
gdz on 1 Feb 2023
Edited: gdz on 1 Feb 2023
I have verified the MATLAB Function block once again. I discovered that the function works under "Simulation" tab, and it will not work under "Hardware" tab.
Therefore, are there any way allow my function to work under "Hardware" tab?
Thank you.

Sign in to comment.

Accepted Answer

Kartik
Kartik on 17 Apr 2023
Hi,
I understand that you have been trying to use the “fsolve” function with specific arguments (solving algorithm) in the Hardware Tab.
To make sure that your function works under the "Hardware" tab, you need to check if the “fsolve” function is supported for code generation. You can do this by running the following command in the MATLAB Command Window:
cfg = coder.config('lib');
cfg.TargetLang = 'C';
checkCodegenCompatibility(F, cfg);
This command generates C code for the 'F' function using the default code generation configuration ('lib'), which generates a static library. If the “fsolve” function is supported for code generation, the command should run without errors.
If the “fsolve” function is not supported for code generation, the command will return an error message indicating which part of the function is not supported.
If the “fsolve” function is not supported for code generation, you can try using a different solver that is supported, such as “fsolve” with the “trust-region-dogleg” algorithm or “fmincon”.
For more information on using the fsolve function in Simulink, see the following documentation:

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!