This function is excellent and exactly what I needed!
One potential bug:
The compound inequality on line 36 (1<=alf<2) always evaluates to true (1<=alf evaluates to a logical 0 or 1, which is always less than 2). I don't think this was the statement's intent.