lsqcurvefit with nested sub-parameter/constraint

조회 수: 5 (최근 30일)
Markus
Markus 2013년 1월 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에서 Linear Least Squares에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by