Curve fitting with an if statement

7 次查看(过去 30 天)
I am using the curve fitting app on matlab to fit the following function to my data set:
y(x) = (a0*(1-exp(-b0*(x-c0))))+(a1*(1-exp(-b1*(x-c1))))
However, I would like to alter the equation to be:
y(x) = ((a0*(1-exp(-b0*(x-c0))))*u0)+((a1*(1-exp(-b1*(x-c1))))*u1)
with the following command
u0 = if(x<c0,0,1)
u1= if(x<c1,0,1)
i.e. y would equal 0 until x passes c0 and y(x) would equal the first part of the equation between c0 and c1 and the full model from c1 onwards.
Please see page three on the attached pdf under VO2 kinetics for a similar example.
Any help would be greatly appreciated.

采纳的回答

Walter Roberson
Walter Roberson 2019-5-23
编辑:Walter Roberson 2019-5-23
y = @(x) ((a0*(1-exp(-b0*(x-c0)))).*(x>=c0)) + ((a1*(1-exp(-b1*(x-c1)))).*(x>=c1))
  2 个评论
Julian Dale
Julian Dale 2019-5-23
Hi Walter,
Thanks for your quick response. Would it be correct to say that .* means when.
Thanks
Julian
Walter Roberson
Walter Roberson 2019-5-23
.* means element-by-element multiplication.
The result of a logical test is a logical array of true and false values. In many circumstances, the value false is automatically converted to 0, and the value true is automatically converted to 1. So if you multiply by a logical value, then if the logical value was false, that is multiplication by 0, and if the logical value was true, then that is multiplication by 1.
The situation you need to watch out for is the case where an unselected case generates infinity:
(1./x).*(x ~= 0) + -1 .* (x == 0)
looks like it should express:
x == 0 -> -1
x ~= 0 -> 1/x
However, when x is 0, then 1/x is inf, and you would be calculating (inf).*(0) but inf*0 is NaN, and NaN + anything is NaN.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by