#pragma rtGlobals=1 // Use modern global access method. print;timestamp() Macro timestamp() print Secs2Date(DateTime,0),Secs2Time(DateTime,1) End // ==================== Fitting functions ====================================== function cf_ln(coefs,t) // elimination, one-compartment model, logarithms 19/7/2021 wave coefs variable t return ln(coefs[0]*exp(-coefs[1]*t)) end function function cf_bo(coefs,t) // elimination, one-compartment model, 17/7/2021 wave coefs variable t return coefs[0]*exp(-coefs[1]*t) end function // models naming scheme: 1st letter is b, z, or f for bolus, zero-, or first-order absoprtion // 2nd letter is o or w for One or tWo compartment models // t or t1, t2, t3 for one or multiple termination times // _k are extensions indicating modification of two-compartment models // parameters are given as k10 and k12 instead of a and b (or l1 and l2), although the latter are the actual parameters used in the calculations function cf_fo0(coefs,t) // classical absorption-elimination, one-compartment model, ka = ke 20/6/2021 wave coefs // coefs[0] = F D / Vd, coefs[1]=k variable t return coefs[0]*coefs[1]*t*exp(-coefs[1]*t) end function function cf_fo0t(coefs,t) // truncated classical absorption-elimination, one-compartment model, ka = ke 20/6/2021 wave coefs // coefs[0] = F D / Vd, coefs[1]=k, coefs[2]=tau variable t variable y if (t<=coefs[2]) y = coefs[0]*coefs[1]*t*exp(-coefs[1]*t) else y = cf_fo0t(coefs,coefs[2])*exp(-coefs[1]*(t-coefs[2])) endif return y end function function cf_fo(coefs,t) // classical absorption-elimination, one-compartment model 7/4/2021 wave coefs // coefs[0] = F D / Vd, coefs[1]=ka, coefs[2]=kel variable t return coefs[0]*coefs[1]/(coefs[1]-coefs[2])*(exp(-coefs[2]*t)-exp(-coefs[1]*t)) end function function cf_fot(coefs,t) // truncated classical absorption-elimination, one-compartment model 25/4/2021 wave coefs // coefs[0] = F D / Vd, coefs[1]=ka, coefs[2]=kel, coefs[3]=tau variable t variable y if (t<=coefs[3]) y=coefs[0]*coefs[1]/(coefs[1]-coefs[2])*(exp(-coefs[2]*t)-exp(-coefs[1]*t)) else y=cf_fot(coefs,coefs[3])*exp(-coefs[2]*(t-coefs[3])) endif return y end function function cf_zot1(coefs,t) // finite, zero-order absorption, one-compartment elimination 16/3/2021 // changed to compact format on 7/4/2021 wave coefs // coefs[0] = F D / Vd, coefs[1]=kel, coefs[2]=tau variable t variable y if (t<=coefs[2]) y= coefs[0]/coefs[1]/coefs[2]*(1-exp(-coefs[1]*t)) // /coefs[2] added 10/4/2021 else y= cf_zot1(coefs,coefs[2])*exp(-coefs[1]*(t-coefs[2])) endif return y end function function cd_zot1(coefs,t) // finite, zero-order absorption, one-compartment elimination, time derivative of cf_zot1 2022-01-16 wave coefs // coefs[0] = F D / Vd, coefs[1]=kel, coefs[2]=tau variable t variable y if (t<=coefs[2]) y= coefs[0]/coefs[2]*exp(-coefs[1]*t) else y= -cf_zot1(coefs,coefs[2])*coefs[1]*exp(-coefs[1]*(t-coefs[2])) endif return y end function function cf_zot2(coefs,t) // finite, 2-stage, zero-order absorption, one-compartment elimination 5/4/2021 // 2021-04-07 compact syntax for previous segments // corrected expression for elimination of first term 7/4/2021 wave coefs variable t variable y if (t<=coefs[1]) y= coefs[0]/coefs[4]/coefs[1]*(1-exp(-coefs[4]*t)) // /coefs[1] added 10/4/2021 else if (t<=coefs[1]+coefs[3]) y= cf_zot2(coefs,coefs[1])*exp(-coefs[4]*(t-coefs[1])) + coefs[2]/coefs[4]/coefs[3]*(1-exp(-coefs[4]*(t-coefs[1]))) // /coefs[3] added 10/4/2021 else y= cf_zot2(coefs,coefs[1]+coefs[3])*exp(-coefs[4]*(t-coefs[1]-coefs[3])) endif endif return y end function function cf_zot3(coefs,t) // finite, 3-stage, zero-order absorption, one-compartment elimination 7/4/2021 wave coefs variable t variable y if (t<=coefs[1]) y= coefs[0]/coefs[6]/coefs[1]*(1-exp(-coefs[6]*t)) // /coefs[1] added 10/4/2021 else if (t<=coefs[1]+coefs[3]) y= cf_zot3(coefs,coefs[1])*exp(-coefs[6]*(t-coefs[1])) + coefs[2]/coefs[6]/coefs[3]*(1-exp(-coefs[6]*(t-coefs[1]))) // /coefs[3] added 10/4/2021 else if (t<=coefs[1]+coefs[3]+coefs[5]) y= cf_zot3(coefs,coefs[1]+coefs[3])*exp(-coefs[6]*(t-coefs[1]-coefs[3])) + coefs[4]/coefs[6]/coefs[5]*(1-exp(-coefs[6]*(t-coefs[1]-coefs[3]))) // /coefs[5] added 10/4/2021 else y= cf_zot3(coefs,coefs[1]+coefs[3]+coefs[5])*exp(-coefs[6]*(t-coefs[1]-coefs[3]-coefs[5])) endif endif endif return y end function function cd_zot3(coefs,t) // finite, 3-stage, zero-order absorption, one-compartment elimination time derivative 2022-01-16 wave coefs variable t variable y if (t<=coefs[1]) y= coefs[0]/coefs[1]*exp(-coefs[6]*t) else if (t<=coefs[1]+coefs[3]) y= -cf_zot3(coefs,coefs[1])*coefs[6]*exp(-coefs[6]*(t-coefs[1])) + coefs[2]/coefs[3]*exp(-coefs[6]*(t-coefs[1])) else if (t<=coefs[1]+coefs[3]+coefs[5]) y= -cf_zot3(coefs,coefs[1]+coefs[3])*coefs[6]*exp(-coefs[6]*(t-coefs[1]-coefs[3])) + coefs[4]/coefs[5]*exp(-coefs[6]*(t-coefs[1]-coefs[3])) else y= -cf_zot3(coefs,coefs[1]+coefs[3]+coefs[5])*coefs[6]*exp(-coefs[6]*(t-coefs[1]-coefs[3]-coefs[5])) endif endif endif return y end function function cf_zot4(coefs,t) // finite, 4-stage, zero-order absorption, one-compartment elimination 6/9/2022 wave coefs variable t variable y if (t<=coefs[1]) y= coefs[0]/coefs[8]/coefs[1]*(1-exp(-coefs[8]*t)) else if (t<=coefs[1]+coefs[3]) y= cf_zot4(coefs,coefs[1])*exp(-coefs[8]*(t-coefs[1])) + coefs[2]/coefs[8]/coefs[3]*(1-exp(-coefs[8]*(t-coefs[1]))) else if (t<=coefs[1]+coefs[3]+coefs[5]) y= cf_zot4(coefs,coefs[1]+coefs[3])*exp(-coefs[8]*(t-coefs[1]-coefs[3])) + coefs[4]/coefs[8]/coefs[5]*(1-exp(-coefs[8]*(t-coefs[1]-coefs[3]))) else if (t<=coefs[1]+coefs[3]+coefs[5]+coefs[7]) y= cf_zot4(coefs,coefs[1]+coefs[3]+coefs[5])*exp(-coefs[8]*(t-coefs[1]-coefs[3]-coefs[5])) + coefs[6]/coefs[8]/coefs[7]*(1-exp(-coefs[8]*(t-coefs[1]-coefs[3]-coefs[5]))) else y= cf_zot4(coefs,coefs[1]+coefs[3]+coefs[5]+coefs[7])*exp(-coefs[8]*(t-coefs[1]-coefs[3]-coefs[5]-coefs[7])) endif endif endif endif return y end function function cf_bwe(coefs,t) // double exponential decay 2022-01-27 wave coefs variable t variable y y= coefs[0]*exp(-coefs[2]*t)+coefs[1]*exp(-coefs[3]*t) return y end function function cf_bwet(coefs,t) // double exponential decay 2022-01-28 wave coefs variable t variable y y= coefs[0]*exp(-coefs[2]*(t-coefs[4]))+coefs[1]*exp(-coefs[3]*(t-coefs[4])) return y end function function cf_bw(coefs,t) // two-compartment elimination with peripheral calculation 2022-05-31 [improvement of 2021-08-18 version] wave coefs variable t return real(cf_bwcp(coefs,t)) end function/c cf_bwcp(coefs,t) // two-compartment elimination with peripheral calculation 2022-05-31 [improvement of 2021-08-18 version] wave coefs variable t variable k12=coefs[2]+coefs[3]-coefs[1]-coefs[2]*coefs[3]/coefs[1] variable y,z y= coefs[0]/(coefs[2]-coefs[3])*((coefs[1]-coefs[3])*exp(-coefs[3]*t) - (coefs[1]-coefs[2])*exp(-coefs[2]*t)) z= coefs[0]/(coefs[2]-coefs[3])*k12*(exp(-coefs[3]*t) - exp(-coefs[2]*t)) return cmplx(y,z) end function function cf_bw_k(coefs,t) // two-compartment elimination with peripheral calculation with k12 and k10 (not a and b) 2022-05-31 [improvement of 2021-08-18 version] wave coefs variable t return real(cf_bw_kcp(coefs,t)) end function/c cf_bw_kcp(coefs,t) // two-compartment elimination with peripheral calculation with k12 and k10 (not a and b) 2022-05-31 [improvement of 2021-08-18 version] wave coefs variable t variable k12=coefs[2],k10=coefs[3],k21=coefs[1] variable b=k21+k12+k10,c=k21*k10,d=sqrt(b*b-4*c),l1=0.5*(b-d),l2=0.5*(b+d) // b,c,d nomenclature from trinomial variable y,z y= coefs[0]/(l1-l2)*((k21-l2)*exp(-l2*t) - (k21-l1)*exp(-l1*t)) z= coefs[0]/(l1-l2)*k12*(exp(-l2*t) - exp(-l1*t)) return cmplx(y,z) end function function cf_zwt1(coefs,t) // finite, 1-stage, zero-order absorption, two-compartment elimination 6/12/2021 // old format without peripheral compartment calculation 9/4/2021, modified 20/7/2021 wave coefs variable t return real(cf_zwt1cp(coefs,t)) end function/c cf_zwt1cp(coefs,t) // finite, 1-stage, zero-order absorption, two-compartment elimination 17/11/2021 // xxxcp functions are used for two-compartment models. // they return both central (c) and peripheral (p) compartment concentrations in complex number format wave coefs variable t variable y,z,yt,zt,tt variable l1=coefs[3],l2=coefs[4],k21=coefs[2],k10=l1*l2/k21,k12=l1+l2-k21-k10 if (t<=coefs[1]) y= coefs[0]/coefs[1]/(coefs[4]-coefs[3])*((coefs[2]-coefs[3])/coefs[3]*(1-exp(-coefs[3]*t)) - (coefs[2]-coefs[4])/coefs[4]*(1-exp(-coefs[4]*t))) z= coefs[0]*k12/coefs[1]/(coefs[4]-coefs[3])*(1/coefs[3]*(1-exp(-coefs[3]*t)) - 1/coefs[4]*(1-exp(-coefs[4]*t))) else yt=real(cf_zwt1cp(coefs,coefs[1])) zt=imag(cf_zwt1cp(coefs,coefs[1])) tt=t-coefs[1] y=1/(coefs[4]-coefs[3])*(coefs[2]*(yt+zt)*(exp(-coefs[3]*tt)-exp(-coefs[4]*tt))-yt*(coefs[3]*exp(-coefs[3]*tt)-coefs[4]*exp(-coefs[4]*tt))) z=(yt+coefs[2]/(coefs[2]-coefs[3])*zt)*exp(-coefs[3]*tt)-(yt+coefs[2]/(coefs[2]-coefs[4])*zt)*exp(-coefs[4]*tt) z*=k12/(coefs[4]-coefs[3]) endif return cmplx(y,z) end function function cf_zwt1_k(coefs,t) // finite, 1-stage, zero-order absorption, two-compartment elimination with ks 2021-12-22 // old format without peripheral compartment calcuation: finite, 1-stage, zero-order absorption, two-compartment elimination (k12,k10, not l1,l2) 16/8/2021 wave coefs variable t return real(cf_zwt1_kcp(coefs,t)) end function/c cf_zwt1_kcp(coefs,t) // finite, 1-stage, zero-order absorption, two-compartment elimination with k12, k10 2021-12-22 wave coefs variable t variable y,z,yt,zt,tt variable k21=coefs[2],k12=coefs[3],k10=coefs[4],t1=coefs[1] variable b=coefs[2]+coefs[3]+coefs[4],c=coefs[2]*coefs[4],d=sqrt(b*b-4*c),l1=0.5*(b-d),l2=0.5*(b+d) // b,c,d nomenclature from trinomial if (t<=t1) y= coefs[0]/t1/(l2-l1)*((k21-l1)/l1*(1-exp(-l1*t)) - (k21-l2)/l2*(1-exp(-l2*t))) z= coefs[0]*k12/t1/(l2-l1)*(1/l1*(1-exp(-l1*t)) - 1/l2*(1-exp(-l2*t))) else yt=real(cf_zwt1_kcp(coefs,t1)) zt=imag(cf_zwt1_kcp(coefs,t1)) tt=t-t1 y=1/(l2-l1)*(k21*(yt+zt)*(exp(-l1*tt)-exp(-l2*tt))-yt*(l1*exp(-l1*tt)-l2*exp(-l2*tt))) z=(yt+k21/(k21-l1)*zt)*exp(-l1*tt)-(yt+k21/(coefs[2]-l2)*zt)*exp(-l2*tt) z*=k12/(l2-l1) endif return cmplx(y,z) end function function cf_zwt2(coefs,t) // finite, 2-stage, zero-order absorption, two-compartment elimination 5/12/2021 wave coefs variable t return real(cf_zwt2cp(coefs,t)) end function/c cf_zwt2cp(coefs,t) // finite, 2-stage, zero-order absorption, two-compartment elimination 5/12/2021 wave coefs variable t variable y,z,yt,zt,tt variable l1=coefs[3],l2=coefs[4],k21=coefs[2],k10=l1*l2/k21,k12=l1+l2-k21-k10 if (t<=coefs[1]) y= coefs[0]/coefs[1]/(coefs[4]-coefs[3])*((coefs[2]-coefs[3])/coefs[3]*(1-exp(-coefs[3]*t)) - (coefs[2]-coefs[4])/coefs[4]*(1-exp(-coefs[4]*t))) z= coefs[0]*k12/coefs[1]/(coefs[4]-coefs[3])*(1/coefs[3]*(1-exp(-coefs[3]*t)) - 1/coefs[4]*(1-exp(-coefs[4]*t))) else if (t<=coefs[1]+coefs[6]) yt=real(cf_zwt2cp(coefs,coefs[1])) zt=imag(cf_zwt2cp(coefs,coefs[1])) tt=t-coefs[1] y=yt*(coefs[4]*exp(-coefs[4]*tt)-coefs[3]*exp(-coefs[3]*tt))+coefs[2]*(yt+zt)*(exp(-coefs[3]*tt)-exp(-coefs[4]*tt)) y+=coefs[5]/coefs[6]*((coefs[2]/coefs[3]-1)*(1-exp(-coefs[3]*tt))-(coefs[2]/coefs[4]-1)*(1-exp(-coefs[4]*tt))) y/=coefs[4]-coefs[3] z=(yt+coefs[2]/(coefs[2]-coefs[3])*zt)*exp(-coefs[3]*tt)-(yt+coefs[2]/(coefs[2]-coefs[4])*zt)*exp(-coefs[4]*tt) z+=coefs[5]/coefs[6]*((1-exp(-coefs[3]*tt))/coefs[3]-(1-exp(-coefs[4]*tt))/coefs[4]) z*=k12/(coefs[4]-coefs[3]) else yt=real(cf_zwt2cp(coefs,coefs[1]+coefs[6])) zt=imag(cf_zwt2cp(coefs,coefs[1]+coefs[6])) tt=t-coefs[1]-coefs[6] y=yt*(coefs[4]*exp(-coefs[4]*tt)-coefs[3]*exp(-coefs[3]*tt))+coefs[2]*(yt+zt)*(exp(-coefs[3]*tt)-exp(-coefs[4]*tt)) y/=coefs[4]-coefs[3] z=(yt+coefs[2]/(coefs[2]-coefs[3])*zt)*exp(-coefs[3]*tt)-(yt+coefs[2]/(coefs[2]-coefs[4])*zt)*exp(-coefs[4]*tt) z*=k12/(coefs[4]-coefs[3]) endif endif return cmplx(y,z) end function function cf_zwt2_k(coefs,t) // finite, 2-stage, zero-order absorption, two-compartment elimination 5/12/2021 wave coefs variable t return real(cf_zwt2_kcp(coefs,t)) end function/c cf_zwt2_kcp(coefs,t) // finite, 2-stage, zero-order absorption, two-compartment elimination 5/12/2021, with ks 2021-12-22 wave coefs variable t variable y,z,yt,zt,tt variable k21=coefs[2],k12=coefs[3],k10=coefs[4],t1=coefs[1],t2=coefs[6] variable b=coefs[2]+coefs[3]+coefs[4],c=coefs[2]*coefs[4],d=sqrt(b*b-4*c),l1=0.5*(b-d),l2=0.5*(b+d) // b,c,d nomenclature from trinomial if (t<=t1) y= coefs[0]/t1/(l2-l1)*((k21-l1)/l1*(1-exp(-l1*t)) - (k21-l2)/l2*(1-exp(-l2*t))) z= coefs[0]*k12/t1/(l2-l1)*(1/l1*(1-exp(-l1*t)) - 1/l2*(1-exp(-l2*t))) else if (t<=t1+t2) yt=real(cf_zwt2_kcp(coefs,t1)) zt=imag(cf_zwt2_kcp(coefs,t1)) tt=t-t1 y=yt*(l2*exp(-l2*tt)-l1*exp(-l1*tt))+k21*(yt+zt)*(exp(-l1*tt)-exp(-l2*tt)) y+=coefs[5]/t2*((k21/l1-1)*(1-exp(-l1*tt))-(k21/l2-1)*(1-exp(-l2*tt))) y/=l2-l1 z=(yt+k21/(k21-l1)*zt)*exp(-l1*tt)-(yt+k21/(k21-l2)*zt)*exp(-l2*tt) z+=coefs[5]/t2*((1-exp(-l1*tt))/l1-(1-exp(-l2*tt))/l2) z*=k12/(l2-l1) else yt=real(cf_zwt2_kcp(coefs,t1+t2)) zt=imag(cf_zwt2_kcp(coefs,t1+t2)) tt=t-t1-t2 y=yt*(l2*exp(-l2*tt)-l1*exp(-l1*tt))+k21*(yt+zt)*(exp(-l1*tt)-exp(-l2*tt)) y/=l2-l1 z=(yt+k21/(k21-l1)*zt)*exp(-l1*tt)-(yt+k21/(k21-l2)*zt)*exp(-l2*tt) z*=k12/(l2-l1) endif endif return cmplx(y,z) end function function cf_zwt3(coefs,t) // finite, 3-stage, zero-order absorption, two-compartment elimination 5/12/2021 wave coefs variable t return real(cf_zwt3cp(coefs,t)) end function/c cf_zwt3cp(coefs,t) // finite, 3-stage, zero-order absorption, two-compartment elimination 5/12/2021 wave coefs variable t variable y,z,yt,zt,tt variable l1=coefs[3],l2=coefs[4],k21=coefs[2],k10=l1*l2/k21,k12=l1+l2-k21-k10 if (t<=coefs[1]) y= coefs[0]/coefs[1]/(coefs[4]-coefs[3])*((coefs[2]-coefs[3])/coefs[3]*(1-exp(-coefs[3]*t)) - (coefs[2]-coefs[4])/coefs[4]*(1-exp(-coefs[4]*t))) z= coefs[0]*k12/coefs[1]/(coefs[4]-coefs[3])*(1/coefs[3]*(1-exp(-coefs[3]*t)) - 1/coefs[4]*(1-exp(-coefs[4]*t))) else if (t<=coefs[1]+coefs[6]) yt=real(cf_zwt3cp(coefs,coefs[1])) zt=imag(cf_zwt3cp(coefs,coefs[1])) tt=t-coefs[1] y=yt*(coefs[4]*exp(-coefs[4]*tt)-coefs[3]*exp(-coefs[3]*tt))+coefs[2]*(yt+zt)*(exp(-coefs[3]*tt)-exp(-coefs[4]*tt)) y+=coefs[5]/coefs[6]*((coefs[2]/coefs[3]-1)*(1-exp(-coefs[3]*tt))-(coefs[2]/coefs[4]-1)*(1-exp(-coefs[4]*tt))) y/=coefs[4]-coefs[3] z=(yt+coefs[2]/(coefs[2]-coefs[3])*zt)*exp(-coefs[3]*tt)-(yt+coefs[2]/(coefs[2]-coefs[4])*zt)*exp(-coefs[4]*tt) z+=coefs[5]/coefs[6]*((1-exp(-coefs[3]*tt))/coefs[3]-(1-exp(-coefs[4]*tt))/coefs[4]) z*=k12/(coefs[4]-coefs[3]) else if (t<=coefs[1]+coefs[6]+coefs[8]) yt=real(cf_zwt3cp(coefs,coefs[1]+coefs[6])) zt=imag(cf_zwt3cp(coefs,coefs[1]+coefs[6])) tt=t-coefs[1]-coefs[6] y=yt*(coefs[4]*exp(-coefs[4]*tt)-coefs[3]*exp(-coefs[3]*tt))+coefs[2]*(yt+zt)*(exp(-coefs[3]*tt)-exp(-coefs[4]*tt)) y+=coefs[7]/coefs[8]*((coefs[2]/coefs[3]-1)*(1-exp(-coefs[3]*tt))-(coefs[2]/coefs[4]-1)*(1-exp(-coefs[4]*tt))) y/=coefs[4]-coefs[3] z=(yt+coefs[2]/(coefs[2]-coefs[3])*zt)*exp(-coefs[3]*tt)-(yt+coefs[2]/(coefs[2]-coefs[4])*zt)*exp(-coefs[4]*tt) z+=coefs[7]/coefs[8]*((1-exp(-coefs[3]*tt))/coefs[3]-(1-exp(-coefs[4]*tt))/coefs[4]) z*=k12/(coefs[4]-coefs[3]) else yt=real(cf_zwt3cp(coefs,coefs[1]+coefs[6]+coefs[8])) zt=imag(cf_zwt3cp(coefs,coefs[1]+coefs[6]+coefs[8])) tt=t-coefs[1]-coefs[6]-coefs[8] y=yt*(coefs[4]*exp(-coefs[4]*tt)-coefs[3]*exp(-coefs[3]*tt))+coefs[2]*(yt+zt)*(exp(-coefs[3]*tt)-exp(-coefs[4]*tt)) y/=coefs[4]-coefs[3] z=(yt+coefs[2]/(coefs[2]-coefs[3])*zt)*exp(-coefs[3]*tt)-(yt+coefs[2]/(coefs[2]-coefs[4])*zt)*exp(-coefs[4]*tt) z*=k12/(coefs[4]-coefs[3]) endif endif endif return cmplx(y,z) end function function cf_zwt3_k(coefs,t) // finite, 3-stage, zero-order absorption, two-compartment elimination 5/12/2021 with ks 2021-12-22 wave coefs variable t return real(cf_zwt3_kcp(coefs,t)) end function/c cf_zwt3_kcp(coefs,t) // finite, 3-stage, zero-order absorption, two-compartment elimination 5/12/2021 with ks 2021-12-22 wave coefs variable t variable y,z,yt,zt,tt variable k21=coefs[2],k12=coefs[3],k10=coefs[4],t1=coefs[1],t2=coefs[6],t3=coefs[8] variable b=coefs[2]+coefs[3]+coefs[4],c=coefs[2]*coefs[4],d=sqrt(b*b-4*c),l1=0.5*(b-d),l2=0.5*(b+d) // b,c,d nomenclature from trinomial if (t<=t1) y= coefs[0]/t1/(l2-l1)*((k21-l1)/l1*(1-exp(-l1*t)) - (k21-l2)/l2*(1-exp(-l2*t))) z= coefs[0]*k12/t1/(l2-l1)*(1/l1*(1-exp(-l1*t)) - 1/l2*(1-exp(-l2*t))) else if (t<=t1+t2) yt=real(cf_zwt3_kcp(coefs,t1)) zt=imag(cf_zwt3_kcp(coefs,t1)) tt=t-t1 y=yt*(l2*exp(-l2*tt)-l1*exp(-l1*tt))+k21*(yt+zt)*(exp(-l1*tt)-exp(-l2*tt)) y+=coefs[5]/t2*((k21/l1-1)*(1-exp(-l1*tt))-(k21/l2-1)*(1-exp(-l2*tt))) y/=l2-l1 z=(yt+k21/(k21-l1)*zt)*exp(-l1*tt)-(yt+k21/(k21-l2)*zt)*exp(-l2*tt) z+=coefs[5]/t2*((1-exp(-l1*tt))/l1-(1-exp(-l2*tt))/l2) z*=k12/(l2-l1) else if (t<=t1+t2+t3) yt=real(cf_zwt3_kcp(coefs,t1+t2)) zt=imag(cf_zwt3_kcp(coefs,t1+t2)) tt=t-t1-t2 y=yt*(l2*exp(-l2*tt)-l1*exp(-l1*tt))+k21*(yt+zt)*(exp(-l1*tt)-exp(-l2*tt)) y+=coefs[7]/t3*((k21/l1-1)*(1-exp(-l1*tt))-(k21/l2-1)*(1-exp(-l2*tt))) y/=l2-l1 z=(yt+k21/(k21-l1)*zt)*exp(-l1*tt)-(yt+k21/(k21-l2)*zt)*exp(-l2*tt) z+=coefs[7]/t3*((1-exp(-l1*tt))/l1-(1-exp(-l2*tt))/l2) z*=k12/(l2-l1) else yt=real(cf_zwt3_kcp(coefs,t1+t2+t3)) zt=imag(cf_zwt3_kcp(coefs,t1+t2+t3)) tt=t-t1-t2-t3 y=yt*(l2*exp(-l2*tt)-l1*exp(-l1*tt))+k21*(yt+zt)*(exp(-l1*tt)-exp(-l2*tt)) y/=l2-l1 z=(yt+k21/(k21-l1)*zt)*exp(-l1*tt)-(yt+k21/(k21-l2)*zt)*exp(-l2*tt) z*=k12/(l2-l1) endif endif endif return cmplx(y,z) end function function cf_zwt4(coefs,t) // finite, 4-stage, zero-order absorption, two-compartment elimination 6/9/2022 wave coefs variable t return real(cf_zwt4cp(coefs,t)) end function/c cf_zwt4cp(coefs,t) // finite, 4-stage, zero-order absorption, two-compartment elimination 6/9/2022 wave coefs variable t variable y,z,yt,zt,tt variable l1=coefs[3],l2=coefs[4],k21=coefs[2],k10=l1*l2/k21,k12=l1+l2-k21-k10 if (t<=coefs[1]) y= coefs[0]/coefs[1]/(coefs[4]-coefs[3])*((coefs[2]-coefs[3])/coefs[3]*(1-exp(-coefs[3]*t)) - (coefs[2]-coefs[4])/coefs[4]*(1-exp(-coefs[4]*t))) z= coefs[0]*k12/coefs[1]/(coefs[4]-coefs[3])*(1/coefs[3]*(1-exp(-coefs[3]*t)) - 1/coefs[4]*(1-exp(-coefs[4]*t))) else if (t<=coefs[1]+coefs[6]) yt=real(cf_zwt4cp(coefs,coefs[1])) zt=imag(cf_zwt4cp(coefs,coefs[1])) tt=t-coefs[1] y=yt*(coefs[4]*exp(-coefs[4]*tt)-coefs[3]*exp(-coefs[3]*tt))+coefs[2]*(yt+zt)*(exp(-coefs[3]*tt)-exp(-coefs[4]*tt)) y+=coefs[5]/coefs[6]*((coefs[2]/coefs[3]-1)*(1-exp(-coefs[3]*tt))-(coefs[2]/coefs[4]-1)*(1-exp(-coefs[4]*tt))) y/=coefs[4]-coefs[3] z=(yt+coefs[2]/(coefs[2]-coefs[3])*zt)*exp(-coefs[3]*tt)-(yt+coefs[2]/(coefs[2]-coefs[4])*zt)*exp(-coefs[4]*tt) z+=coefs[5]/coefs[6]*((1-exp(-coefs[3]*tt))/coefs[3]-(1-exp(-coefs[4]*tt))/coefs[4]) z*=k12/(coefs[4]-coefs[3]) else if (t<=coefs[1]+coefs[6]+coefs[8]) yt=real(cf_zwt4cp(coefs,coefs[1]+coefs[6])) zt=imag(cf_zwt4cp(coefs,coefs[1]+coefs[6])) tt=t-coefs[1]-coefs[6] y=yt*(coefs[4]*exp(-coefs[4]*tt)-coefs[3]*exp(-coefs[3]*tt))+coefs[2]*(yt+zt)*(exp(-coefs[3]*tt)-exp(-coefs[4]*tt)) y+=coefs[7]/coefs[8]*((coefs[2]/coefs[3]-1)*(1-exp(-coefs[3]*tt))-(coefs[2]/coefs[4]-1)*(1-exp(-coefs[4]*tt))) y/=coefs[4]-coefs[3] z=(yt+coefs[2]/(coefs[2]-coefs[3])*zt)*exp(-coefs[3]*tt)-(yt+coefs[2]/(coefs[2]-coefs[4])*zt)*exp(-coefs[4]*tt) z+=coefs[7]/coefs[8]*((1-exp(-coefs[3]*tt))/coefs[3]-(1-exp(-coefs[4]*tt))/coefs[4]) z*=k12/(coefs[4]-coefs[3]) else if (t<=coefs[1]+coefs[6]+coefs[8]+coefs[10]) yt=real(cf_zwt4cp(coefs,coefs[1]+coefs[6]+coefs[8])) zt=imag(cf_zwt4cp(coefs,coefs[1]+coefs[6]+coefs[8])) tt=t-coefs[1]-coefs[6]-coefs[8] y=yt*(coefs[4]*exp(-coefs[4]*tt)-coefs[3]*exp(-coefs[3]*tt))+coefs[2]*(yt+zt)*(exp(-coefs[3]*tt)-exp(-coefs[4]*tt)) y+=coefs[9]/coefs[10]*((coefs[2]/coefs[3]-1)*(1-exp(-coefs[3]*tt))-(coefs[2]/coefs[4]-1)*(1-exp(-coefs[4]*tt))) y/=coefs[4]-coefs[3] z=(yt+coefs[2]/(coefs[2]-coefs[3])*zt)*exp(-coefs[3]*tt)-(yt+coefs[2]/(coefs[2]-coefs[4])*zt)*exp(-coefs[4]*tt) z+=coefs[9]/coefs[10]*((1-exp(-coefs[3]*tt))/coefs[3]-(1-exp(-coefs[4]*tt))/coefs[4]) z*=k12/(coefs[4]-coefs[3]) else yt=real(cf_zwt4cp(coefs,coefs[1]+coefs[6]+coefs[8]+coefs[10])) zt=imag(cf_zwt4cp(coefs,coefs[1]+coefs[6]+coefs[8]+coefs[10])) tt=t-coefs[1]-coefs[6]-coefs[8]-coefs[10] y=yt*(coefs[4]*exp(-coefs[4]*tt)-coefs[3]*exp(-coefs[3]*tt))+coefs[2]*(yt+zt)*(exp(-coefs[3]*tt)-exp(-coefs[4]*tt)) y/=coefs[4]-coefs[3] z=(yt+coefs[2]/(coefs[2]-coefs[3])*zt)*exp(-coefs[3]*tt)-(yt+coefs[2]/(coefs[2]-coefs[4])*zt)*exp(-coefs[4]*tt) z*=k12/(coefs[4]-coefs[3]) endif endif endif endif return cmplx(y,z) end function function cf_zwt4_k(coefs,t) // finite, 4-stage, zero-order absorption, two-compartment elimination with ks 2022-09-06 wave coefs variable t return real(cf_zwt4_kcp(coefs,t)) end function/c cf_zwt4_kcp(coefs,t) // finite, 4-stage, zero-order absorption, two-compartment elimination with ks 2022-09-06 wave coefs variable t variable y,z,yt,zt,tt variable k21=coefs[2],k12=coefs[3],k10=coefs[4],t1=coefs[1],t2=coefs[6],t3=coefs[8],t4=coefs[10] variable b=coefs[2]+coefs[3]+coefs[4],c=coefs[2]*coefs[4],d=sqrt(b*b-4*c),l1=0.5*(b-d),l2=0.5*(b+d) // b,c,d nomenclature from trinomial if (t<=t1) y= coefs[0]/t1/(l2-l1)*((k21-l1)/l1*(1-exp(-l1*t)) - (k21-l2)/l2*(1-exp(-l2*t))) z= coefs[0]*k12/t1/(l2-l1)*(1/l1*(1-exp(-l1*t)) - 1/l2*(1-exp(-l2*t))) else if (t<=t1+t2) yt=real(cf_zwt4_kcp(coefs,t1)) zt=imag(cf_zwt4_kcp(coefs,t1)) tt=t-t1 y=yt*(l2*exp(-l2*tt)-l1*exp(-l1*tt))+k21*(yt+zt)*(exp(-l1*tt)-exp(-l2*tt)) y+=coefs[5]/t2*((k21/l1-1)*(1-exp(-l1*tt))-(k21/l2-1)*(1-exp(-l2*tt))) y/=l2-l1 z=(yt+k21/(k21-l1)*zt)*exp(-l1*tt)-(yt+k21/(k21-l2)*zt)*exp(-l2*tt) z+=coefs[5]/t2*((1-exp(-l1*tt))/l1-(1-exp(-l2*tt))/l2) z*=k12/(l2-l1) else if (t<=t1+t2+t3) yt=real(cf_zwt4_kcp(coefs,t1+t2)) zt=imag(cf_zwt4_kcp(coefs,t1+t2)) tt=t-t1-t2 y=yt*(l2*exp(-l2*tt)-l1*exp(-l1*tt))+k21*(yt+zt)*(exp(-l1*tt)-exp(-l2*tt)) y+=coefs[7]/t3*((k21/l1-1)*(1-exp(-l1*tt))-(k21/l2-1)*(1-exp(-l2*tt))) y/=l2-l1 z=(yt+k21/(k21-l1)*zt)*exp(-l1*tt)-(yt+k21/(k21-l2)*zt)*exp(-l2*tt) z+=coefs[7]/t3*((1-exp(-l1*tt))/l1-(1-exp(-l2*tt))/l2) z*=k12/(l2-l1) else if (t<=t1+t2+t3+t4) yt=real(cf_zwt4_kcp(coefs,t1+t2+t3)) zt=imag(cf_zwt4_kcp(coefs,t1+t2+t3)) tt=t-t1-t2-t3 y=yt*(l2*exp(-l2*tt)-l1*exp(-l1*tt))+k21*(yt+zt)*(exp(-l1*tt)-exp(-l2*tt)) y+=coefs[9]/t4*((k21/l1-1)*(1-exp(-l1*tt))-(k21/l2-1)*(1-exp(-l2*tt))) y/=l2-l1 z=(yt+k21/(k21-l1)*zt)*exp(-l1*tt)-(yt+k21/(k21-l2)*zt)*exp(-l2*tt) z+=coefs[9]/t4*((1-exp(-l1*tt))/l1-(1-exp(-l2*tt))/l2) z*=k12/(l2-l1) else yt=real(cf_zwt4_kcp(coefs,t1+t2+t3+t4)) zt=imag(cf_zwt4_kcp(coefs,t1+t2+t3+t4)) tt=t-t1-t2-t3-t4 y=yt*(l2*exp(-l2*tt)-l1*exp(-l1*tt))+k21*(yt+zt)*(exp(-l1*tt)-exp(-l2*tt)) y/=l2-l1 z=(yt+k21/(k21-l1)*zt)*exp(-l1*tt)-(yt+k21/(k21-l2)*zt)*exp(-l2*tt) z*=k12/(l2-l1) endif endif endif endif return cmplx(y,z) end function function cf_fw(coefs,t) // 1-stage, first-order absorption, two-compartment elimination 8/6/2021 wave coefs variable t return real(cf_fwcp(coefs,t)) end function function cf_fw_k(coefs,t) // 1-stage, first-order absorption, two-compartment elimination with k12 and k10 22/8/2021 wave coefs variable t return real(cf_fw_kcp(coefs,t)) end function function/c cf_fwcp(coefs,t) // 1-stage, first-order absorption, two-compartment elimination 10/6/2021 with calculation for peripheral compartment wave coefs variable t variable y,z,B1,B2,B3,l1=coefs[3],l2=coefs[4],ka=coefs[1],k21=coefs[2],k10=l1*l2/k21,k12=l1+l2-k21-k10 B1=coefs[0]*ka*(k21-l1)/(l2-l1)/(ka-l1) B2=coefs[0]*ka*(k21-l2)/(ka-l2)/(l1-l2) B3=coefs[0]*ka*(k21-ka)/(l1-ka)/(l2-ka) y=B1*exp(-l1*t)+B2*exp(-l2*t)+B3*exp(-ka*t) z=(-(l1*B1*exp(-l1*t)+l2*B2*exp(-l2*t)+ka*B3*exp(-ka*t))-ka*coefs[0]*exp(-ka*t)+(k12+k10)*y)/k21 return cmplx(y,z) end function function/c cf_fw_kcp(coefs,t) // 1-stage, first-order absorption, two-compartment elimination with k12 and k10 22/8/2021 wave coefs variable t variable y,z,B1,B2,B3,ka=coefs[1],k21=coefs[2],k12=coefs[3],k10=coefs[4] variable b=k12+k21+k10,c=k21*k10,d=sqrt(b*b-4*c),l1=0.5*(b+d),l2=0.5*(b-d) // b,c,d nomenclature from trinomial B1=coefs[0]*ka*(k21-l1)/(l2-l1)/(ka-l1) B2=coefs[0]*ka*(k21-l2)/(ka-l2)/(l1-l2) B3=coefs[0]*ka*(k21-ka)/(l1-ka)/(l2-ka) y=B1*exp(-l1*t)+B2*exp(-l2*t)+B3*exp(-ka*t) z=(-(l1*B1*exp(-l1*t)+l2*B2*exp(-l2*t)+ka*B3*exp(-ka*t))-ka*coefs[0]*exp(-ka*t)+(k12+k10)*y)/k21 return cmplx(y,z) end function function cf_fwt(coefs,t) // finite, 1-stage, first-order absorption, two-compartment elimination 21/7/2021 wave coefs variable t return real(cf_fwtcp(coefs,t)) end function function/c cf_fwtcp(coefs,t) // finite, 1-stage, first-order absorption, two-compartment elimination 21/7/2021 with calculation for peripheral compartment wave coefs variable t variable y,yt,zt,z,tt,B1,B2,B3,l1=coefs[3],l2=coefs[4],ka=coefs[1],k21=coefs[2],k10=l1*l2/k21,k12=l1+l2-k21-k10,A1,A2 if (t<=coefs[5]) B1=coefs[0]*ka*(k21-l1)/(l2-l1)/(ka-l1) B2=coefs[0]*ka*(k21-l2)/(ka-l2)/(l1-l2) B3=coefs[0]*ka*(k21-ka)/(l1-ka)/(l2-ka) y=B1*exp(-l1*t)+B2*exp(-l2*t)+B3*exp(-ka*t) z=(-(l1*B1*exp(-l1*t)+l2*B2*exp(-l2*t)+ka*B3*exp(-ka*t))-ka*coefs[0]*exp(-ka*t)+(k12+k10)*y)/k21 else yt=real(cf_fwtcp(coefs,coefs[5])) zt=imag(cf_fwtcp(coefs,coefs[5])) tt=t-coefs[5] A1=((k21-l1)*yt+k21*zt)/(l2-l1) A2=((k21-l2)*yt+k21*zt)/(l1-l2) y=A1*exp(-l1*tt)+A2*exp(-l2*tt) z=(yt+k21/(k21-l1)*zt)*exp(-l1*tt)-(yt+k21/(k21-l2)*zt)*exp(-l2*tt) z*=k12/(l2-l1) endif return cmplx(y,z) end function function cf_fwt_k(coefs,t) // finite, 1-stage, first-order absorption, two-compartment elimination with k12 and k10 (not l1,l2) 16/8/2021 wave coefs variable t return real(cf_fwt_kcp(coefs,t)) end function function/c cf_fwt_kcp(coefs,t) // finite, 1-stage, first-order absorption, two-compartment elimination with k12 and k10 (not l1, l2) 16/8/2021 with calculation for peripheral compartment wave coefs variable t variable y,z,yt,zt,tt,B1,B2,B3,A1,A2,ka=coefs[1],k21=coefs[2],k12=coefs[3],k10=coefs[4] variable b=k12+k21+k10,c=k21*k10,d=sqrt(b*b-4*c),l1=0.5*(b+d),l2=0.5*(b-d) // b,c,d nomenclature from trinomial if (t<=coefs[5]) B1=coefs[0]*ka*(k21-l1)/(l2-l1)/(ka-l1) B2=coefs[0]*ka*(k21-l2)/(ka-l2)/(l1-l2) B3=coefs[0]*ka*(k21-ka)/(l1-ka)/(l2-ka) y=B1*exp(-l1*t)+B2*exp(-l2*t)+B3*exp(-ka*t) z=(-(l1*B1*exp(-l1*t)+l2*B2*exp(-l2*t)+ka*B3*exp(-ka*t))-ka*coefs[0]*exp(-ka*t)+(k12+k10)*y)/k21 else yt=real(cf_fwt_kcp(coefs,coefs[5])) zt=imag(cf_fwt_kcp(coefs,coefs[5])) tt=t-coefs[5] A1=((k21-l1)*yt+k21*zt)/(l2-l1) A2=((k21-l2)*yt+k21*zt)/(l1-l2) y=A1*exp(-l1*tt)+A2*exp(-l2*tt) z=(yt+k21/(k21-l1)*zt)*exp(-l1*tt)-(yt+k21/(k21-l2)*zt)*exp(-l2*tt) z*=k12/(l2-l1) endif return cmplx(y,z) end function function cf_zzt2(coefs,t) // zero order absorption and elimination for duration tau1 and tau2 wave coefs variable t variable y if (t<=coefs[1]) y=coefs[0]*t/coefs[1] else if (t<=coefs[1]+coefs[2]) y=coefs[0]*(1-(t-coefs[1])/coefs[2]) else y=0 endif endif return y end function // +++++++++++++++ fitting tools +++++++++++++++++++++++ Function chisq(x,y,i1,i2) // 2021-12-29 wave x,y variable i1,i2 return real(chisq_corrcoef(x,y,i1,i2)) end Function corrcoef(x,y,i1,i2) // correlation coefficient for fit (improvised) 2021-04-09 wave x,y variable i1,i2 return imag(chisq_corrcoef(x,y,i1,i2)) end Function/c chisq_corrcoef(x,y,i1,i2) // correlation coefficient for fit (improvised) 2021-04-09, turned complex 2021-12-29 to include ss wave x,y variable i1,i2 variable i=i1,ss=0,sxy=0,sx2=0,sy2=0,xm=mean(x,i1,i2),ym=mean(y,i1,i2) do ss+=(x[i]-y[i])^2 sxy+=(x[i]-xm)*(y[i]-ym) sx2+=(x[i]-xm)^2 sy2+=(y[i]-ym)^2 i+=1 while (ic_max) c_max=c_data[i] t_max=t_data[i] i_max=i endif i+=1 while (ic_max) c_max=c_data[i] t_max=t_data[i] i_max=i endif i+=1 while (i 0) i_start_dc=pcsr(B) else i_start_dc=i_max+2 endif CurveFit/Q/M=2/W=0 line, dc_data[i_start_dc,]/X=c_data/D // fit dc/dt = f(c) duplicate/o dc_data dcs_data wavestats/q dc_data setaxis left V_min*1.1,0 dcs_data=poly(W_coef,c_data) cc=corrcoef(dcs_data,dc_data,i_start_dc,numpnts(c_data)-1) fitlabel="\f02k\f00\\Bel\\M = "+num2str(-W_coef[1])+" ± "+num2str(W_sigma[1])+" "+t_unit+"\S-1\M" fitlabel+="\rd\\f02C\\f00/d\f02t\\f00 = "+num2str(W_coef[0])+" ± "+num2str(W_sigma[0])+" "+c_unit+" "+t_unit fitlabel+="\r\\f02?\\f00\\S2\\M = "+num2str(V_chisq)+", \\f02R\\f00\S2\M = "+num2str(cc) TextBox/C/N=CF_dc_data fitlabel // print W_coef[0]," ±", W_sigma[0],W_coef[1]," ±", W_sigma[1], lbl // sprintf templbl " kel = %.3f (±%.3f) "+t_unit+"^-1 %s",-W_coef[1],W_sigma[1],lbl End Proc fitp(mdl,flag) // 2021-08-17 main fitting procedure string mdl // fo,fot,fo0,fo0t,zot1,zot2,zot3,zwt1_k,bo ,bw, bw_k, etc. variable flag // 0, 1, 2 string graph="Gcf_"+mdl,table="Tcf_"+mdl,fit="fit_"+mdl,res="res_"+mdl,H="H_"+mdl,cf="cf_"+mdl string par="par_"+mdl,yt="yt_"+mdl,parl="parl_"+mdl,parh="parh_"+mdl variable tau string cflag // 2022-01-06 for showing in output the use of fitting constraints string c_unit=StringFromList(0,note(c_data)) string t_unit=StringFromList(1,note(c_data)) DoWindow/F $graph label left "\f02C\f00 (\E"+c_unit+")" label bottom "\f02t\f00 ("+t_unit+")" TextBox/C/N=text0 data_label variable i=0 holdflags="" do holdflags+=num2str($H[i]) i+=1 while (i0)*$H[j] if ((flag==2) && (exists(parh)==1)) // fitting with constraints, if high parameter limits are defined make/o/t/n=(2*(numpnts($par)-sum($H))) T_par_constr // 2021-12-30 i=0 j=0 do if ($H[j]==0) // 2022-01-05 to constrain parameters than are not held constant T_par_constr[i]="K"+num2str(j)+" > 0" T_par_constr[i+numpnts($par)-sum($H)]="K"+num2str(j)+" < "+num2str($parh[j]) i+=1 endif j+=1 while (j0) TextBox/C/N=CF_c_data fitlabel endif if(tau>0) make/o/n=1 $yt=$cf($par,tau) SetScale/P x tau,1,"", $yt if (ItemsInList(wavelist(yt,";","WIN:"))==0) AppendToGraph $yt ModifyGraph mode($yt)=3,marker($yt)=17,rgb($yt)=(0,0,0),msize=4 endif endif print $par string templbl, printlabel=data_label+"\t"+mdl+"\t"+cflag sprintf templbl "\t%.6g\t%.5g",V_chisq,cc printlabel+=templbl i=0 do sprintf templbl "\t%.5g \t± %.3g",$par[i],W_sigma[i] printlabel+=templbl i+=1 while (ic_max) c_max=c_data[i] t_max=t_data[i] i_max=i endif i+=1 while (i0) make/o/n=1 $yt=$model($par,tau) SetScale/P x tau,1,"", $yt if (ItemsInList(wavelist(yt,";","WIN:"))==0) AppendToGraph $yt ModifyGraph mode($yt)=3,marker($yt)=17,rgb($yt)=(0,0,0),msize=3 endif endif End Proc map_chisq(ctrlName) : ButtonControl // 2021-12-29 String ctrlName variable steps=10 // number of values to be tried for logarithmic partitioning of each parameter range variable i=0 string par="par_"+model_name,parl="parl_"+model_name,parh="parh_"+model_name,parlabels="plb_"+model_name string model="cf_"+model_name,H="H_"+model_name do if (!exists(parl) || !exists(parh)) print "Creating ranges for model", model_name make/n=(numpnts($par)) $parl=0.01,$parh=1000 i=0 do if (char2num($parlabels[i][0])==107) // parameter label starts with "k" $parh[i]=5 endif i+=1 while (i0)*$H[j] duplicate/o $par pars duplicate/o c_data sim_data do ipar=0 div=step do rem=mod(div,steps) // 2022-01-08 corrected calculation div=(div-rem)/steps pars[ipar]=$parl[ipar]*($parh[ipar]/$parl[ipar])^(rem/(steps-1)) // print step,ipar,rem,div ipar+=1 while (ipar0)*$H[j] duplicate/o $par pars variable npar=numpnts(pars) make/o/n=(n_fits+1,2*npar+2) $fit_res=NaN DoWindow/K $Tr edit/N=$Tr/W=(2,200,600,500) $fit_res ModifyTable width(Point)=20,format($fit_res)=3,digits($fit_res)=4,width($fit_res)=40 $fit_res[0][0,npar-1]=$par[y] t_data=sample_times duplicate/o t_data,sim_data i=0 do make/o/n=(numpnts(sample_times)) c_data=$cf($par,sample_times)*(1+gnoise(nslevel)) pars=$par FuncFit/Q/H=holdflags/NTHR=0/M=2 $cf pars c_data[startp,endp] /X=t_data sim_data=$cf(pars,t_data) $fit_res[i+1][0,npar-1]=pars[y] $fit_res[i+1][npar,2*npar-1]=W_sigma[y-npar] $fit_res[i+1][2*npar]=V_chisq $fit_res[i+1][2*npar+1]=corrcoef(sim_data,c_data,startp,endp) i+=1 while (i0) labellist=RemoveListItem(0,labellist) labellist=SortList(AddListItem(newlabel,labellist)) labellist=AddListItem(newlabel,labellist) // redimension/n=(ItemsInList(labellist)) data_labels // data_labels=StringFromList(x,labellist) // labellist= endif End Function/s RebuildDataLabels() wave/t data_labels=data_labels variable n = numpnts(data_labels),i=0 string out="" do out+=data_labels[i]+";" i+=1 while (ic_max) c_max=c_data[i] t_max=t_data[i] i_max=i endif i+=1 while (i 0) i_start_dc=pcsr(B) else i_start_dc=i_max+2 endif CurveFit/Q/M=2/W=0 line, dc_data[i_start_dc,]/X=c_data/D // fit dc/dt = f(c) duplicate/o dc_data dcs_data wavestats/q dc_data setaxis left V_min*1.1,0 dcs_data=poly(W_coef,c_data) cc=corrcoef(dcs_data,dc_data,i_max+1,numpnts(c_data)-1) fitlabel="\f02k\f00\\Bel\\M = "+num2str(-W_coef[1])+" ± "+num2str(W_sigma[1]) fitlabel+="\rd\f02c\f00/d\f02t\f00\\Boo\\M = "+num2str(W_coef[0])+" ± "+num2str(W_sigma[0]) fitlabel+="\r\f02?\f00\\S2\\M = "+num2str(V_chisq)+", \f02R\f00\S2\M = "+num2str(cc) TextBox/C/N=CF_dc_data fitlabel // print W_coef[0]," ±", W_sigma[0],W_coef[1]," ±", W_sigma[1], lbl sprintf templbl " kel = %.3f (±%.3f) h^-1 %s",-W_coef[1],W_sigma[1],lbl printlbl+=templbl DoWindow/F Gcf_ln // 2nd analysis TextBox/C/N=text0 lbl duplicate/o c_data lnc_data lnc_data=ln(c_data) CurveFit/Q/M=2/NTHR=0 line lnc_data[i_max,] /X=t_data /D /I=1 // fit lnc = f(t) without weights duplicate/o c_data cs_lndata cs_lndata=poly(W_coef,t_data) cc=corrcoef(cs_lndata,lnc_data,i_max,numpnts(c_data)-1) fitlabel="\f02k\f00\\Bel\\M = "+num2str(-W_coef[1])+" ± "+num2str(W_sigma[1]) fitlabel+="\r\f02?\f00\\S2\\M = "+num2str(V_chisq)+", \f02R\f00\S2\M = "+num2str(cc) TextBox/C/N=CF_lnc_data fitlabel sprintf templbl " ln fit: tmax = %1.2f h kel = %.3f (±%.3f) h^-1 R^2 = %1.5f",t_max,-W_coef[1],W_sigma[1],cc printlbl+=templbl duplicate/o c_data s_lndata // 3rd analysis s_lndata=c_data^-2 CurveFit/Q/W=0/M=2 line, lnc_data[i_max,]/X=t_data[i_max,] /W=s_lndata/I=1 // fit lnc = f(t) to line with weights for ln(c) data duplicate/o c_data cs_data cs_data=exp(poly(W_coef,t_data)) cc=corrcoef(cs_data,c_data,i_max,numpnts(c_data)-1) // fitlabel="\f02k\f00\\Bel\\M = "+num2str(-W_coef[1])+" ± "+num2str(W_sigma[1]) // fitlabel+="\r\f02?\f00\\S2\\M = "+num2str(V_chisq)+", \f02R\f00\S2\M = "+num2str(cc) TextBox/C/N=CF_c_data fitlabel sprintf templbl " w-ln fit: kel = %.3f (±%.3f) h^-1 R^2 = %1.5f",-W_coef[1],W_sigma[1],cc printlbl+=templbl DoWindow/F Gcf_zot1 // 4th analysis TextBox/C/N=text0 lbl par_zot1[2]=t_max par_zot1[1]=-w_coef[1] par_zot1[0]=exp(W_coef[0]) RemoveFromGraph/Z fit_zot1,res_zot1 killwaves/Z fit_zot1,res_zot1 FuncFit/Q/NTHR=0/M=2 cf_zot1 par_zot1 c_data /X=t_data /I=1 /D /R // fit non-lin 2021-04-14 AddCorrelation() rename fit_c_data fit_zot1 rename res_c_data,res_zot1 duplicate/o c_data,sim_data sim_data=cf_zot1(par_zot1,t_data) cc=corrcoef(sim_data,c_data,0,numpnts(c_data)-1) fitlabel="\f02FD/V\f00\Bd\M = "+num2str(par_zot1[0])+" ±"+num2str(W_sigma[0])+"\r\f02o\f00 = "+num2str(par_zot1[2])+" ± "+num2str(W_sigma[2]) fitlabel+="\r\f02k\f00\\Bel\\M = "+num2str(par_zot1[1])+" ± "+num2str(W_sigma[1]) fitlabel+="\r\f02?\f00\\S2\\M = "+num2str(V_chisq)+", \f02R\f00\S2\M = "+num2str(cc) TextBox/C/N=CF_c_data fitlabel sprintf templbl " o = %1.2f (±%.2f) h kel = %.3f (±%.3f) h^-1 R^2 = %1.5f",par_zot1[2],W_sigma[2],par_zot1[1],W_sigma[1],cc printlbl+=templbl par_fo[0]=par_zot1[0] par_fo[2]=par_zot1[1] fit_fo(lbl) // 5th analysis sprintf templbl " Bateman: ka = %.3f (±%.3f) h^-1 kel = %.3f (±%.3f) h^-1 R^2 = %1.5f",par_fo[1],W_sigma[1],par_fo[2],W_sigma[2],cc printlbl+=templbl par_fot[0]=par_fo[0] par_fot[1]=par_fo[1] par_fot[2]=par_fo[2] par_fot[3]=0.5*(t_max+par_zot1[2]) fit_fot(lbl) // 6h analysis sprintf templbl " trunc-Bateman: ka = %.3f (±%.3f) h^-1 kel = %.3f (±%.3f) h^-1 o = %1.2f (±%.2f) h R^2 = %1.5f",par_fot[1],W_sigma[1],par_fot[2],W_sigma[2],par_fot[3],W_sigma[3],cc printlbl+=templbl print printlbl DoWindow/H/F End Macro AUCs1(lbl) // 2021-04-12 string lbl variable i=0,i_max,t_max,c_max,AUC,AUCiv,AUCr,s_AUCr,s_AUCr2,cc,kt,ekt string printlbl,templbl,fitlabel DoWindow/F G_AUC TextBox/C/N=text0 lbl do if (c_data[i]>c_max) c_max=c_data[i] t_max=t_data[i] i_max=i endif i+=1 while (i