- Check Array Sizes: Ensure that the armCoords and qsim arrays have the same number of elements before performing the subtraction. You can add debugging statements to print their sizes just before the subtraction operation.
- Consistent Output: The function you pass to lsqnonlin must return an array with a consistent number of elements.
- Simulation Output: Verify that the simulation output out contains the expected data with the correct dimensions.
- Finite Differences: The error in finitedifferences and computeFinDiffGradAndJac indicates that the Jacobian estimation step in lsqnonlin is failing due to inconsistent array sizes.Make sure that the function is vectorized properly and that it returns a vector of residuals.
lsqnonlin() with Simulink
조회 수: 4 (최근 30일)
이전 댓글 표시
- 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
댓글 수: 0
답변 (1개)
arushi
대략 4시간 전
Hi Kukhokuhle,
The error you’re encountering with lsqnonlin might be because there is a mismatch in the size of the arrays being subtracted in the compute_error function. Specifically, the armCoords array and the qsim array do not have the same number of elements when you attempt to compute the error by subtracting one from the other.
Here are a few ways you can try to resolve the issue:
Hope this helps.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Multicore Processor Targets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!