# how to solve this problem?

10 views (last 30 days)
Federico Belli on 10 May 2024
Commented: Federico Belli on 11 May 2024
When I run the code I get this error
clc;
% System parameters definition
m = 10;
A = [0 1; 0 0];
B = [0; 1/m];
C = [1 0; 0 1];
D = 0;
% State-space model creation
sys = ss(A, B, C, D);
% Number of experiments
N = 1000;
U = cell(N,1);
Y = cell(N,1);
rng(0);
%% Dataset generation for identification
for i = 1:N
% Generating random input sequence (force)
t = linspace(0, 1, 11)';
u = 0.5 * randn(size(t));
% Random initial conditions for velocity and position
x0 = [0.5 * randn(1, 1); 0.5 * randn(1, 1)];
% System simulation
x = lsim(sys, u, t, x0);
% Saving input sequences and state measurements
U{i} = timetable(seconds(t), u);
Y{i} = timetable(seconds(t), x(:,1), x(:,2));
end
%% Generate Data Set for Validation
% Use random initial state and input sequence
t = linspace(0,10,101)';
u = 0.5*randn(size(t));
x0 = [0.5*randn(1,1);0.5*randn(1,1)];
% Obtain state measurements over t
x = lsim(sys,u,t,x0);
% Append the validation experiment (also a timetable) as the last entry in the data set
U{end+1} = timetable(seconds(t), u);
Y{end+1} = timetable(seconds(t), x(:,1), x(:,2));
%% Creation and initialization of the state-space model
sys = idNeuralStateSpace(2, NumInputs=1); % Valid if nx=ny where nx is the number of states and ny is the number of outputs
sys.StateName = {'Position', 'Velocity'}; % State variable names declaration
sys.InputName = 'Force'; % Input name declaration (1 in this case)
sys.InputUnit = 'N';
sys.OutputName = {'Position', 'Velocity'}; % Output names addition
sys.StateUnit = {'m', 'm/s'};
sys.OutputUnit = sys.StateUnit;
sys.Notes = "Prediction Model using Neural Network";
%% Inner layers declaration for the state network
% State network
sys.StateNetwork = createMLPNetwork(sys, 'state', ...
'LayerSizes', 128, ...
'Activations', "tanh", ...
'WeightsInitializer', "glorot", ...
'BiasInitializer', "zeros");
%% Training options declaration for the state network
opt.MaxEpochs = 400;
opt.MiniBatchSize = N;
opt.LearnRate = 0.005;
%% Testing
resultTrain = nlssest(U, Y, sys, opt, 'UseLastExperimentForValidation', true);
The input data must contain 1 columns.

[tU(kexp),vU(kexp),uname,tY(kexp),vY(kexp),yname] = localProcessSeparateData(sys,N,U{kexp},Y{kexp});

[tU, vU, uname, tY, vY, yname] = localProcessCell(U,Y,sys,N,nexp);

[tU,vU,uname,tY,vY,yname] = localProcessSeparateData(sys,N,varargin{:});

Error in nlssest (line 129)
where am I wrong?

Paul on 11 May 2024
Hi Federico,
According to the nlssest, the variable names in the input and output time tables have to macth the InputName and OutputName of nss. I made that change below, and the code gets past the error you saw, but now there's another error that maybe you can sort out (I'm not at all familiar with nlssest).
% System parameters definition
m = 10;
A = [0 1; 0 0];
B = [0; 1/m];
C = [1 0; 0 1];
D = 0;
% State-space model creation
sys = ss(A, B, C, D);
% Number of experiments
N = 1000;
U = cell(N,1);
Y = cell(N,1);
rng(0);
%% Dataset generation for identification
for i = 1:N
% Generating random input sequence (force)
t = linspace(0, 1, 11)';
u = 0.5 * randn(size(t));
% Random initial conditions for velocity and position
x0 = [0.5 * randn(1, 1); 0.5 * randn(1, 1)];
% System simulation
x = lsim(sys, u, t, x0);
Change here. Apparently the the variable name for the time variable is not controlled by 'Variable Names'.
% Saving input sequences and state measurements
U{i} = timetable(seconds(t), u,'VariableNames',{'Force'});
Y{i} = timetable(seconds(t), x(:,1), x(:,2),'VariableNames',{'Position','Velocity'});
end
%% Generate Data Set for Validation
% Use random initial state and input sequence
t = linspace(0,10,101)';
u = 0.5*randn(size(t));
x0 = [0.5*randn(1,1);0.5*randn(1,1)];
% Obtain state measurements over t
x = lsim(sys,u,t,x0);
% Append the validation experiment (also a timetable) as the last entry in the data set
U{end+1} = timetable(seconds(t), u);
Y{end+1} = timetable(seconds(t), x(:,1), x(:,2));
%% Creation and initialization of the state-space model
sys = idNeuralStateSpace(2, NumInputs=1); % Valid if nx=ny where nx is the number of states and ny is the number of outputs
sys.StateName = {'Position', 'Velocity'}; % State variable names declaration
sys.InputName = 'Force'; % Input name declaration (1 in this case)
sys.InputUnit = 'N';
sys.OutputName = {'Position', 'Velocity'}; % Output names addition
sys.StateUnit = {'m', 'm/s'};
sys.OutputUnit = sys.StateUnit;
sys.Notes = "Prediction Model using Neural Network";
%% Inner layers declaration for the state network
% State network
sys.StateNetwork = createMLPNetwork(sys, 'state', ...
'LayerSizes', 128, ...
'Activations', "tanh", ...
'WeightsInitializer', "glorot", ...
'BiasInitializer', "zeros");
%% Training options declaration for the state network
opt.MaxEpochs = 400;
opt.MiniBatchSize = N;
opt.LearnRate = 0.005;
%% Testing
resultTrain = nlssest(U{1}, Y{1}, sys, opt, 'UseLastExperimentForValidation', true);
Error using nlssest (line 190)
You can use the last data experiment for validation only if the number of data experiments is greater than 1.
Federico Belli on 11 May 2024
Hi Paul,
Thank you very much now it goes, removing ( 'UseLastExperimentForValidation', true).

### Categories

Find more on Transfer Function Models in Help Center and File Exchange

R2023b

### Community Treasure Hunt

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

Start Hunting!