lsqcurvefit with nested sub-parameter/constraint

6 visualizzazioni (ultimi 30 giorni)
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 Commento
Matt J
Matt J il 30 Gen 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.

Accedi per commentare.

Risposta accettata

Matt J
Matt J il 30 Gen 2013
Modificato: Matt J il 31 Gen 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.
  1 Commento
Markus
Markus il 31 Gen 2013
Thanks for the reply and the suggestions, Matt.
It took me a while, but it is working perfectly with LSQLIN-solver!
Thanks a lot!

Accedi per commentare.

Più risposte (1)

Shashank Prasanna
Shashank Prasanna il 30 Gen 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:
  1 Commento
Markus
Markus il 31 Gen 2013
moved from answer to comment by Markus:
Thanks for the answer, Benji.
I played around with OPTIONS and told the routine to give me the results for each iteration step. Now I see, that after about 30 iterations nothing changes anymore. But still, the average condition is not fulfilled.
So I guess my average-condition is somehow stated wrong. Any ideas about that?

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by