3
$\begingroup$

I have some system for fitting my data:

fitModeluv = Piecewise[{{A0uv - Auv Exp[-(t/\[Tau])], 
 t < t0uv}, {B0uv + Buv Exp[-(t/\[Tau])], t >= t0uv}}, A0uv - Auv Exp[-(t0uv/\[Tau])] - Buv Exp[-(t0uv/\[Tau])]];

This system fits (green line) data (blue) like: enter image description here

2 questions:

  1. How can I connet 2 equations in one point (red points on the picture)?

enter image description here

  1. When I substituting values in the system (by Range[]), output is very awful and i can't minus this array from different list of values. How to fix it?
$\endgroup$
  • 1
    $\begingroup$ You need to put a restriction on the parameters so that they meet at a single point. Specifically you want t0uv = \[Tau] Log[(Auv + Buv)/(A0uv - B0uv)]. This is from equating the two pieces at t0uv. $\endgroup$ – JimB May 1 '17 at 19:53
4
$\begingroup$

You'll need to restrict the parameters so that the two curves meet at a single point:

t0uv = t0uv /. (Solve[A0uv - Auv Exp[-(t0uv/τ)] == B0uv + Buv Exp[-(t0uv/τ)],
 t0uv] /. C[1] -> 0)[[1]]
(* τ Log[(Auv + Buv)/(A0uv - B0uv)] *)

For example, first generate some data:

SeedRandom[1235];
A0uvInit = 49.273;
AuvInit = 13.273;
τInit = 100;
B0uvInit = 37.006;
BuvInit = 36.472;
t0uvInit = 140;
data = Table[{t, Piecewise[{{A0uvInit - AuvInit Exp[-(t/τInit)], t < t0uvInit}, 
  {B0uvInit + BuvInit Exp[-(t/τInit)], t >= t0uvInit}}]}, {t, 0, 250}];
data[[All, 2]] = data[[All, 2]] + RandomVariate[NormalDistribution[0, 1], 251];

Fit data to piecewise curve...

nlm = NonlinearModelFit[data,
   Piecewise[{{A0uv - Auv Exp[-(t/τ)], t < t0uv}, 
     {B0uv + Buv Exp[-(t/τ)], t >= t0uv}}],
   {{A0uv, A0uvInit}, {Auv, AuvInit}, {B0uv, B0uvInit}, {Buv, BuvInit}, {τ, τInit}}, t];

nlm["BestFitParameters"]
(* {A0uv -> 49.59797043559751, Auv -> 13.465589303011921,
    B0uv -> 36.869607720932656, Buv -> 34.24815518555274,
    τ -> 103.17795183868176} *)
t0uv /. nlm["BestFitParameters"]
(* 136.33797461279067 *)

Plot the results

Show[ListPlot[data], Plot[nlm[t], {t, 0, 250}]]

Data and curve fit

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.