Fitting a function with constraints
53 次查看(过去 30 天)
显示 更早的评论
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 个评论
Matt J
2024-6-4
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.
回答(2 个)
Aditya
2024-6-4
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:
- Fit the polynomial as you've done without specific shape constraints beyond ensuring it's U-shaped.
- Check the vertex position (x_v = -\frac{b}{2a}) relative to your x-range.
- 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.
0 个评论
Matt J
2024-6-4
编辑:Matt J
2024-6-4
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')
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!