It's hard to see in your plot, Jonas, but it seems the spike occurs somewhere above -0.09, where the second sub-function is to be used. The function is composed of three sub-functions, each valid within its respective range: -1 <= to < -0.08905 -0.08905 <= to < 0.320018 0.320018 <= to <= 1 What you code seem correct, but it is only valid for the first range and so that it has a spike outside that range is really unimportant; in that region where the spike is the second sub-function is valid, the other two not. So your code should look something like (not JS code obviously) Code: