Fitting a monotonically increasing spline function
조회 수: 36 (최근 30일)
이전 댓글 표시
I want to fit a monotonously increasing smooth spline function for a dataset
x = [0., 0.75, 1.8, 2.25, 3.75, 4.5, 6.45, 6.75, 7.5, 8.325, 10.875, 11.25, 12.525, 12.75, 15., 20.85, 21.]
y = [2.83811035, 2.81541896, 3.14311655, 3.22373554, 3.43033456, 3.50433385, 3.66794514, 3.462296, 3.59480959,
3.56250726, 3.6209845, 3.63034523, 3.68238915, 3.69096892, 3.75560395, 3.83545191, 3.90419498]
The current fit using interp1d looks like the above. I would like to know how to fit a monotonously increasing spline function.
댓글 수: 0
채택된 답변
Bruno Luong
2022년 9월 5일
One way is to use my https://uk.mathworks.com/matlabcentral/fileexchange/25872-free-knot-spline-approximation
x = [0., 0.75, 1.8, 2.25, 3.75, 4.5, 6.45, 6.75, 7.5, 8.325, 10.875, 11.25, 12.525, 12.75, 15., 20.85, 21.]
y = [2.83811035, 2.81541896, 3.14311655, 3.22373554, 3.43033456, 3.50433385, 3.66794514, 3.462296, 3.59480959,3.56250726, 3.6209845, 3.63034523, 3.68238915, 3.69096892, 3.75560395, 3.83545191, 3.90419498]
nknots=10;
opt=struct('shape',struct('p',1,'lo',zeros(1,nknots),'up',inf(1,nknots)));
pp=BSFK(x,y,4,nknots,[],opt); %FEX
xi=linspace(min(x),max(x),100);
yi=ppval(pp,xi);
plot(xi,yi,'-',x,y,'or')
댓글 수: 7
Bruno Luong
2022년 9월 6일
편집: Bruno Luong
2022년 9월 6일
Monotonic polynomial
x = [0., 0.75, 1.8, 2.25, 3.75, 4.5, 6.45, 6.75, 7.5, 8.325, 10.875, 11.25, 12.525, 12.75, 15., 20.85, 21.];
y = [2.83811035, 2.81541896, 3.14311655, 3.22373554, 3.43033456, 3.50433385, 3.66794514, 3.462296, 3.59480959,3.56250726, 3.6209845, 3.63034523, 3.68238915, 3.69096892, 3.75560395, 3.83545191, 3.90419498];
% Stuff needed to normalize the data for better inversion
[xmin, xmax] = bounds(x);
xnfun = @(x)(x(:)-xmin)/(xmax-xmin);
xn=xnfun(x);
% cofficients of 2D polynomial 3d order
k = 0:7;
C = [xn.^k]; % please no comment about my use of bracket here
d = y;
% Constraint positive of 3 x 3 points in the recatagular domain to be positive,
% it should be enough
XNC = linspace(0,1,41);
A = -[k.*XNC(:).^(k-1)]; % please no comment ...
A(:,k==0)=0;
b = 0+zeros(size(A,1),1); % A*P<=0 means polynomial at (xnc,ync)>=0
P = lsqlin(C,d,A,b);
% Graphical check
% Create a grided model surface
xi=linspace(xmin,xmax,201);
Xin=xnfun(xi);
Yi=[Xin.^k]*P; % please no comment about my use of bracket here
close all
plot(xi,Yi);
hold on
plot(x,y,'or')
xlabel('x')
ylabel('y')
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Polynomials에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!