Error using lsqcurvefit: Function value and YDATA sizes are not equal.
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I am trying to use lsqcurvefit where size(xfit) = 71750 20 and size(yfit) = 71750 1 with the code appended but I recieve an error:
Error using lsqcurvefit (line 269)
Function value and YDATA sizes are not equal.
Error in Untitled (line 22)
constants = lsqcurvefit(f,x0,xfit,yfit);
Can someone help? I am guessing its the parametrization of my equation f but I am not sure where to start.
Thank you and best wishes
disp('Start fitting data')
tic
%declare fitting function
f = @(constants, x)-x(18).*2.*pi.*x(1).*constants(1).*constants(2).*(...
(1./2.*1./(2.*pi).*constants(3).*(...
x(10).*(exp(2.*constants(4).*x(18)./(x(19).*x(20).*x(2)))+exp(2.*constants(4).*x(18)./(x(19).*x(20).*x(1))))+...
x(11).*(exp(2.*constants(4).*x(18)./(x(19).*x(20).*x(3)))+exp(2.*constants(4).*x(18)./(x(19).*x(20).*x(1))))+...
x(12).*(exp(2.*constants(4).*x(18)./(x(19).*x(20).*x(4)))+exp(2.*constants(4).*x(18)./(x(19).*x(20).*x(1))))+...
x(13).*(exp(2.*constants(4).*x(18)./(x(19).*x(20).*x(5)))+exp(2.*constants(4).*x(18)./(x(19).*x(20).*x(1))))+...
x(14).*(exp(2.*constants(4).*x(18)./(x(19).*x(20).*x(6)))+exp(2.*constants(4).*x(18)./(x(19).*x(20).*x(1))))+...
x(15).*(exp(2.*constants(4).*x(18)./(x(19).*x(20).*x(7)))+exp(2.*constants(4).*x(18)./(x(19).*x(20).*x(1))))+...
x(16).*(exp(2.*constants(4).*x(18)./(x(19).*x(20).*x(8)))+exp(2.*constants(4).*x(18)./(x(19).*x(20).*x(1))))+...
x(17).*(exp(2.*constants(4).*x(18)./(x(19).*x(20).*x(9)))+exp(2.*constants(4).*x(18)./(x(19).*x(20).*x(1))))))...
-constants(3).*exp(2.*constants(4).*x(18)./(x(19).*x(20).*x(1))));
%decelare starting values
x0 = [1, 1, 1, 2];
xfit = rho_local_paramms(:,2:end);
yfit = rho_local_paramms(:,1);
constants = lsqcurvefit(f,x0,xfit,yfit);
disp('Fitting Completed')
toc
Respuestas (1)
Vaibhav
el 12 de Abr. de 2024
Hi Hannes
The issue with "lsqcurvefit" is that the output of function "f" doesn't match the size of the "yfit" data, which is a vector with dimensions (71750, 1). This typically happens when the function you are using to fit the data does not return a vector of the same length as the input "y" data you are trying to model.
To resolve this, you can try modifying function "f" so that it can process all rows of "xfit" and return a column vector of the computed values. This often involves iterating over rows of "xfit" or using vectorized operations that can handle the entire dataset at once.
Here is a conceptual approach to modifying your function to handle the entire "xfit" dataset:
disp('Start fitting data')
tic
% Modify the fitting function to handle multiple rows of xfit
f = @(constants, x) arrayfun(@(idx) -x(idx, 18).*2.*pi.*x(idx, 1).*constants(1).*constants(2).*(...
(1./2.*1./(2.*pi).*constants(3).*(...
x(idx, 10).*(exp(2.*constants(4).*x(idx, 18)./(x(idx, 19).*x(idx, 20).*x(idx, 2)))+exp(2.*constants(4).*x(idx, 18)./(x(idx, 19).*x(idx, 20).*x(idx, 1))))+...
x(idx, 11).*(exp(2.*constants(4).*x(idx, 18)./(x(idx, 19).*x(idx, 20).*x(idx, 3)))+exp(2.*constants(4).*x(idx, 18)./(x(idx, 19).*x(idx, 20).*x(idx, 1))))+...
x(idx, 12).*(exp(2.*constants(4).*x(idx, 18)./(x(idx, 19).*x(idx, 20).*x(idx, 4)))+exp(2.*constants(4).*x(idx, 18)./(x(idx, 19).*x(idx, 20).*x(idx, 1))))+...
x(idx, 13).*(exp(2.*constants(4).*x(idx, 18)./(x(idx, 19).*x(idx, 20).*x(idx, 5)))+exp(2.*constants(4).*x(idx, 18)./(x(idx, 19).*x(idx, 20).*x(idx, 1))))+...
x(idx, 14).*(exp(2.*constants(4).*x(idx, 18)./(x(idx, 19).*x(idx, 20).*x(idx, 6)))+exp(2.*constants(4).*x(idx, 18)./(x(idx, 19).*x(idx, 20).*x(idx, 1))))+...
x(idx, 15).*(exp(2.*constants(4).*x(idx, 18)./(x(idx, 19).*x(idx, 20).*x(idx, 7)))+exp(2.*constants(4).*x(idx, 18)./(x(idx, 19).*x(idx, 20).*x(idx, 1))))+...
x(idx, 16).*(exp(2.*constants(4).*x(idx, 18)./(x(idx, 19).*x(idx, 20).*x(idx, 8)))+exp(2.*constants(4).*x(idx, 18)./(x(idx, 19).*x(idx, 20).*x(idx, 1))))+...
x(idx, 17).*(exp(2.*constants(4).*x(idx, 18)./(x(idx, 19).*x(idx, 20).*x(idx, 9)))+exp(2.*constants(4).*x(idx, 18)./(x(idx, 19).*x(idx, 20).*x(idx, 1))))))...
-constants(3).*exp(2.*constants(4).*x(idx, 18)./(x(idx, 19).*x(idx, 20).*x(idx, 1)))), (1:size(x,1)).');
% Declare starting values
x0 = [1, 1, 1, 2];
xfit = rho_local_paramms(:,2:end);
yfit = rho_local_paramms(:,1
Hope this helps!
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!