How do I fit two equations that explain two parts of a curve?

Hi,my equations are: y(t)={(a/b)*(1-exp(-b(x-x_start)),for x_start <= x < x_end %equ1 and {c*exp(-b(x-xend)),for x > x_end %equ2
Here I do not know a,b and c. The idea is that since both equations have b in common, I would like to know how fitting these equations I can get one single value for b. I already fitted these equations individually, but I obtained two values of b, which are different from each fit.

1 件のコメント

John D'Errico
John D'Errico 2015 年 2 月 17 日
I predict that your next question will be why is my curvefit not continuous at the break point? I.e., at x_end, the pair of functions will not be continuous.

サインインしてコメントする。

 採用された回答

Sean de Wolski
Sean de Wolski 2015 年 2 月 17 日

0 投票

Break the data into two pieces and do the curve fit
xlow = x(x<x_end);
ylow = y*x<x_end);
xhigh = x(x>x_end); % Note you have nothing for x==x_end
yhigh = y(x>x_end);
Now do the curve fit - either with the Curve Fitting App, lsqcurvefit or fitnlm.

7 件のコメント

mdjupin
mdjupin 2015 年 2 月 17 日
Thanks Sean. I want to make sure that the common variable (here 'b') in both equations is fitted to the same value. Does your suggestion do this?
Sean de Wolski
Sean de Wolski 2015 年 2 月 17 日
No, you would have to solve for one and use that value of b for both with mine (i.e. not solve for it in the second equation). In order to find b that works for both equations, I don't know how to do that as there is a discontinuity in the fitting function. A global optim solver would probably be best doing both curve fits with it at once..
mdjupin
mdjupin 2015 年 2 月 18 日
Hi Sean, could you tell me more about global optimum solver. Thank you.
Torsten
Torsten 2015 年 2 月 18 日
Use lsqnonlin and define the f_i as
f_i = y_i -(p(1)/p(2))*(1-exp(-p(2)(x_i - x_start)) for x_start <= x_i < x_end
and
f_i = y_i -{p(3)*exp(-p(2)(x_i-xend)) for x_i > x_end
(x_i,y_i) are your measurement data.
Best wishes
Torsten.
mdjupin
mdjupin 2015 年 2 月 23 日
@ Torsten thank you. But this does not work out, due to lsqnonlin fit only the first equation in myfuntion.m. I also tried: myfuntion.m
funtion err = myfuntion(p) err = sum(y_i -(p(1)/p(2))*(1-exp(-p(2)(x_i - x_start)))-sum(y_i -{p(3)*exp(-p(2)(x_i-xend)))
But this also failed. Any other option?
Torsten
Torsten 2015 年 2 月 23 日
You programmed it as
function call
xdata=[...];
ydata=[...];
xstart=...;
xend=...;
p0=[1 1 1];
p = lsqnonlin(@(p)myfun(xdata,ydata,xstart,xend,p),p0);
function F = myfun(xdata,ydata,xstart,xend,p)
for i=1:length(xdata)
if (xdata(i) >= xstart) && (xdata(i) < xend)
F(i) = ydata(i)-(p(1)/p(2))*(1-exp(-p(2)(xdata(i)-xstart));
else
F(i)=ydata(i)-(p(3)*exp(-p(2)(xdata(i)-xend));
end
end
?
Best wishes
Torsten.
mdjupin
mdjupin 2015 年 2 月 24 日
Thanks Torsten, this does work. I would advise not to use more than two equations, because the fitting get harder.
Cheers.
mdjupin

サインインしてコメントする。

その他の回答 (1 件)

Joep
Joep 2015 年 2 月 17 日

0 投票

This is very simple solution which works in some cases one thing you need to noticed is the 1-based indexing of matlab.
a=10; b=20; c=-1; d=-3;
for x=1:a
t(x)=x;
y(x)=c*x;
end
for x=a:b
t(x)=t;
y(x)=d*x.^2;
end
figure
plot(t,y)

1 件のコメント

mdjupin
mdjupin 2015 年 2 月 17 日
Thanks Joep. Here I do not know a,b and c. The idea is that since both equations have b in common, I would like to know how fitting these equations I can get one single value for b. I already fitted these equations individually, but I obtained two values of b, which are different from each fit.

サインインしてコメントする。

質問済み:

2015 年 2 月 17 日

コメント済み:

2015 年 2 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by