Fitting a function with constraints

52 views (last 30 days)
Jonas Kampp
Jonas Kampp on 18 May 2021
Edited: Matt J on 4 Jun 2024
Hi
I am currently struggling to fit a 2nd order polynomial function with specific constraints.
I have an external script that tells me wether the polynomial function is ascending, descending or u-shaped.
Therefore I've made a constraints (using the "fit" command) for the x^2 part to only be positive because I can only use downwards curves.
This is my current formula:
[p,gof] = fit(x,y,'poly2','lower',[0,-inf,-inf],'normalize','on')
Now I need to give specific constraint for each shape:
ascending: The minimum y-value must be at the first x-value (ymin = xmin)
descending: The minimum y-value must be at the last x-value (ymin = xmax)
U-curve: The minimum y-value must be in between the first and last x-value (xmin < ymin < xmax)
  1 Comment
Matt J
Matt J on 4 Jun 2024
Therefore I've made a constraints (using the "fit" command) for the x^2 part to only be positive because I can only use downwards curves.
I assume you mean convex curves. A quadratic polynomial will always have both upward and downward parts, regardless of the leading coefficient.

Sign in to comment.

Answers (2)

Aditya
Aditya on 4 Jun 2024
To fit a 2nd order polynomial function with specific constraints based on the shape of the curve (ascending, descending, or U-shaped) and to ensure the minimum y-value meets certain conditions relative to the x-values, you will need to take a slightly different approach. The fit function in MATLAB allows for lower and upper bounds on the coefficients but does not directly support constraints on the polynomial's shape or the location of its extremum directly in the way you've described.
Implementing Constraints
The direct constraints you've set for poly2 ensure the curve is U-shaped by setting the lower bound of a to 0. To handle the specific constraints for each shape:
  1. Fit the polynomial as you've done without specific shape constraints beyond ensuring it's U-shaped.
  2. Check the vertex position (x_v = -\frac{b}{2a}) relative to your x-range.
  3. Validate or adjust: After fitting, you can check if the curve meets the shape requirements (e.g., for an ascending curve, ensure that (x_v) is less than the minimum x in your data). If it doesn't, you may need to adjust your approach, possibly by selecting a different model or using optimization techniques to directly incorporate these constraints.
% Fit the polynomial
[p, gof] = fit(x, y, 'poly2', 'lower', [0, -Inf, -Inf], 'normalize', 'on');
% Calculate the vertex
a = p.p1;
b = p.p2;
xv = -b / (2 * a);
% Check the position of the vertex for U-curve
if xv > min(x) && xv < max(x)
disp('U-curve with vertex within range.');
else
disp('Vertex outside range. Adjust fitting or model.');
end
For more specific constraints like ensuring the curve is strictly ascending or descending from the first or last point, you might need a custom fitting approach or to apply post-fit adjustments based on the vertex position and the curve's direction.

Matt J
Matt J on 4 Jun 2024
Edited: Matt J on 4 Jun 2024
You can use a custom model for each case:
ascending: The minimum y-value must be at the first x-value
g = fittype( @(a, t, c, x) a*x.^2 - 2*a*(xmin-t)*x + c );
[p,gof] = fit(x,y,g,'lower',[0,0,-inf],'normalize','on')
descending: The minimum y-value must be at the last x-value
g = fittype( @(a, t, c, x) a*x.^2 - 2*a*(xmax+t)*x + c );
[p,gof] = fit(x,y,g,'lower',[0,0,-inf],'normalize','on')
U-curve: The minimum y-value must be in between the first and last x-value
g = fittype( @(a, t, c, x) a*x.^2 - 2*a*(xmin+t*(xmax-xmin))*x + c );
[p,gof] = fit(x,y,g,'lower',[0,0,-inf],'upper',[+inf,1,+inf],'normalize','on')

Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!