{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\ndoc_parameters_valuesdict.py\n============================\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "##\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n##\n# \nimport numpy as np\n\nfrom lmfit import Minimizer, Parameters, report_fit\n\n# create data to be fitted\nx = np.linspace(0, 15, 301)\ndata = (5.0 * np.sin(2.0*x - 0.1) * np.exp(-x*x*0.025) +\n np.random.normal(size=x.size, scale=0.2))\n\n\n# define objective function: returns the array to be minimized\ndef fcn2min(params, x, data):\n \"\"\"Model a decaying sine wave and subtract data.\"\"\"\n v = params.valuesdict()\n\n model = v['amp'] * np.sin(x * v['omega'] + v['shift']) * np.exp(-x*x*v['decay'])\n return model - data\n\n\n# create a set of Parameters\nparams = Parameters()\nparams.add('amp', value=10, min=0)\nparams.add('decay', value=0.1)\nparams.add('shift', value=0.0, min=-np.pi/2., max=np.pi/2)\nparams.add('omega', value=3.0)\n\n# do fit, here with the default leastsq algorithm\nminner = Minimizer(fcn2min, params, fcn_args=(x, data))\nresult = minner.minimize()\n\n# calculate final result\nfinal = data + result.residual\n\n# write error report\nreport_fit(result)\n\n# try to plot results\ntry:\n import matplotlib.pyplot as plt\n plt.plot(x, data, 'k+')\n plt.plot(x, final, 'r')\n plt.show()\nexcept ImportError:\n pass\n# " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }