fsolve usage in Simulink fails due to P-code error

1 次查看(过去 30 天)
Hi,
I would to solve an expression numerically using fsolve in Simulink however I get a P-code function error which I cannot understand.
I am trying to solve the following function in a matlab function block in simulink which works when used in a Matlab script:
function [lam_the] = slip_mag(theta)
% Magic formula
% Unpack
B0 = theta(1,1);
C0 = theta(1,2);
D0 = theta(1,3);
E0 = theta(1,4);
mu0 = theta(1,5);
% Solve
F = @(sig) (D0 * sin(C0 * atan(B0 * sig - E0 * (B0 * sig - atan(B0 * sig)))) - mu0);
opt = optimoptions('fsolve','Algorithm',"levenberg-marquardt");
lam_the = fsolve(F,0.1,opt);
end
Which results in the following error:
Simulink does not have enough information to determine output sizes for this block. If you think the errors below are inaccurate, try specifying types for the block inputs and/or sizes for the block outputs.
Component: MATLAB Function | Category: Coder error
Index expression out of bounds. Attempted to access element 1. The valid range is 1-0.
More information
P-code function 'driver.p' produced an error.
Component: MATLAB Function | Category: Coder error
Function call failed.
P-code function 'fsolve.p' produced an error.
Component: MATLAB Function | Category: Coder error
Function call failed.
Function 'Particle Filter1' (#24.3081.3098), line 95, column 15:
"fsolve(F,0.1,opt)"
Launch diagnostic report.
Component: MATLAB Function | Category: Coder error
How can I correctly use fsolve in Simulink?
Thank you in advance.

回答(1 个)

Christian
Christian 2024-1-9
编辑:Christian 2024-1-9
Initialize lam_the=0; in the first line of the function.
Matlab just fails to provide a proper Error.

类别

Help CenterFile Exchange 中查找有关 Manual Performance Optimization 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by