Source code for naginterfaces.library.examples.opt.lsq_uncon_mod_func_comp_ex

#!/usr/bin/env python3
"``naginterfaces.library.opt.lsq_uncon_mod_func_comp`` Python Example."

# NAG Copyright 2021.

# pylint: disable=invalid-name,too-many-arguments

import numpy as np

from naginterfaces.library import opt

[docs]def main(): """ Example for :func:`naginterfaces.library.opt.lsq_uncon_mod_func_comp`. Find an unconstrained minimum of a sum of squares of m nonlinear functions in n variables (m at least n). No derivatives are required. >>> main() naginterfaces.library.opt.lsq_uncon_mod_func_comp Python Example Results. Find an unconstrained minimum of a sum of squares. <BLANKLINE> Best fit model parameters are: x_0 = 0.082 x_1 = 1.133 x_2 = 2.344 <BLANKLINE> Residuals for observed data: -0.0059 -0.0003 0.0003 0.0065 -0.0008 -0.0013 -0.0045 -0.0200 0.0822 -0.0182 -0.0148 -0.0147 -0.0112 -0.0042 0.0068 <BLANKLINE> Sum of squares of residuals: 0.0082 """ print( 'naginterfaces.library.opt.lsq_uncon_mod_func_comp ' 'Python Example Results.' ) print('Find an unconstrained minimum of a sum of squares.') def cb_lsqfun(iflag, m, xc): """Evaluate the residuals.""" fvec = (xc[0] + t[:,0]/(xc[1]*t[:,1]+xc[2]*t[:,2]) - y[:]) return iflag, fvec y = [ 0.14, 0.18, 0.22, 0.25, 0.29, 0.32, 0.35, 0.39, 0.37, 0.58, 0.73, 0.96, 1.34, 2.10, 4.39, ] m = len(y) t = np.array([ [1.0, 15.0, 1.0], [2.0, 14.0, 2.0], [3.0, 13.0, 3.0], [4.0, 12.0, 4.0], [5.0, 11.0, 5.0], [6.0, 10.0, 6.0], [7.0, 9.0, 7.0], [8.0, 8.0, 8.0], [9.0, 7.0, 7.0], [10.0, 6.0, 6.0], [11.0, 5.0, 5.0], [12.0, 4.0, 4.0], [13.0, 3.0, 3.0], [14.0, 2.0, 2.0], [15.0, 1.0, 1.0], ]) # Initial guess x = [.5, 1., 1.5] soln = opt.lsq_uncon_mod_func_comp( m, cb_lsqfun, x, ) print('\nBest fit model parameters are:') for i in range(len(x)): print(' x_{:d} = {:10.3f}'.format(i, soln.x[i])) print('\nResiduals for observed data:') print('\n'.join([''.join([' {:8.4f}']*5)]*3).format(*soln.fvec)) print('\nSum of squares of residuals: {:6.4f}'.format(soln.fsumsq))
if __name__ == '__main__': import doctest import sys sys.exit( doctest.testmod( None, verbose=True, report=False, optionflags=doctest.REPORT_NDIFF, ).failed )