File Exchange

image thumbnail

Fit Polynomial to Data with Constraints

version 1.0 (3.73 KB) by

essentially polyfit with many added features



View License

P=MMPOLYFIT(X,Y,N) finds the coefficients of a polynomial P(X) of degree N that fits the data Y in a least-squares sense. P is a row vector of length N+1 containing the polynomial coefficients in descending order,
P(1)*X^N + P(2)*X^(N-1) +...+ P(N)*X + P(N+1).
This matches POLYFIT. The number of elements in X and Y must be equal and greater than N.
MMPOLYFIT(X,Y,N,'Param1',PValue1,...) sets selected options based on pairs of parameters and associated values as described below.
MMPOLYFIT(X,Y,N,D) sets options based on the options structure D, whose fieldnames are parameters names and whose contents are associated values.
D=MMPOLYFIT('Param1',PValue1,...) returns an options structure D using the given input parameter names and parameter values that can be used for calls to MMPOLYFIT. Dnew=MMPOLYFIT(Dold,'Param1',PValue1,...) returns an options structure Dnew that is a copy of the existing options structure Dold altered by the added parameters and associated parameter values.


Weight A vector having NUMEL(X) real positive weights. Solution found is the weighted least squares solution. Default value is ONES(SIZE(X)), i.e., no weighting. The equation for the i(th) data point is scaled by the square root of the i(th) weight.

ZeroCoef A vector containing the powers of x whose coefficients must be zero, e.g., [1 3 0] sets the x^1, x^3 and x^0 coefficients to zero.

Point A p-by-2 matrix containing p points that the resulting polynomial must pass through. The i(th) row contains the i(th) data pair [x(i) y(i)].
Slope An s-by-2 matrix containing s points that the slope of the polynomial must pass through. The i(th) row contains the(i)th data pair [x(i) dy(i)/dx].

Equal A k-by-(N+2) matrix containing k equality constraints on the N+1 coefficients. The structure of this matrix is [B d] where B is k-by-N+1, d is k-by 1, and the constraints are given by B*P' = d where P is the row vector solution
containing the polynomial coefficients.

Comments and Ratings (3)

Hi! Can you please send me an example of how the Equality constraints can be used? Thanks!


Robert (view profile)

Figured it out, thanks!


Robert (view profile)

Could you provide an example on how to obtain the options structure D? I'd like to force a 2nd-order polynomial through 2 points.

MATLAB Release
MATLAB 7.1.0 (R14SP3)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video