lsqnonlin() with Simulink
I am using lsqnonlin() with Simulink as it is used here. I am getting the errors:
Unable to perform assignment because the size of the left side is 30012-by-1 and the size of the right side is 30018-by-1
Error in finitedifferences
Error in computeFinDiffGradAndJac
Here is my code:
function [f] = trackq
% Computes f using lsqnonlin.
mdl = ‘armDynamics’;
open_system(mdl) % Load the model
in = Simulink.SimulationInput(mdl); % Create simulation input object
in = in.setModelParameter(‘StopTime’,’100′); % Stop time 100
f0 = 1; % Initial f
%
options = optimoptions(@lsqnonlin, ‘Algorithm’, ‘levenberg-marquardt’,…
‘Display’,’off’,’StepTolerance’,0.001,’OptimalityTolerance’,0.001);
% Optimize f
set_param(mdl,’FastRestart’,’on’); % Fast restart
% f = lsqnonlin(@compute_error, f0, [], [], options);
f = lsqnonlin(@compute_error, f0, [], [], options);
set_param(mdl,’FastRestart’,’off’);
function error = compute_error(f)
% Set the simulation input object parameters
in = in.setVariable(‘f’, f, ‘Workspace’, mdl);
% Simulate
out = sim(in);
% get q from the actual space arm
armCoords = out.qArm.signals.values;
armCoords = armCoords(2:4);
armCoords = armCoords’;
% get q from modelled space arm
qsim = out.get(‘yout’);
qsim = qsim.getElement(1);
qsim = qsim.Values;
qsim = qsim.Data;
qsim = qsim’;
error = armCoords – qsim;
end
endI am using lsqnonlin() with Simulink as it is used here. I am getting the errors:
Unable to perform assignment because the size of the left side is 30012-by-1 and the size of the right side is 30018-by-1
Error in finitedifferences
Error in computeFinDiffGradAndJac
Here is my code:
function [f] = trackq
% Computes f using lsqnonlin.
mdl = ‘armDynamics’;
open_system(mdl) % Load the model
in = Simulink.SimulationInput(mdl); % Create simulation input object
in = in.setModelParameter(‘StopTime’,’100′); % Stop time 100
f0 = 1; % Initial f
%
options = optimoptions(@lsqnonlin, ‘Algorithm’, ‘levenberg-marquardt’,…
‘Display’,’off’,’StepTolerance’,0.001,’OptimalityTolerance’,0.001);
% Optimize f
set_param(mdl,’FastRestart’,’on’); % Fast restart
% f = lsqnonlin(@compute_error, f0, [], [], options);
f = lsqnonlin(@compute_error, f0, [], [], options);
set_param(mdl,’FastRestart’,’off’);
function error = compute_error(f)
% Set the simulation input object parameters
in = in.setVariable(‘f’, f, ‘Workspace’, mdl);
% Simulate
out = sim(in);
% get q from the actual space arm
armCoords = out.qArm.signals.values;
armCoords = armCoords(2:4);
armCoords = armCoords’;
% get q from modelled space arm
qsim = out.get(‘yout’);
qsim = qsim.getElement(1);
qsim = qsim.Values;
qsim = qsim.Data;
qsim = qsim’;
error = armCoords – qsim;
end
end I am using lsqnonlin() with Simulink as it is used here. I am getting the errors:
Unable to perform assignment because the size of the left side is 30012-by-1 and the size of the right side is 30018-by-1
Error in finitedifferences
Error in computeFinDiffGradAndJac
Here is my code:
function [f] = trackq
% Computes f using lsqnonlin.
mdl = ‘armDynamics’;
open_system(mdl) % Load the model
in = Simulink.SimulationInput(mdl); % Create simulation input object
in = in.setModelParameter(‘StopTime’,’100′); % Stop time 100
f0 = 1; % Initial f
%
options = optimoptions(@lsqnonlin, ‘Algorithm’, ‘levenberg-marquardt’,…
‘Display’,’off’,’StepTolerance’,0.001,’OptimalityTolerance’,0.001);
% Optimize f
set_param(mdl,’FastRestart’,’on’); % Fast restart
% f = lsqnonlin(@compute_error, f0, [], [], options);
f = lsqnonlin(@compute_error, f0, [], [], options);
set_param(mdl,’FastRestart’,’off’);
function error = compute_error(f)
% Set the simulation input object parameters
in = in.setVariable(‘f’, f, ‘Workspace’, mdl);
% Simulate
out = sim(in);
% get q from the actual space arm
armCoords = out.qArm.signals.values;
armCoords = armCoords(2:4);
armCoords = armCoords’;
% get q from modelled space arm
qsim = out.get(‘yout’);
qsim = qsim.getElement(1);
qsim = qsim.Values;
qsim = qsim.Data;
qsim = qsim’;
error = armCoords – qsim;
end
end lsqnonlin MATLAB Answers — New Questions