{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ad import adnumber\n", "\n", "x = adnumber(2.)\n", "x" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y = 5.*x + 3\n", "y" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y.d(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y = 7.*x*x + 5.*x + 3\n", "y" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y.d(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from math import sin,pi\n", "\n", "x = adnumber(pi)\n", "y = sin(x)\n", "y" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y.d(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ad.admath import sin\n", "y = sin(x)\n", "y.d(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy\n", "from ad.admath import cos\n", "from math import pi\n", "\n", "numpy.random.seed(1)\n", "xrand = numpy.random.normal(scale=.1,size=10)\n", "xrand[0] = 0.\n", "for i in range(1,10): \n", " xrand[i] = abs(xrand[i-1]) + abs(xrand[i]) + .02\n", "yrand = numpy.random.normal(scale=.42,size=10)\n", "\n", "def frantisek(x):\n", "# print(x)\n", " i = 0\n", " while i < len(xrand) and xrand[i] <= x: \n", " i += 1\n", " if i>=len(xrand):\n", " return 0\n", " return (yrand[i]-yrand[i-1]) * ( \n", " .5 - .5*cos((x - xrand[i-1])/(xrand[i]-xrand[i-1])*pi)\n", " ) + yrand[i-1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "xx = numpy.arange(0.,xrand[9],xrand[9]/500)\n", "yy = [frantisek(x)*25. for x in xx]\n", "plt.plot(xx,yy)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "yyd = []\n", "for x in xx:\n", " xd = adnumber(x)\n", " yd = frantisek(xd)\n", " yyd.append(yd.d(xd))\n", " \n", "plt.plot(xx,yy,xx,yyd)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from scipy.optimize import minimize\n", "\n", "def frantisek_dx(x):\n", " xd = adnumber(x[0])\n", " yd = frantisek(xd)\n", " ret = numpy.empty((1,1))\n", " ret[0,0] = yd.d(xd)\n", " return ret\n", "\n", "minimize(fun=lambda x: frantisek(x[0]),\n", " x0=[.5],\n", " method='L-BFGS-B',\n", " bounds=[(.4,.8)],\n", " jac=frantisek_dx\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.3" } }, "nbformat": 4, "nbformat_minor": 2 }