lsqcurvefit error: Function value and YDATA sizes are not equal.

Hello, I'm trying to get the following code to work:
[ud,pd] = assignp3data
x0=[1; 1] ;
%
p2fit = lsqcurvefit(@p32,x0,ud,pd) ;
K2 = p2fit(1); C2 = p2fit(2) ;
%
[xmin,Jmin]=fminsearch(@p32,x0) ;
%
p2_predict = ((K2./ud(n)).*(ud(n)/C2).^K2.*exp(-(ud(n)/C2).^K2)) ;
%
with the function
function J = p32(K2,C2)
[ud,pd] = assignp3data(1807387)
N = numel(ud,pd) ;
for n = 1:N
J = sum((pd(n) - (K2./ud(n)).*(ud(n)/C2).^K2.*exp(-(ud(n)/C2).^K2)).^2) ;
end
end
however I keep getting the error in the title.
If it helps, the question I am trying to anwer is given here:
Thanks in advance for any help that can be provided

 Accepted Answer

One possibility is that the output of ‘p32’ is a single value, that being the last value of ‘J’ since it was not subscripted in the loop:
J = sum((pd(n) - (K2./ud(n)).*(ud(n)/C2).^K2.*exp(-(ud(n)/C2).^K2)).^2) ;
Another possibility could be that the y-data are a column vector and the output of ‘p32’ is a row vector, because this:
J(n) = sum((pd(n) - (K2./ud(n)).*(ud(n)/C2).^K2.*exp(-(ud(n)/C2).^K2)).^2) ;
will produce a row vector. To get it to produce a column vector, add a second ‘column’ subscript:
J(n,:) = sum((pd(n) - (K2./ud(n)).*(ud(n)/C2).^K2.*exp(-(ud(n)/C2).^K2)).^2) ;

6 Comments

Thanks for the quick reply. Do you know how I could make the loop output all the values (31 of them) that I need?
I tried to add in the
J(n)
and
J(n,:)
as well, however the second gives the same error and the first gives some other different errors.
I cannot improve on what I already wrote without your data.
If it helps you to see where I have gone wrong, I have attached my data set here
ud =
0.5000
1.0000
1.5000
2.1000
2.6000
3.1000
3.6000
4.1000
4.6000
5.1000
5.7000
6.2000
6.7000
7.2000
7.7000
7.8000
8.2000
8.7000
9.3000
9.8000
10.3000
10.8000
11.3000
11.8000
12.3000
12.9000
13.4000
13.9000
14.4000
15.4000
17.0000
pd =
-0.0107
0.0177
0.1369
0.1075
0.1973
0.1746
0.1812
0.1320
0.2040
0.1093
0.1226
0.0931
0.1089
0.0695
0.0538
0.0803
0.0955
0.0793
0.0052
0.0332
0.0098
0.0114
0.0064
0.0028
0.0038
0.0015
0.0014
0.0010
0.0006
0.0001
0.0001
thanks
You wrote your objective function incorrectly. The parameters must be one vector and the first argument, and the independent variable must be the second argument. (Note that you can write it ass an anonymous function, as I did here.)
Try this:
% % % MAPPING: b(1) = K, b(2) = C
p32 = @(b,u) (b(1)./u).*(u/b(2)).^b(1).*exp(-(u/b(2)).^b(1)) ;
That worked with lsqcurvefit when I ran it:
p2fit = lsqcurvefit(p32,x0,ud,pd)
I realise this is homework, and that normally we do not provide anything closely resembling complete code for homework. Please explore the relevant lsqcurvefit documentation on the corrrect syntax for objective functions.
A slightly different apprroach woule be required to use ‘p32’ with fminsearch or some of the other optimisation functions. Those would require calculating the norm of the difference between the dependent variable and the objective function, and optimising with respect to the paarameter vector.
Thank you for all of your help. I think I understand this a bit better now, and I will try to explore what I can do with fminsearch as I believe this is what we were supposed to use.
My pleasure.
If my Answer helped you solve your problem, please Accept it!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!