Curve fitting with an if statement

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.

 Accepted Answer

y = @(x) ((a0*(1-exp(-b0*(x-c0)))).*(x>=c0)) + ((a1*(1-exp(-b1*(x-c1)))).*(x>=c1))

2 Comments

Hi Walter,
Thanks for your quick response. Would it be correct to say that .* means when.
Thanks
Julian
.* 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.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2019a

Community Treasure Hunt

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

Start Hunting!