Optimisation toolbox error

1 vue (au cours des 30 derniers jours)
Shalini
Shalini le 10 Mar 2012
I have the following function which I wan to use in optimisation toolbox:
function diff = fit_simp_optim()
% 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.
X=xlsread('QS2T.xls',9,'V638:C1835');
Y=xlsread('QS2T.xls',9,'W638:W1835');
diff = (A+B*(X.^n)) - Y;
end
Now, in my command window I type:
>>optimtool
I get the optimisation toolbox;
In the optimisation toolbox,
1)corresponding to to the solver i select, lsqnonlin
2)then corresponding to algorithm, I select Trust region reflective
3)then corresponding to objective function i type @fit_simp_optim() which is the name of my .m function file
4)derivatives approximated by solver
5)Start point i give (which is for constants A,B,C- see my function above):[800;1537;0.1448]
6)Lower bound as [800;0;0] and Upper bound as [1600;6000;10]
Howver, when i start, it starts optimising but then says 'too many input arguments'
Please can anyoone help.

Réponse acceptée

Walter Roberson
Walter Roberson le 10 Mar 2012
The function you pass to lsqnonlin must accept a vector of values the same size as your starting point, and must return a vector of function values (possibly a different size.)
Have another look at your fit_simp_optim code: you use A and B and n but those are not defined as variables anywhere. (What happened to C, by the way?)
Redefine your code:
function diff = fit_simp_optim(ABn, 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 = ABC(1); B = ABC(2); n = ABC(3);
diff = (A+B*(X.^n)) - Y;
end
Then code
X = xlsread('QS2T.xls',9,'V638:C1835');
Y = xlsread('QS2T.xls',9,'W638:W1835');
lb = [800;0;0];
ub = [1600;600;10];
StartAt = [800;1537;0.1448];
optABn = lsqnonlin( @(ABn) fit_simp_optim(ABn,X,Y), StartAt, lb, ub);
  2 commentaires
Shalini
Shalini le 10 Mar 2012
Thanks a lot..it works.Can you tell me how to see the R-square for the fitted curve?
Walter Roberson
Walter Roberson le 10 Mar 2012
I am not certain, but it looks to me as this might be the second output argument:
[x,resnorm] = lsqnonlin(...) returns the value of the squared 2-norm of the residual at x: sum(fun(x).^2).
If so, then
[optABn, R2] = lsqnonlin( @(ABn) fit_simp_optim(ABn,X,Y), StartAt, lb, ub);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by