How to fit a step function?

조회 수: 16 (최근 30일)
Mr M.
Mr M. 2015년 9월 16일
댓글: Jörn Froböse 2021년 6월 10일
The problem is only the first parameter (p(1)) is fitted, but p1(2) = p0(2), unchanged! Why?
X = [-1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0];
Y = [0.1 -0.15 0.05 0.0 -0.05 10.2 10.5 11.5 8.2 9.2 10.0 10.5 10.2 10.9 10.5 11.5];
plot(X,Y)
step = @(x)x > 0;
fun = @(p,x) p(1)*step(x-p(2))
p0 = [9,-0.5];
p1 = lsqcurvefit(fun,p0,X,Y);
Z = fun(p1,linspace(min(X),max(X)));
hold on
plot(linspace(min(X),max(X)),Z,'r')
  댓글 수: 1
Adam Danz
Adam Danz 2021년 4월 13일
It's not clear to me what the fit should look like or the resolution of the steps. For example,
X = [-1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0];
Y = [0.1 -0.15 0.05 0.0 -0.05 10.2 10.5 11.5 8.2 9.2 10.0 10.5 10.2 10.9 10.5 11.5];
plot(X,Y, 'b-o')
xr = [X(1),repelem(X(2:end),1,2)]; % X is row vec
yr = [repelem(Y(1:end-1),1,2),Y(end)]; % Y is row vec
hold on
plot(xr, yr, 'r-','LineWidth',2)

댓글을 달려면 로그인하십시오.

답변 (1개)

Figen Ece Demirer
Figen Ece Demirer 2021년 4월 13일
X = [-1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0];
Y = [0.1 -0.15 0.05 0.0 -0.05 10.2 10.5 11.5 8.2 9.2 10.0 10.5 10.2 10.9 10.5 11.5];
cut=0;
plot(X,Y,'g--o')
step = @(x)x > cut;
step2 = @(x)x < cut;
fun = @(p,x) p(1)*step(x)+p(2)*step2(x);
p0 = [9,-0.5];
p1 = lsqcurvefit(fun,p0,X,Y);
Z = fun(p1,linspace(min(X),max(X)));
hold on
plot(linspace(min(X),max(X)),Z,'r')
  댓글 수: 1
Jörn Froböse
Jörn Froböse 2021년 6월 10일
lsqcurvefit works for continous functions only

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Least Squares에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by