How to fit a polynomial to a step function??

4 views (last 30 days)
Swati Jain
Swati Jain on 28 Mar 2019
Edited: Swati Jain on 31 Mar 2019
Hi,
I have a noisy step from a scnner result. Is there any way to fit a higher order polynimial to step ? I am not sure which polynomial should work. Attached is the data and figure of my step.

Answers (1)

Walter Roberson
Walter Roberson on 28 Mar 2019
No finite polynomial can possibly fit that -- not unless perhaps you swapped axes. What you have is more like a sigmoid function.
What might be acceptable is to fit it as a ratio of polynomials. If you use cftool and select Rational, and ask for numerator order 2 and denominator order 4, the fit is not terrible. (You might have to change the degree and change back again in order to get a fit that does not have singularities.)
  2 Comments
Walter Roberson
Walter Roberson on 31 Mar 2019
That status is not an error. It says that fmincon found a place it was willing to live with. It would have preferred that the difference between adjacent locations was smaller, but it has reached the minimum step size that you configured in (by default because you pass no options) and would need to take smaller steps to reduce the slope more. You can configure options to permit it to take smaller steps, but probably it would not help.
You appear to be fitting a phased gaussian. Those are notoriously difficult to fit well. When you fit with guassians, one of the common tendencies is for the fitting to push towards a narrow peak with a high phase, which is a adaptation to fit a noise spike.
Swati Jain
Swati Jain on 31 Mar 2019
Edited: Swati Jain on 31 Mar 2019
Sorry, my bad. I got my mistake. I will have to convolve gaussain with a perfect step and optimize it for minimum error with the measured point cloud. I could solve it with two methods. Both are giving almost same cofficients. But problem is error value (objective value for least square) is too large (around 274.9005). I tried no. of different initial values but every time I got same result. I tired to optimize with two different methods, both of them are giving approximately same cofficients.
My Method1 is as follows:
% Creat a Perfect Step
h=ones(1,numel(xf)); %height=1mm
h(1:5890)=-1.20;
h(5891:end)=-0.15;
figure;
plot(xf,h,'.-r','linewidth',1.5,'markersize',12);
grid on
set(gca, 'FontName', 'Arial');
set(gca, 'FontSize', 30);
xlabel('x(mm)'); ylabel('y(mm)'); zlabel('z(mm)');
axis equal
% initial parameters
mu= -0.97;
% mu= -0.970;
sigma=0.011/2.355;
A=1/17460;
xff=xf(1800:9800);
xff=xff';
z_Tr1=z_Tr1(1800:9800);
z_Tr1=z_Tr1';
p0=[A, mu, sigma];
yp=@(p) conv(h,(p(1)/(p(3)*sqrt(2*pi)))*exp(-((xff-p(2)).^2)/(2*p(3)^2)),'same');
objective = @(p) sum(((yp(p)-z_Tr1)).^2);
disp(['Initial Objective: ' num2str(objective(p0))])
popt = fmincon(objective,p0);
figure;
plot(xff,z_Tr1,'.k');
hold on
plot(xff,yp(p0),'.-b');
hold on
plot(xff,yp(popt),'gs')
legend('measured','initial predicted','optimal predicted')
ylabel('y')
xlabel('x')
disp(['Final Objective: ' num2str(objective(popt))])
disp(['Optimal parameters: ' num2str(popt)])
Method2:
f = @(F,xff) conv(h,(F(1)/(F(3)*sqrt(2*pi)))*exp(-((xff-F(2)).^2)/(2*F(3)^2)),'same');
F_fitted = nlinfit(xff, z_Tr1,f,[A mu sigma]);
% Display fitted coefficients
disp(['F = ',num2str(F_fitted)])
% Plot the data and fit
figure(1)
plot(xff,z_Tr1,'*',xff,f(F_fitted,xff),'.-k');
legend('data','fit')
Is there any other way to minimize the error value?

Sign in to comment.

Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!