下面是我在simulink中S-Function搭建的递推最小二乘法的函数,这样之后就进行不下去了,也找不到解决方法,有没有大佬解答一下,救命!!!
以下代码中感觉有问题的在y的公式和最后J、TL的计算上面!
function [sys, x0, str, ts] = RLS_J_TL_SFunction(t, x, u, flag)
switch flag
case 0 % Initialization
[sys, x0, str, ts] = mdlInitializeSizes;
case 2 % Update discrete states
sys = [];
case 3 % Calculate outputs
sys = mdlOutputs(t, x, u);
case {1, 4, 9} % Unused flags
sys = [];
otherwise
DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));
end
function [sys, x0, str, ts] = mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 2; % Two outputs: J and TL
sizes.NumInputs = 2; % Two inputs: Measured torque and speed
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = []; % No initial states
str = [];
ts = [0 0]; % Set the sample time (0 means continuous)
% Initialize parameters for RLS
global P_past2 theta_past2
P_past2 = 1e4 * eye(3, 3); % Three-dimensional covariance matrix
theta_past2 = [0.0001; 0.0001; 0.0001];
function sys = mdlOutputs(t, x, u)
global P_past2 theta_past2
%初值的确定
lambda = 1; %遗忘因子0-1
Te = u(1);
wm = u(2);
xt = [Te -wm -1];
y = u(2);%theta_past2(1)*Te+theta_past2(2)*(-wm)+theta_past2(3)*(-1);
I = eye(3,3);
K = P_past2*xt'/(lambda + xt * P_past2*xt');
P_new = 1/lambda*(I - K*xt) * P_past2;
theta_new = theta_past2 + K*(y-xt*theta_past2);
P_past2 = P_new ;
theta_past2 = theta_new;
A = theta_new(1);
B = theta_new(2);
C = -theta_new(3);
ts= ;
J = -(1+B)*ts/A*log(-B) ;
TL= C/A;
sys = [J; TL];