code for curve fitting