Error: Error using lsqcurvefit Function value and YDATA sizes are not equal.

Hi all!
I have written the following script (just show the important part):
grad=xydata(:,1);
int=xydata(:,2);
b=(nuclgamma*2*p30)^2
exponent_cnst= (nuclgamma*2*p30)^2 * (d20 -p30/3);
Delta_prime=(d20 -p30/3)
x = linspace(min(grad),max(grad));
options = optimset('MaxFunEvals', 300,'MaxIter', 300,'TolFun',1e-12,'Display','off');
D1_prime= @(x,p) (0.5*(p(1)+p(2)+1./(b*x.^2)*p(3))+sqrt(sqrt(p(1)+p(2)+1./(b*x.^2*p(3)))+4./(b^2*x.^4*p(4))))
D2_prime= @(x,p) (0.5*(p(1)+p(2)+1./(b*x.^2)*p(3))-sqrt(sqrt(p(1)+p(2)+1./(b*x.^2*p(3)))+4./(b^2*x.^4*p(4))))
p2_prime= @(x,p) ((p(5)*p(1)+p(6)*p(2)-D1_prime(x,p))/(D2_prime(x,p)-D1_prime(x,p)))
p1_prime= @(x,p) (1-((p(5)*p(1)+p(6)*p(2)-D1_prime(x,p))/(D2_prime(x,p)-D1_prime(x,p))))
model= @(x,p) p1_prime(x,p)*exp(-exponent_cnst.*D1_prime(x,p).*Delta_prime)+p2_prime(x,p)*exp(-exponent_cnst.*D2_prime(x,p).*Delta_prime)
% p(1) p(2) p(3) p(4) p(5) p(6)
% D1 D2 1/tau1+1/tau2 tau1*tau2 P1 P2
%
% p(1) p(2) p(3) p(4) p(5) p(6)
startingVals = [1e-9 1e-8 1e5 1e-3 0.5 0.5 ]; %guess values
lb = [1e-13 1e-13 1 1e-12 0 0 ]; %lower bound
ub = [1e-4 1e-4 1e10 1e-6 1 1 ]; %upper bound
[p] = lsqcurvefit(model,startingVals,grad,int,lb,ub,options);
I've checked the script again and again but don't find my mistake :-( I'm very new with MATLAB so please forgive if my question is kind of trivial.
Many thx in advance!

Answers (2)

MATLAB think that your objective function is not giving you a return vector of the correct size. Take your x data, grad, take a vector x of potential parameter values (might as well take lb or ub), and evaluate
model(x,grad)
See if you get a result the same size as your y data, int.
If not, then fix things until you do.
If so, then please post the entire error message, copy and paste, do not retype.
Alan Weiss
MATLAB mathematical toolbox documentation
You switched the order of the arguments in "model": use model(p,x) instead of model(x,p).
Furthermore, the line
x = linspace(min(grad),max(grad));
can be deleted.
Best wishes
Torsten.

Categories

Asked:

on 7 Apr 2016

Answered:

on 8 Apr 2016

Community Treasure Hunt

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

Start Hunting!