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 件のコメント

Walter Roberson
Walter Roberson 2018 年 1 月 19 日
What do you want to have happen if the caller attempts to violate the constraints?
John D'Errico
John D'Errico 2018 年 1 月 19 日
編集済み: John D'Errico 2018 年 1 月 19 日
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.
Giru Mishra
Giru Mishra 2018 年 1 月 19 日
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.
Giru Mishra
Giru Mishra 2018 年 1 月 19 日
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 件のコメント

Giru Mishra
Giru Mishra 2018 年 1 月 19 日
i am already applying one if else loop as there is one more condition on x.. how to apply two if else?
Walter Roberson
Walter Roberson 2018 年 1 月 19 日
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.
Giru Mishra
Giru Mishra 2018 年 1 月 20 日
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 ExchangeGet Started with Curve Fitting Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by