Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polynomial fit matlab with some constraints on the coefficients

I have data that I should interpolate with a function which must be of the following kind:

f(x) = ax4 + bx2 + c

with a > 0 and b ≤ 0. Unfortunately, MATLAB's polyfit does not allow any constraints on the coefficients of the polynomial. Does anybody know if there is a MATLAB function to do this? Otherwise, how can I implement it?

Thank you very much in advance,

Elisabetta

like image 368
bettaberg Avatar asked Jul 01 '13 14:07

bettaberg


People also ask

How do you fit a polynomial in Matlab?

Fit Polynomial to Set of Points In general, for n points, you can fit a polynomial of degree n-1 to exactly pass through the points. p = polyfit(x,y,4); Evaluate the original function and the polynomial fit on a finer grid of points between 0 and 2. x1 = linspace(0,2); y1 = 1./(1+x1); f1 = polyval(p,x1);

What is the difference between Polyfit and Polyval?

Polyfit generates the coefficients of the polynomial, which can be used to model a curve to fit the data. Polyval evaluates a polynomial for a given set of x values. So, Polyval generates a curve to fit the data based on the coefficients found using polyfit.

What could be done to improve the cubic fit in Matlab?

To improve the cubic fit - Call polyfit with 3 outputs to let x be scaled and shifted automatically - ex = [p, S, mu] = polyfit(x, y, n) .

What is polynomial regression Matlab?

Polynomial Regression – Least Square FittingsThe idea is to find the polynomial function that properly fits a given set of data points. For this purpose, we're going to use two useful built-in functions: polyfit (for fitting polynomial to data) and polyval (to evaluate polynomials).


2 Answers

You can try using fminsearch, fminunc defining your objective function manually.

Alternatively, you can define your problem slightly different:

f(x) = a2x4 - b2x2 + c

Now, the new a and b can be optimized for without constraints, while ensuring that the final a and b you are looking for are positive (negative resp.).

like image 88
Shai Avatar answered Sep 20 '22 13:09

Shai


Without constraints, the problem can be written and solved as a simple linear system:

% Your design matrix ([4 2 0] are the powers of the polynomial)
A = bsxfun(@power, your_X_data(:), [4 2 0]);

% Best estimate for the coefficients, [a b c], found by 
% solving A*[a b c]' = y in a least-squares sense
abc = A\your_Y_data(:)

Those constraints will of course automatically be satisfied iff that constrained model indeed underlies your data. For example,

% some example factors
a = +23.9;
b = -15.75;
c = 4;

% Your model
f = @(x, F) F(1)*x.^4 + F(2)*x.^2 + F(3);

% generate some noisy XY data
x = -1:0.01:1;
y = f(x, [a b c]) + randn(size(x));

% Best unconstrained estimate a, b and c from the data
A = bsxfun(@power, x(:), [4 2 0]);
abc = A\y(:);

% Plot results
plot(x,y, 'b'), hold on
plot(x, f(x, abc), 'r')
xlabel('x (nodes)'), ylabel('y (data)')

enter image description here

However, if you impose constraints on data that are not accurately described by that constrained model, things might go wrong:

% Note: same data, but flipped signs 
a = -23.9;
b = +15.75;
c = 4;

f = @(x, F) F(1)*x.^4 + F(2)*x.^2 + F(3);

% generate some noisy XY data
x = -1:0.01:1;
y = f(x, [a b c]) + randn(size(x));

% Estimate a, b and c from the data, Forcing a>0 and b<0
abc = fmincon(@(Y) sum((f(x,Y)-y).^2), [0 0 0], [-1 0 0; 0 +1 0; 0 0 0], zeros(3,1));

% Plot results
plot(x,y, 'b'), hold on
plot(x, f(x, abc), 'r')
xlabel('x (nodes)'), ylabel('y (data)')

enter image description here

(this solution has a == 0, indicative of an incorrect model choice).

If the exact equality of a == 0 is a problem: there is of course no difference if you set a == eps(0). Numerically, this will not be noticeable for real-world data, but it's nonzero nonetheless.

Anyway, I have a suspicion that your model is not well chosen and the constraints are a "fix" to get everything to work, or your data should actually be unbiased/rescaled before trying to make any fit, or that some similar preconditions apply (I've often seen people do this sort of thing, so yes, I'm a bit biased in this respect :).

So...what are the real reasons behind those constraints?

like image 42
Rody Oldenhuis Avatar answered Sep 21 '22 13:09

Rody Oldenhuis