how to solve this problem?
5 次查看(过去 30 天)
显示 更早的评论
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 = nssTrainingOptions('adam');
opt.MaxEpochs = 400;
opt.MiniBatchSize = N;
opt.LearnRate = 0.005;
%% Testing
resultTrain = nlssest(U, Y, sys, opt, 'UseLastExperimentForValidation', true);
where am I wrong?
0 个评论
采纳的回答
Paul
2024-5-11
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 = nssTrainingOptions('adam');
opt.MaxEpochs = 400;
opt.MiniBatchSize = N;
opt.LearnRate = 0.005;
%% Testing
resultTrain = nlssest(U{1}, Y{1}, sys, opt, 'UseLastExperimentForValidation', true);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Transfer Function Models 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!