first cut at porting bill's curve fitting code from matlab