{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from scipy.optimize import curve_fit\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from math import sin\n", "\n", "xrand = np.random.normal(scale=4.,size=100)\n", "\n", "yrand = np.random.normal(scale=.5,size=100)\n", "yrand = [ (sin(xrand[i]) + yrand[i]) + 15 for i in range(len(xrand))]\n", "\n", "xrand *= 20.\n", "xrand += 1800.\n", "\n", "fig, ax = plt.subplots()\n", "ax.plot(xrand, yrand, 'o')\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def lin(x,a,b):\n", " return a*x + b\n", "\n", "ab,cov = curve_fit(lin,xrand,yrand,p0=(0,0))\n", "\n", "xfit = np.arange(min(xrand),max(xrand),20.)\n", "#yfit = [ab[0]*x+ab[1] for x in xfit]\n", "yfit = lin(xfit,*ab)\n", "\n", "fig, ax = plt.subplots()\n", "ax.plot(xrand, yrand, 'o', xfit, yfit)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from math import sqrt \n", "\n", "ab,cov = curve_fit(lin,xrand[::2],yrand[::2],p0=(0,0))\n", "\n", "def sqerr(x,y,f,par):\n", " fit = f(x,*par)\n", " diff = fit - y\n", " return sqrt(np.sum(np.vectorize(lambda x: x*x)(diff))) / len(x)\n", "\n", "print(\"fit err: \",sqerr(xrand[::2],yrand[::2],lin,ab))\n", "print(\"test err: \",sqerr(xrand[1::2],yrand[1::2],lin,ab)) \n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def sinscaled(xarr,a,b,c,d):\n", " return np.array([a*sin(b*x+c)+d for x in xarr])\n", "\n", "abcd,cov = curve_fit(sinscaled,xrand,yrand,p0=[1.,.04,0.,15.])\n", "\n", "xfit = np.arange(min(xrand),max(xrand),2.)\n", "yfit = sinscaled(xfit,*abcd)\n", "\n", "fig, ax = plt.subplots()\n", "ax.plot(xrand, yrand, 'o', xfit, yfit)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "abcd,cov = curve_fit(sinscaled,xrand[::2],yrand[::2],p0=[1.,.04,0.,15.])\n", "\n", "print(\"fit err: \",sqerr(xrand[::2],yrand[::2],sinscaled,abcd))\n", "print(\"test err: \",sqerr(xrand[1::2],yrand[1::2],sinscaled,abcd)) \n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def doublesin(xarr,a,b,c,d,a2,b2,c2):\n", " return np.array([a*sin(b*x+c)+d+a2*sin(b2*x+c2) for x in xarr])\n", "\n", "par,cov = curve_fit(doublesin,xrand,yrand,p0=[1.,.04,0.,15.,5.,.5,0.])\n", "\n", "xfit = np.arange(min(xrand),max(xrand),2.)\n", "yfit = doublesin(xfit,*par)\n", "\n", "fig, ax = plt.subplots()\n", "ax.plot(xrand, yrand, 'o', xfit, yfit)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "par,cov = curve_fit(doublesin,xrand[::2],yrand[::2],p0=[1.,.04,0.,15.,5.,.5,0.])\n", "\n", "print(\"fit err: \",sqerr(xrand[::2],yrand[::2],doublesin,par))\n", "print(\"test err: \",sqerr(xrand[1::2],yrand[1::2],doublesin,par)) \n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def poly(xarr,*par):\n", " val = np.zeros(len(xarr))\n", " for i in range(len(xarr)):\n", " xp = 1\n", " for p in par:\n", " val[i] += xp*p\n", " xp *= xarr[i]\n", " return val\n", "\n", "par,cov = curve_fit(poly,xrand,yrand,p0 = [15.] + [1.] * 8)\n", "\n", "xfit = np.arange(min(xrand),max(xrand),2.)\n", "yfit = poly(xfit,*par)\n", "\n", "fig, ax = plt.subplots()\n", "ax.plot(xrand, yrand, 'o', xfit, yfit)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "par,cov = curve_fit(poly,xrand[::2],yrand[::2],p0=[15.] + [1.] * 8)\n", "\n", "print(\"fit err: \",sqerr(xrand[::2],yrand[::2],poly,par))\n", "print(\"test err: \",sqerr(xrand[1::2],yrand[1::2],poly,par)) \n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 2 }