A local extremum of a function ƒ is determined by computing a Jacobian of ƒ at a test point x by adding an imaginary part to x and a Hessian of ƒ by adding two imaginary parts to a multicomplex copy of x and extracting a third imaginary part. Solving a system of equations defined by the Jacobian and Hessian yields a delta the process is repeated until convergence. This method is used in each of a series of time intervals to compute an insulin-delivery amount for an insulin pump. ƒ is a model-predictive-control cost function x is a set of successive candidate insulin delivery amounts beginning from a selected time interval. A system includes a glucose monitor and a controller using glucose measurement data therefrom to determine an insulin delivery amount for a time interval by minimizing ƒ an insulin pump provides insulin corresponding to the delivery amount.