Code covered by the BSD License  

Highlights from
polyfitcon

Be the first to rate this file! 10 Downloads (last 30 days) File Size: 2.45 KB File ID: #38845

polyfitcon

by

 

polynomial fitting with an optional constraint (can be set as a local extrema)

| Watch this File

File Information
Description

% 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

MATLAB release MATLAB 7.12 (R2011a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.

Contact us