Undefined function 'de' for input arguments of type 'double'.

3 views (last 30 days)
Hi,
when I execute the following code, I get the error message: "Undefined function 'de' for input arguments of type 'double'." with the vector "de" is well defined in mdlderivatives of my s-function.
I tried everything and, I still can't solve this problem!
I need your help!!
Thank you.
function [sys,x0,str,ts] = dynamic_model(t,x,u,flag)
switch flag,
case 0,
[sys,x0,str,ts]=mdlInitializeSizes;
case 1,
sys=mdlDerivatives(t,x,u);
case 3,
sys=mdlOutputs(t,x,u);
case { 2, 4, 9 },
sys = [];
otherwise
DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));
end
function [sys,x0,str,ts]=mdlInitializeSizes(~,~,~,~)
sizes = simsizes;
sizes.NumContStates = 5;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 18;
sizes.NumInputs = 22;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = [20,0,0,0,0];
str = [];
ts = [0 0];
function de=mdlDerivatives(~,x,u)
m=1298.9; Iz=1627; lf=1; lr=1.454; br=1.436; bf=br;
M=[m 0 0 0 0;0 m 0 0 0;0 0 Iz 0 0;0 0 0 1 0;0 0 0 0 1]; %matrice d'inertie
C=[0 m*u(22) 0 0 0;-m*u(22) 0 0 0 0;0 0 0 0 0;0 0 0 cos(u(21)) -sin(u(21));0 0 0 sin(u(21)) cos(u(21))];
F=[u(9)*cos(u(1))-u(13)*sin(u(1))+u(10)*cos(u(2))-u(14)*sin(u(2))+u(11)*cos(u(3))-u(15)*sin(u(3))+u(12)*cos(u(4))-u(16)*sin(u(4));u(13)*cos(u(1))+u(9)*sin(u(1))+u(14)*cos(u(2))+u(10)*sin(u(2))+u(15)*cos(u(3))+u(11)*sin(u(3))+u(16)*cos(u(4))+u(12)*sin(u(4));u(9)*(lf*sin(u(1))+0.5*bf*cos(u(1)))+u(10)*(lr*sin(u(2))-0.5*bf*cos(u(2)))+u(11)*(-lr*sin(u(3))+0.5*br*cos(u(3)))+u(12)*(-lr*sin(u(4))-0.5*br*cos(u(4)))+u(13)*(lf*sin(u(1))-0.5*bf*cos(u(1)))+u(14)*(lf*sin(u(2))+0.5*bf*cos(u(2)))+u(15)*(-lr*sin(u(3))-0.5*br*cos(u(3)))+u(16)*(-lr*sin(u(4))+0.5*br*cos(u(4)));0;0];
de=(F-C*x)/M;
function sys=mdlOutputs(~,~,u)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% non-linear dugoff tyre model %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Cz=0.001; %vertical deflection rate of the tyre
Cs=50000;
epsilon=0.015;
I=2.1;
R=0.35;
Ca=30000;
m=1298.9;
lf=1;
lr=1.454;
br=1.436;
bf=br;
mu=0.9;
g=9.81;
cons=m/(lr+lf);
h=0.5;
u1=(de(2)+lf*de(3))/(de(1)+0.5*bf*de(3));
u2=(de(2)+lf*de(3))/(de(1)-0.5*bf*de(3));
u3=(de(2)-lr*de(3))/(de(1)+0.5*br*de(3)); %% les ui représentent les rapports permettant de calculer les angles de dérives de chaque roue%%
u4=(de(2)-lr*de(3))/(de(1)-0.5*br*de(3));
U=[u1 u2 u3 u4];
%%%vertical load/charge verticale:
dde=-C*de/M; %dérivée de de!
Fz1= cons*(0.5*g*lr-0.5*dde(1)*h-lr*h*dde(2)/bf);
Fz2= cons*(0.5*g*lr-0.5*dde(1)*h+lr*h*dde(2)/bf);
Fz3= cons*(0.5*g*lr+0.5*dde(1)*h-lr*h*dde(2)/bf);
Fz4= cons*(0.5*g*lr+0.5*dde(1)*h+lr*h*dde(2)/bf);
Fz=[Fz1 Fz2 Fz3 Fz4];
%%% velocity component in the wheel plane : is the longitunal velocity
v1=(de(1)+0.5*bf*de(3))*cos(u(1))+(de(2)+lf*de(3))*sin(u(1));
v2=(de(1)-0.5*bf*de(3))*cos(u(2))+(de(2)+lf*de(3))*sin(u(2));
v3=(de(1)+0.5*br*de(3))*cos(u(3))+(de(2)-lr*de(3))*sin(u(3));
v4=(de(1)-0.5*br*de(3))*cos(u(4))+(de(2)-lr*de(3))*sin(u(4));
V=[v1 v2 v3 v4];
lamda=zeros(1,4);f=length(lamda);
omega=zeros(1,4);
alph=zeros(1,4);
Re=zeros(1,4);
S=zeros(1,4);
dz=zeros(1,4);
Fs=zeros(1,4);
Ft=zeros(1,4);
for i=1:4
dz(i)=-Cz*Fz(i)+ 0.33*R;
Re(i)=R-dz(i)/3;
omega(i)=(-R*u(i+16)+u(i+4))/I;
S(i)=1+omega(i)*Re(i)/V(i);
alph(i)=atan(U(i))-u(i);
lamda(i)= mu*Fz(i)*(1-S(i))*(1-epsilon*V(i)*sqrt((S(i))^2 + (tan(alph(i)))^2))/(2*sqrt((Cs^2)*(S(i))^2 + (Ca^2)*(tan(alph(i))^2)));
if lamda(i)<1
f(i)=lamda(i)*(2-lamda(i));
Fs(i)=Ca*f(i)*tan(alph(i))/(1-S(i));
Ft(i)=Cs*f(i)*S(i)/(1-S(i));
elseif lamda(i)>1
f(i)=1;
Fs(i)=Ca*f(i)*tan(alph(i))/(1-S(i));
Ft(i)=Cs*f(i)*S(i)/(1-S(i));
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Sorties %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sys(1)=de(1);
sys(2)=de(2);
sys(3)=atan2(de(2),de(1));
sys(4)=de(4);
sys(5)=de(5);
sys(6)=Ft(1);
sys(7)=Ft(2);
sys(8)=Ft(3);
sys(9)=Ft(4);
sys(10)=Fs(1);
sys(11)=Fs(2);
sys(12)=Fs(3);
sys(13)=Fs(4);
sys(14)=de(3);
sys(15)=Fz(1);
sys(16)=Fz(2);
sys(17)=Fz(3);
sys(18)=Fz(4);

Accepted Answer

Stephen23
Stephen23 on 9 Feb 2020
Edited: Stephen23 on 9 Feb 2020
"... the vector "de" is well defined in mdlderivatives of my s-function."
And that is part of the problem: mdlderivatives has a different function workspace to the second function where you try to access that variable:
In MATLAB, variables defined in one function's workspace are not simply visible in all other functions' workspaces, unless you explicitly pass them (e.g. as input/output arguments) or explicitly make their workspace visible to the other functions (e.g. by using nested funtions). Simply defining variables in one function and hoping that they will be visible inside another function will not work.
Not only that, but function workspaces are cleared one they have been run, so even if you do generate the variable de on one call to dynamic_model there is nothing in your code to store it or keep it in memory for the next call when you might want to use it. The obvious solution for that is to use persistent.
However from what I understand after looking over your code, it appears that nested functions would work:
function [...] = dynamic_model(...)
de = []; % Must be defined in the parent function's workspace!
...
function de = mdlDerivatives(~,x,u) % nested function.
...
de = ...; % define DE's value here.
end
function sys = mdlOutputs(...)
...
de % afterwards access DE's value here.
end
...
end % Required at the end of each function.

More Answers (0)

Categories

Find more on Simulink Environment Customization in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!