lsqcurvefit with nested sub-parameter/constraint
7 ビュー (過去 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
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
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 件)
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:
参考
カテゴリ
Help Center および File Exchange で Linear Least Squares についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!