필터 지우기
필터 지우기

fminsearch with if statement

조회 수: 2 (최근 30일)
Christina
Christina 2013년 7월 26일
Hello all,
I need to optimise two parameters by minimising by objective function.
I've got an equation that has got two parts.
The parameters that I want to optimise are p0 and b0.
I already got the vectors obs and p.
The equation is:
If p < p0, then sim = p +b0*p0
if p>= p0, then sim = 5
Then I need to put my simulated values into my objective function, that looks like:
OF = (sim - obs)^2
So, now I want to minimise OF using fminsearch.
How can I do that, if the parameter that I am looking for is inside the constraint?
Thanks

채택된 답변

Matt J
Matt J 2013년 7월 26일
[xmin, fmin] = fminsearch(@(x) OF(x,obs,p), xguess);
function val=OF(x,obs,p)
p0=x(1);
b0=x(2);
sim= (p +b0*p0).*(p<p0)+5.*(p>p0);
val=(sim - obs).^2;
  댓글 수: 2
Christina
Christina 2013년 7월 27일
Hey Matt!
Thanks a lot for you reply, yes, that's mainly what I'd want to do.
However, the function option does not work for me, could you suggest how this could be written, without a function, i.e. as a simple script?
Thanks a lot.
Christina
Christina 2013년 7월 27일
Actually, I just found it.
Thanks a lot for your help!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt J
Matt J 2013년 7월 26일
편집: Matt J 2013년 7월 26일
Looks like you're doing a first order spline fit with a single free knot, and therefore this FEX file might already do everything you need,

카테고리

Help CenterFile Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by