
Re: lsqcurvefit with matrix "xdata"
Posted:
May 10, 2013 2:20 PM


On 5/10/2013 11:29 AM, Jonathan wrote: > Alan_Weiss <aweiss@mathworks.com> wrote in message > <kminu8$35e$1@newscl01ah.mathworks.com>... >> On 5/9/2013 3:54 PM, Jonathan wrote: >> > I am trying to use lsqcurvefit to find a single parameter estimate >> > with two x values per predicted y, but the result is always >> "Function > value and YDATA sizes are incommensurate." Previously, >> when using a > similar function with only one value for x data per >> predicted y the > lsqcurvefit worked fine. The function that I am >> using returns the > correct results, a single y value, when I supply >> a parameter. I have > tried tinkering with the function thinking I >> messed it up some how, > but to no avail. >> > >> > function = @(k,x) (100./(1+k.*x(1))) + (100./(1+k.*x(2))) >> > x0 = .01 >> > ydata = <36,1> Double >> > xdata = <36,2> Double >> > >> > I am trying to determine if the summation of two hyperbolas is >> enough > to explain the ydata or if I need to consider another model. >> > >> > The documentation and help that I can find suggests that using a >> two > variable function for lsqcurvefit is possible, but for the life >> of me > I can not figure it out (where I screwed up). Thanks for any >> help. >> > >> > Jon >> >> Did you try to run the documentation example >> http://www.mathworks.com/help/optim/ug/nonlinearcurvefittingwithlsqcurvefit.html >> >> Does that example run for you? >> >> Alan Weiss >> MATLAB mathematical toolbox documentation > > I have used the documentation, and it was instrumental to me figuring > out how to use lsqcurvefit in the first place. The documentation does > not address the specific issue I am having. > > In the example there is one predicted y per x value, and three > parameters to fit. I have one predicted y per 2 x values, and only one > parameter. > I have not had trouble previously finding the parameters in situations > like the one in the documentation.
I just ran a little experiment and found no problem fitting matrix data. See if the following is anything like your case.
rng(5489,'twister') % reproducible xdata = 2*log(rand(100,1)); ydata = (ones(100,1) + .1*randn(100,1)) + (3*ones(100,1)+... 0.5*randn(100,1)).*exp(((2*ones(100,1)+... .5*randn(100,1))).*xdata); ydata2 = (.2*ones(100,1) + .1*randn(100,1)) + (2*ones(100,1)+... 0.5*randn(100,1)).*exp(((0.5*ones(100,1)+... .15*randn(100,1))).*xdata); % the second component of y ydata = [ydata,ydata2]; % second column depends on a4, a5, a6
predicted = @(a,xdata) [a(1)*ones(100,1)+a(2)*exp(a(3)*xdata),... a(4)*ones(100,1)+a(5)*exp(a(6)*xdata)];
a0 = [2;2;2;2;2;2];
[ahat,resnorm,residual,exitflag,output,lambda,jacobian] =... lsqcurvefit(predicted,a0,xdata,ydata);
% My result: ahat =
1.0169 3.1444 2.1596 0.2284 2.0523 0.5077
This is a good fit.
Alan Weiss MATLAB mathematical toolbox documentation

