lsqcurvefit with nested sub-parameter/constraint
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi there,
I need to fit the function
%code
y = gamma + a(1).*(x.^2)+ a(2).*(x.^4);
to experimental data. Independent variable is x, dependent variable is y, parameters to be optimized are a(1) and a(2).
The difficulty is that the parameter "gamma" is computed from a(1) and a(2) according to
% code
gamma = const - mean(a(1).*(x(1:xf).^2)+ a(2).*(x(1:xf).^4));
with xf<length(x) and a known constant "const".
In other words, my third parameter is computed from the average function value of the remaining function over some limited range of x=1:xf.
What is the aim of this construction? I want to force my fit to have an average value of "const" in the range x=1:xf.
I tried following function:
% code
function F = myfun(a,data)
x=data(1,:);
F = const - mean(a(1).*(x(1:xf).^2)+ a(2).*(x(1:xf).^4)) + a(1).*(x.^2)+ a(2).*(x.^4);
end
The optimization terminates, and I receive the message:
Optimization terminated: first-order optimality less than OPTIONS.TolFun, and no negative/zero curvature detected in trust region model.
But the fit does not fulfill the "average"-condition I stated above.
Any suggestions? I'm glad for any help/advise.
1 Kommentar
Matt J
am 30 Jan. 2013
Note, the function
y = const - mean(a(1).*(x(1:xf).^2)+ a(2).*(x(1:xf).^4)) + a(1).*(x.^2)+ a(2).*(x.^4);
is linear in a(1) and a(2). You could have just used a linear equation solver (e.g. backslash) to solve it. Since this probably isn't what you want anyway, though, see my answer below.
Akzeptierte Antwort
Matt J
am 30 Jan. 2013
Bearbeitet: Matt J
am 31 Jan. 2013
The only way you're going to fulfill the average condition exactly (or within some small precision) is to explicitly impose the linear equality constraint
gamma = const - mean(a(1).*(x(1:xf).^2)+ a(2).*(x(1:xf).^4));
on the optimization, and for that you're going to have to use another solver, one which let's you specify equality constraints. LSQLIN would probably be best. You could also look at QUADPROG or FMINCON.
Note also that you will not be able to eliminate gamma for this. You will need to include it as a 3rd unknown parameter.
Weitere Antworten (1)
Shashank Prasanna
am 30 Jan. 2013
Relax the tolerance OPTIONS.TolFun, make it smaller than the default so your optimization can run longer. All iterative algorithms need some reason to terminate, and will need tweaking.
Check the doc on how to do that:
Siehe auch
Kategorien
Mehr zu Linear Least Squares finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!