How can I find the best fit to a bunch of data?

Hi,
I am trying to find the best fitting to a bunch of data. I keep getting an error as below:
x = lsqcurvefit(@fcn,[.2, .2, .2] ,[T, V, M] , ones(93, 1)); ??? Error using ==> lsqcurvefit at 253 Function value and YDATA sizes are incommensurate.
Below are my m-files:
x = lsqcurvefit(@fcn,[.2, .2, .2] ,[T, V, M] , ones(93, 1));
function out = fcn(param, x) alpha = param(1); beta = param(2); gamma = param(3); T = x(1); V = x(2); M = x(3); out =(T/550)^alpha + (V/100)^beta + (M/30)^gamma;
Any help will be appreciated.

Answers (1)

Matt J
Matt J on 9 Dec 2012
Edited: Matt J on 9 Dec 2012
I'm assuming the lengths of T,V, and M are each 31. If so, you probably meant to do this:
x = lsqcurvefit(@fcn,[.2, .2, .2] ,[T,V,M] , ones(31, 1));
function out = fcn(param, x)
alpha = param(1);
beta = param(2);
gamma = param(3);
T = x(:,1); V = x(:,2); M = x(:,3);
out =(T/550)^alpha + (V/100)^beta + (M/30)^gamma;

4 Comments

Hi Matt,
Thank you for your answer. T, V, and M are each 101. They are 101*1 vector each. I have corrected based on what you did and below is the new errors:
*??? Error using ==> mpower Inputs must be a scalar and a square matrix. To compute elementwise POWER, use POWER (.^) instead.
Error in ==> fcn at 9 out = (T/550)^alpha + (V/100)^beta + (M/30)^gamma;
Error in ==> lsqcurvefit at 209 initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in ==> TMV at 1 x = lsqcurvefit(@fcn,[.2, .2, .2] ,[T, V, M] , ones(101, 1)); Caused by: Failure in initial user-supplied objective function evaluation. LSQCURVEFIT cannot continue.*
Regards, Amir
well then, do as the error suggests, as follows
out =(T/550).^alpha + (V/100).^beta + (M/30).^gamma;
Amir
Amir on 9 Dec 2012
Edited: Amir on 9 Dec 2012
Thank you, It seems that it works now with the below comment:
Local minimum possible. lsqcurvefit stopped because the size of the current step is less than the default value of the step size tolerance. criteria details * *Optimization stopped because the norm of the current step, 6.612031e-008, is less than options.TolX = 1.000000e-006.
Optimization Metric Options norm(step) = 6.61e-008 TolX = 1e-006 (default)* *
Any suggestion?
Amir,
See if the fit is good. If not, try to find a better initial guess.

Sign in to comment.

Categories

Products

Asked:

on 8 Dec 2012

Community Treasure Hunt

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

Start Hunting!