Function Error not understood

I wrote the following function:
function diff = fit_simp(x,X,Y)
% This function is called by lsqnonlin.
% x is a vector which contains the coefficients of the
% equation. X and Y are the option data sets that were
% passed to lsqnonlin.
A=x(1);
B=x(2);
n=x(3);
diff = (A+B*(X^n)) - Y;
end
Then the following script;
>> X=xlsread('QS2T.xls',9,'V638:V1835');
>> Y=xlsread('QS2T.xls',9,'W638:W1835');
>> X0=[800 1537 0.1448];
>> options = optimset('Largescale','off');
>> x=lsqnonlin(@fit_simp,X0,[],[],options,X,Y);
I get the error below, don't know what is wrong. All this was given in matlab examples in the website.PLs help, *|??? Error using ==> mpower Matrix must be square.
Error in ==> fit_simp at 12 diff = (A+B*(X^n)) - Y;
Error in ==> lsqnonlin at 202 initVals.F = feval(funfcn{3},xCurrent,varargin{:});
Caused by: Failure in initial user-supplied objective function evaluation. LSQNONLIN cannot continue.|*

 Réponse acceptée

Honglei Chen
Honglei Chen le 8 Mar 2012

0 votes

You may want to use X.^n to achieve element-wise operation

7 commentaires

Shalini
Shalini le 8 Mar 2012
Thanks it works- but still not over!
So the function is;
I added X.^n as you said (please can you explain what it actually does?)
I do not mind running non linear Lavenberg Marquardt or trust region algorithm.
I get this warning and error.Please can you again help?
>> X=xlsread('QS2T.xls',9,'V638:V1835');
Y=xlsread('QS2T.xls',9,'W638:W1835');
X0=[800 1537 0.1448];
>> options = optimset('Largescale','off');
>> x=lsqnonlin(@fit_simp,X0,[],[],options,X,Y);
Warning: Options LevenbergMarquardt and LargeScale will be ignored in
a future release. Running Levenberg-Marquardt method. To run the
Levenberg-Marquardt method without this warning, set option Algorithm
to 'levenberg-marquardt' instead.
> In optim\private\lsqncommon at 60
In lsqnonlin at 240
Maximum number of function evaluations exceeded;
increase options.MaxFunEvals
Honglei Chen
Honglei Chen le 8 Mar 2012
It just does element wise operation. Most likely your X is a vector, so you can only raise each element in the vector to a certain power, that's why you need to use .^ operator.
As to the warning from Levenberg Marquardt, the message is preparing for a future change. You may want to set the Algorithm field in your option variable to 'levenberg-marquanrdt'.
Shalini
Shalini le 8 Mar 2012
I'm very very new to matlab...Where do I set the algorithm field and how?
That option variable I just coped from a mathworks help webpage.I shall eb grateful if you tell me / write how and what to set?
I also would like to try Trust region
Please help..
Honglei Chen
Honglei Chen le 8 Mar 2012
in command line, type
>> option
and you'll see all the fields. To set algorithm field,
>> option.Algorithm = 'levenberg-marquanrdt'
Shalini
Shalini le 8 Mar 2012
Thanks and will do that..(I do not have matlab access for the next 2 hours).What abt the error cocerning MAxFunctions Eval?
Shalini
Shalini le 8 Mar 2012
I mean the error/warning: Maximum number of function evaluations exceeded;
How to increase this? I tried it by typing in the command line;
increase options.MaxFunEvals=60000 but does not help...??
Honglei Chen
Honglei Chen le 8 Mar 2012
this might suggest that the function didn't converge but I'm not sure what's wrong. You may need to examine your data.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by