Error using lsqcurvefit (line 271) Function value and YDATA sizes are not equal.

3 views (last 30 days)
Hello everybody. I am new to MATLAB and I'm trying to find the parameters to best fit a model function having experimental points as ti and yi. The main code and functions are:
data = xlsread("Re-analyze.xlsx");
ti = data(:,6);
yi = data(:,7);
R = data(1,1);
T = data(2,1);
Me = data(3,1);
Mn0i = data(4,1);
Vs0 = data(6,1);
Mse = data(7,1);
V0 = data(8,1);
X0=[0.72; 28.876; 6568.86;1;1];
save variables_LpPsVb R T A Me V0 A Me Mn0i Vs0 Mse X0
function X = Lp_Ps_Vb (b,t)
load variables_LpPsVb R T A V0 Me Mn0i Vs0 Mse
% % % b(1:3) = Parameters, b(4:5) = Initial Conditions
% % % MAPPING: x(1) = x, x(2) = y, b(1) = Lp, b(2) = Ps, b(3) = Vb
Ddv_Div = @(t,x,b)[((-b(1)*x(1)*A*R*T*Me*Vs0)+ b(1)*x(2)*R*T*A+b(1)*A*R*T*Mn0i*V0*Vs0-b(1)*b(3)*A*R*T*Mn0i*Vs0)/(Vs0*x(1));
[~,X] = ode45(@(t,x) Ddv_Div(t,x,b(1:3)), t, b(4:5));
I get this error message:
Error using lsqcurvefit (line 271) Function value and YDATA sizes are not equal.
Unfortunately, I can not see my mistake. What should I try to solve it?
Thank you in advance!
srcn on 18 Sep 2021
so sorry. I wrote it in the comments below but forgot to add it here.
You can find corrected code : updated code

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 15 Sep 2021
The Lp_Ps_Vb function is going to return ‘X’ that is going to be a (numel(t) x 2) matrix, so lsqcurvefit needs to have a matching matrix of data to compare to it. (The error that was thrown can also occur when rows of data are compared to columns returned by the objective function, or the reverse, however I doubt that is the problem here.)
I am not certain what you are doing, however if you want to fit the system of differential equations to your data, the approach in Coefficient estimation for a system of coupled ODEs will likely work.
It will be necessary to adapt that code to your system. That should not be difficult.

Sign in to comment.

More Answers (0)


Find more on Systems of Nonlinear Equations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!