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 Comments

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.
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.

Sign in to comment.

 Accepted Answer

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

4 Comments

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..
@Giru,
You should Accept-click the answer if it helped you.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!