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

32 views (last 30 days)
Hello everybody. I'm trying to find the parameters to best fit a model function having experimental points as xdata and ydata. The programm and functions are:
A=load('data.txt');
xdata=A(:,1);
ydata=A(:,2);
x0=[1 34 10000 0.7]; % initial guess on parameter x
x=lsqcurvefit(@F,x0,xdata,ydata);
Function:
function F = F(x,xdata)
Na=6.0221413e+23;
phi0=0.5;
Rh0=min(xdata);
nius=1.81e-5;
F=(x(1)*x(2)*phi0^2*(Rh0/xdata).^6)/...
(nius*x(3)/(Na*4*pi*Rh0^3/3)*(1/2*(Rh0/xdata).^3-(Rh0/xdata))-...
phi0*(Rh0/xdata).^3-log(1-(phi0*(Rh0/xdata).^3))+...
phi0^2*(x(1)-1/2)*(Rh0/xdata).^6-x(4)*phi0^3*(Rh0/xdata).^9);
I get this two error messages:
Error using lsqcurvefit (line 248) Function value and YDATA sizes are not equal.
Error in datafit (line 12) x=lsqcurvefit(@F,x0,xdata,ydata);
What am I doing wrong? I'm sorry if this is a stupid question, but I'm quite new to Matlab. Thanks.
  2 Comments
Anthony
Anthony on 19 Feb 2014
Edited: Anthony on 19 Feb 2014
Hum... Did you check:
size(ydata)
size(F(x0,xdata))
They should be equal, if not, there is maybe a mistake in your definition of F.
Cheers,

Sign in to comment.

Answers (5)

Ben C
Ben C on 25 Apr 2016
You could have inputs of function reversed. Function inputs should be (x0, x)

sumalatha vasam
sumalatha vasam on 8 Mar 2019
Facing same problem
Error using lsqcurvefit (line 262)
Function value and YDATA sizes are not equal.
Error in test52 (line 12)
MI=lsqcurvefit(@impedance_test52,[100,4,10e7,2,1,1,10e-6],field,Z,lb,ub);
can u pls help me regarding this issue ....
function modZ=impedance_test52(var,field)
M=var(1);Ha=var(2);sig2=var(3);teta=var(4);si=var(5);alpha=var(6);D=var(7);
%Define constants
g=((2.2).*(10e5));
w=((90).*(10e3));
M0=((4).*(pi).*(10e-7));
l=0.03;
c=(3*(10e8));
% Define function.
wm=g*4*(pi)*M;
w1=g*((Ha*(cosd(teta-si))^2)+(field*(sind(teta))));
w2=g*((Ha*(cosd(2*(teta-si))))+(field*(sind(teta))));
%Relative permeability
Mr=(wm.*(wm+w1-(1i.*(alpha).*w)))/(((wm+w1-(1i.*(alpha).*w)).*(w2-(1i.*(alpha).*w)))-(w^2));
Mr=abs(Mr);
Mef=Mr*M0;
T2=c/(sqrt(2*(pi)*(sig2)*w));
Z=(((1-1i)*l)/((pi)*(sig2)*D*T2))*(((sqrt(Mef+1))*((sind(teta))^2))+(cosd(teta))^2);
modZ=((abs(Z)/(4.4572e-10))-1)*100;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all;
close all;
load('mat52.txt');
field= mat52(:,1);
Z= mat52(:,2);
plot(field,Z,'bo');
hold on;
lb=[90,2,5.6*(10e7),1,0.1,0.1,0.1*(10e-5)];
ub=[120,15,5.6*(10e8),10,10,5,100*(10e-5)];
MI=lsqcurvefit(@impedance_test52,[100,4,10e7,2,1,1,10e-6],field,Z,lb,ub);
fitdata=impedance_test52(MI,field);
plot(field, fitdata,'-b');
xlabel('Field');
ylabel('MI');
legend('mat52','fitting')
title('MI vs Field')
xlable and ylable
-15.2 273.20056
-14.4 278.50354
-13.6 283.44331
-12.8 287.36606
-12 291.97893
-11.2 295.96223
-10.4 301.4226
-9.6 305.21823
-8.8 307.87578
-8 310.12168
-7.2 309.65555
-6.4 307.67601
-5.6 309.11677
-4.8 308.11187
-4 302.00375
-3.2 295.5869
-2.4 282.01465
-1.6 266.74738
-0.8 253.90762
0 251.76379
0.8 253.90762
1.6 266.74738
2.4 282.01465
3.2 295.5869
4 302.00375
4.8 308.11187
5.6 309.11677
6.4 307.67601
7.2 309.65555
8 310.12168
8.8 307.87578
9.6 305.21823
10.4 301.4226
11.2 295.96223
12 291.97893
12.8 287.36606
13.6 283.44331
14.4 278.50354
15.2 273.20056
end
  6 Comments
Torsten
Torsten on 14 Mar 2019
Use
Mr=(wm.*(wm+w1-(1i.*(alpha).*w)))./(((wm+w1-(1i.*(alpha).*w)).*(w2-(1i.*(alpha).*w)))-(w^2));
instead of
Mr=(wm.*(wm+w1-(1i.*(alpha).*w)))/(((wm+w1-(1i.*(alpha).*w)).*(w2-(1i.*(alpha).*w)))-(w^2));
What are the sizes now ?

Sign in to comment.


sumalatha vasam
sumalatha vasam on 15 Mar 2019
Thank u very much, It's working.

Joanalyn
Joanalyn on 2 May 2023
Edited: Torsten on 2 May 2023
Facing same problem
In three_system (line 53)
Error using lsqcurvefit
Function value and YDATA sizes are not equal.
Error in three_system (line 53)
[theta,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat]=lsqcurvefit(@kinetics,theta0,t,c);
can u pls help me regarding this issue ....
three_system()
Warning: Failure at t=1.452557e+00. Unable to meet integration tolerances without reducing the step size below the smallest value allowed (3.552714e-15) at time t.
Error using lsqcurvefit
Function value and YDATA sizes are not equal.

Error in solution>three_system (line 39)
[theta,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat]=lsqcurvefit(@kinetics,theta0,t,c);
function three_system
% 2016 12 03
% NOTES:
%
% 1. The ‘theta’ (parameter) argument has to be first in your
% ‘kinetics’ funciton,
% 2. You need to return ALL the values from ‘DifEq’ since you are fitting
% all the values
t=[1
2
3
4
5
6
7
8
9
10
11
12];
c=[96 15 4
98 20 7
85 18 3
91 20 4
75 35 9
74 37 10
84 19 18
73 25 23
72 26 27
99 21 24
102 20 26
110 18 25];
theta0=[1;1;1;1;1;1;1;1;1;1;1];
[theta,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat]=lsqcurvefit(@kinetics,theta0,t,c);
fprintf(1,'\tRate Constants:\n')
for k1 = 1:length(theta)
fprintf(1, '\t\tTheta(%d) = %8.5f\n', k1, theta(k1))
end
tv = linspace(min(t), max(t));
Cfit = kinetics(theta, tv);
figure(1)
plot(t, c, 'p')
hold on
hlp = plot(tv, Cfit);
hold off
grid
xlabel('Time')
ylabel('Number')
legend(hlp, 'C_1(t)', 'C_2(t)', 'C_3(t)', 'Location','N')
end
function C=kinetics(theta,t)
c0=[100;10;2];
[T,Cv]=ode15s(@DifEq,t,c0);
%
function dC=DifEq(t,c)
dcdt=zeros(3,1);
dcdt(1)= theta(1).*c(1)-theta(4).*c(1)^2-((theta(7).*(c(1)^2).*c(2))\(theta(5)+(c(1)^2)))-((theta(11).*theta(7).*c(1)^2.*c(3))\(theta(5)+c(1)^2));
dcdt(2)= theta(2).*c(2)-((theta(8).*c(2).*(c(2)+c(3)))\(theta(6)+c(1)))-theta(10).*c(2).*c(3);
dcdt(3)= theta(10).*c(2).*c(3)+theta(3).*c(3)-((theta(9).*c(3).*(c(2)+c(3)))\(theta(6)+c(1)));
dC=dcdt;
end
C=Cv;
end
  1 Comment
Torsten
Torsten on 2 May 2023
Your integration with ode45 is not successful (see above). Thus the returned Cv vector is incomplete (only 1x3 instead of 12x3).

Sign in to comment.


Tobias Tietz
Tobias Tietz on 4 Jul 2023
Edited: Torsten on 4 Jul 2023
Error using lsqcurvefit
Function value and YDATA sizes are not equal.
y = [31.6968154531912 0.890931218661132
38.4804596257648 0.892775450209936
46.4695759506599 0.894167809732286
55.8756531897544 0.895239085956052
66.9438897413054 0.896075757306964
79.9650953621297 0.896738163648594
95.2780467115373 0.897268894599403
113.280906840510 0.897698637646080
134.425458339456 0.898049290870298
159.262240551692 0.898337962105557
188.417221640351 0.898577105090496
222.621078145907 0.898776292802748
262.722843413351 0.898943093784409];
T = [8.82800139811669
9.40413871093602
9.96672666696677
10.5200423966807
11.0668837672769
11.6100076414882
12.1513939504397
12.6928526284094
13.2343009424779
13.7776845090378
14.3229791441139
14.8700715607888
15.4185722748141];
A = [log(y(:,1)),ones(size(T,1),1),1./y(:,2)];
b = T;
c1 = A\b
c1 = 3×1
3.2655 -42.1436 35.3726
c = [1,1,1];
f = @(c, y) c(1).* log(y(:,1)) + c(2) + c(3).*(1./y(:,2));
c2 = lsqcurvefit(f, c, y, T)
Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
c2 = 1×3
3.2655 -42.1436 35.3726
Can someone help me?
  1 Comment
Torsten
Torsten on 4 Jul 2023
Edited: Torsten on 4 Jul 2023
Corrected (see above).
Your problem is linear in the fitting coefficients. It can easily be solved using backslash as added to your code.
A = [log(y(:,1)),ones(size(T,1),1),1./y(:,2)];
b = T;
c1 = A\b

Sign in to comment.

Categories

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!