Using fsolve in MATLAB FUNCTION BLOCK in Simulink

20 次查看(过去 30 天)
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 个评论
gdz
gdz 2023-2-1
编辑:gdz 2023-2-1
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.

请先登录,再进行评论。

采纳的回答

Kartik
Kartik 2023-4-17
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:

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Subsystems 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by