add contraints on parameters defined in function

I have a function:
y =x.^a + z.^b
For which I wrote a separate function as I neet to fit it over my data. But I want to add constraint: a>b. How will I do that? Please help me with it. Thanks

댓글 수: 5

What do you want to have happen if the caller attempts to violate the constraints?
It is meaningless to say you have a constraint on a and b, because the function does not constrain them. A function is just a function. It does not care what you give it.
You want to use a fitting tool to fit some data, but you do not tell us which tool that is. That is the tool which must understand that the parameters are constrained, NOT the function to be fit.
So you need to use a tool that can handle inequality constraints on the parameters. And as it turns out, few such tools do allow general linear inequality constraints. But you could formulate the problem to be fit using fmincon.
Or, you could redefine the model to be in the form
y = x.^(a+b) + z.^b
where a was specified to have a lower bound of 0. Many tools allow bound constraints on the parameters.
I have x and y data. On that I have to fit the model y =Const(x.^a + z.^b) where a and b are the slopes. For these slopes I have a condition that a should be > b and if not then y is zero. I defined it as a function file and called the function in "fittype" and and fitted the data with "fit" command. But don't know how to add the constraint. Please help. Please let me know if any other information is required.
Torsten
Torsten 2018년 1월 19일
Use "lsqcurvefit" together with the model function y=Const*(x^(c1+c2)+z^c1) and include the bound constraint c2>0.
Once lsqcurvefit has determined c1 and c2, a=c1+c2 and b=c1 in your original model.
Best wishes
Torsten.
any other way?

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

 채택된 답변

Walter Roberson
Walter Roberson 2018년 1월 19일

0 개 추천

if a > b
y = x.^a + z.^b;
else
y = zeros(size(x));
end

댓글 수: 4

i am already applying one if else loop as there is one more condition on x.. how to apply two if else?
Example:
y = zeros(size(x));
if a > b
mask = (x > 17 & x < 23);
y(mask) = x(mask).^a + z.^b;
y(~mask) = exp(-x(~mask) * cos(z));
end
However, as Torsten indicates, it is not usually appropriate to apply constraints in the function being fitted: it most typically more appropriate to apply constraints in the range that function doing the fitting will use.
Thanks Walter and Torsten... It was really very helpful..
Matt J
Matt J 2018년 1월 20일
@Giru,
You should Accept-click the answer if it helped you.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

질문:

2018년 1월 19일

댓글:

2018년 1월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by