polyfitcon

Version 1.0.0.0 (2.45 KB) by Murat
polynomial fitting with an optional constraint (can be set as a local extrema)
238 Downloads
Updated 30 Oct 2012

View License

% POLYFITCON - polynomial fitting with an optional additional constraint.
% the constraint point can be limited as a local extrema.
% [pfunc,pval]=polyfitcon(x,y,n,xc,yc,p0,isLocExt) returns
% pfunc, stores the polynomial of the order 'n' used for fitting.
% pval, weights of the fitting polynomial.
% pfunc=@(x,pval)pval(1).*x.^(n-1)+pval(2).*x.^(n-2)+...+pval(n).*x.^0
%
% INPUTs:
% x: observation locations
% y: observed points
% n: degree of polynomial to be fitted, giving empty value [] results in searching for the best order upto 10.
% xc: location of constraint, giving empty value [] results in unconstrained fitting.
% yc: observed value of the constraint, fitted polynomial passes through (xc,yc), giving empty value [] results in unconstrained fitting.
% p0: inital value for polynomial values, giving empty value [] results in starting from a p0 which is a zero vector of size n+1.
% isLocExt: if it is 1, the constrained point (xc,yc) becomes a local extremum, giving empty value [] results in relaxed first derivative criterion at the constraint point
%
% EXAMPLE 1:
% x=0:0.1:20; % x vector
% y=x.^2+x-3+rand(size(x))*20; % 2nd order polynomial plus some noise
% plot(x,y,'.'); hold on
% [pfunc,pval]=polyfitcon(x,y,2); % fit 2nd order polynomial
% yfit=pfunc(x,pval); % fitted data
% plot(x,yfit,'r')
%
% EXAMPLE 2: 3rd order fit with a constraint which has to be a local extremum
% x=0:0.1:20;
% y=-0.1*x.^3+2*x.^2+x-3+rand(size(x))*20;
% plot(x,y,'.'); hold on
% [pfunc,pval]=polyfitcon(x,y,3,14,149,[],1);
% yfit=pfunc(x,pval);
% plot(x,yfit,'r')
%
% EXAMPLE 3: indefinite order of fitting polynomial
% x=0:0.1:20;
% y=-0.3*x.^5+2000*x.^2+x-3+rand(size(x))*5;
% plot(x,y,'.'); hold on
% [pfunc,pval]=polyfitcon(x,y);
% yfit=pfunc(x,pval);
% plot(x,yfit,'r')
% !!! smaller orders could also work 'fine' !!! the function does consider the compromise between the simplicity(lowest order possible) and fitness
%
% Oct 2012
% ver 1.0
% (c) Murat Saglam
% email: saglammurat@gmail.com

Cite As

Murat (2024). polyfitcon (https://www.mathworks.com/matlabcentral/fileexchange/38845-polyfitcon), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2011a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Polynomials in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.0.0.0