How can I fit data to a piecewise function, where the breakpoint of the function is also a parameter to be optimised?
51 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have data with x and y values. This data should conform to a function: an assymmetric parabola. Here, the parameters that define the shape of the parabola should be different on either side of the maximum point of the parabola i.e. the breakpoint is where the maximum value of y occurs.
I was hoping to use 'fit' and to define an anonymous function for my data. But I'm not able to work out how to define an anonymous, piecewise function, especially where the breakpoint is one of the parameters to be determined by the fitting procedure, as it is not immediately clear from the data itself where the maximum value of y should occur.
Any help would be appreciated.
0 comentarios
Respuesta aceptada
Matt J
hace alrededor de 16 horas
Editada: Matt J
hace alrededor de 9 horas
Once you've chosen the coefficients of the first parabola [a1,b1,c1], the breakpoint is determined from,
d=-b1/(2*a1)
Only the leading coefficient of the second parabola is a free parameter:
F=@(x) asymParabola(-2,1,0,-0.6,x);
fplot(F,[-10,10]);axis padded %example plot
ft = fittype(@(a1,b1,c1,a2, x) asymParabola(a1,b1,c1,a2, x) )
function y=asymParabola(a1,b1,c1,a2, x)
d=-b1/(2*a1);
b2=-d*2*a2;
c2=polyval([a1,b1,c1],d)-polyval([a2,b2,0],d);
left=(x<=d);
y=x;
y(left)=polyval([a1,b1,c1],x(left));
y(~left)=polyval([a2,b2,c2],x(~left));
end
4 comentarios
Torsten
hace 3 minutos
Editada: Torsten
hace 2 minutos
If the given x,y values have no noise/errors, then the maximum y-value and the maximum of the parabola are one and the same.
Why ? Both parabola can intersect below their respective maxima, and nonetheless the point of intersection can be the maximum y-value of the piecewise function.
But it seems you interpreted the question correctly.
Más respuestas (2)
Walter Roberson
hace alrededor de 23 horas
(a1*x.^2 + b1*x + c1) .* (x <= d) + (a2*x.^2 + b2*x + c2) .* (x > d)
Note that for this to work, the coefficients must be constrained to be finite
2 comentarios
Paul
hace alrededor de 1 hora
Sounds like both sides of the function should have the same value at x = d, at least that's how interpret the question. If so, then I think the function would look something like
(a1*(x-d).^2 + b1*(x-d) + c) .* (x <= d) + (a2*(x-d).^2 + b2*(x-d) + c) .* (x > d)
Catalytic
hace alrededor de 6 horas
Editada: Catalytic
hace alrededor de 6 horas
You can also parametrize the model function directly in terms of the break point coordinates (xbreak, ybreak) and two curvature parameters -
F= @(a1,a2,xbreak,ybreak, x) modelFun(a1,a2,xbreak,ybreak, x);
xbreak=3; ybreak=5;
fplot( @(x) F(-2,-0.6,xbreak,ybreak,x), [1,5]);
xline(xbreak,'--')
fType = fittype(F);
function y=modelFun(a1,a2,xbreak,ybreak, x)
X=x-xbreak;
LHS=(X<=0);
RHS=~LHS;
y=X.^2;
y(LHS)=a1.*y(LHS) + ybreak;
y(RHS)=a2.*y(RHS) + ybreak;
end
0 comentarios
Ver también
Categorías
Más información sobre Get Started with Curve Fitting Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!