{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\nExample demonstrating the model parameter estimation feature. \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from prog_models.models.thrown_object import ThrownObject\n\ndef run_example():\n # Step 1: Build the model with your best guess in parameters\n # Here we're guessing that the thrower is 20 meters tall. Obviously not true!\n # Let's see if parameter estimation can fix this\n m = ThrownObject(thrower_height=20)\n\n # Step 2: Collect data from the use of the system. Let's pretend we threw the ball once, and collected position measurements \n times = [0, 1, 2, 3, 4, 5, 6, 7, 8]\n inputs = [{}]*9\n outputs = [\n {'x': 1.83},\n {'x': 36.95},\n {'x': 62.36},\n {'x': 77.81},\n {'x': 83.45},\n {'x': 79.28},\n {'x': 65.3},\n {'x': 41.51},\n {'x': 7.91},\n ]\n\n # Step 3: Identify the parameters to be estimated\n keys = ['thrower_height', 'throwing_speed']\n\n # Printing state before\n print('Model configuration before')\n for key in keys:\n print(\"-\", key, m.parameters[key])\n print(' Error: ', m.calc_error(times, inputs, outputs, dt=1e-4))\n\n # Step 4: Run parameter estimation with data\n m.estimate_params([(times, inputs, outputs)], keys, dt=0.01)\n\n # Print result\n print('\\nOptimized configuration')\n for key in keys:\n print(\"-\", key, m.parameters[key])\n print(' Error: ', m.calc_error(times, inputs, outputs, dt=1e-4))\n \n # Sure enough- parameter estimation determined that the thrower's height wasn't 20 m, instead was closer to 1.9m, a much more reasonable height!\n\nif __name__=='__main__':\n run_example()" ] } ], "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.8.9" } }, "nbformat": 4, "nbformat_minor": 0 }