lsqcurvefit with nested sub-parameter/constraint

3 次查看(过去 30 天)
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 个评论
Matt J
Matt J 2013-1-30
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.

请先登录,再进行评论。

采纳的回答

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

请先登录,再进行评论。

更多回答(1 个)

Shashank Prasanna
Shashank Prasanna 2013-1-30
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 个评论
Markus
Markus 2013-1-31
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?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by