Curve fitting with LsqFit

The ternary operator doesn’t particaipate in broadcasting, but you can just use ifelse instead:

model1(t, p) = @.(p[1] * exp(-p[2] * t) + p[3] * exp(-p[4] * t) * cos(p[5] * t + p[6])+ p[7] * ifelse(t > p[8], 0, (1 - t / p[8])^2))

This does have some implications you should be aware of:

help?> ifelse
search: ifelse

  ifelse(condition::Bool, x, y)

  Return x if condition is true, otherwise return y. This differs from ? or if in that it is an ordinary function, so all the arguments are evaluated first. In some cases, using ifelse instead of an if statement
  can eliminate the branch in generated code and provide higher performance in tight loops.
3 Likes