|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "#\n", |
| 10 | + "# Program 1.1: Motion with constant acceleration (motion.ipynb)\n", |
| 11 | + "# J Wang, Computational modeling and visualization with Python\n", |
| 12 | + "#\n", |
| 13 | + "\n", |
| 14 | + "import matplotlib.pyplot as plt # get matplotlib plot functions \n", |
| 15 | + "import sys\n", |
| 16 | + "%matplotlib notebook\n", |
| 17 | + "\n", |
| 18 | + "if (sys.version_info[0] < 3):\n", |
| 19 | + " a, v0 = input('enter a, v0 (eg 1.0, 0.0) : ') # read input 2.xx \n", |
| 20 | + "else:\n", |
| 21 | + " a, v0 = eval(input('enter a, v0 (eg 1.0, 0.0) : ')) # 3.xx\n", |
| 22 | + "t, h, n = 0.0, 0.1, 20 # init time, step size, number of steps\n", |
| 23 | + "ta, xa = [], [] # time and position arrays for plotting \n", |
| 24 | + "\n", |
| 25 | + "for i in range(n): # loop for n steps \n", |
| 26 | + " ta.append(t) # record time and position\n", |
| 27 | + " xa.append(v0*t + a*t*t/2.0)\n", |
| 28 | + " t = t + h # update time \n", |
| 29 | + "\n", |
| 30 | + "plt.figure() # start a figure; no indent->end of loop \n", |
| 31 | + "plt.plot(ta, xa, '-o') # plot data\n", |
| 32 | + "plt.xlabel('t (s)') # add labels\n", |
| 33 | + "plt.ylabel('x (m)')\n", |
| 34 | + "plt.show() # show figure" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": null, |
| 40 | + "metadata": { |
| 41 | + "collapsed": true |
| 42 | + }, |
| 43 | + "outputs": [], |
| 44 | + "source": [] |
| 45 | + } |
| 46 | + ], |
| 47 | + "metadata": { |
| 48 | + "kernelspec": { |
| 49 | + "display_name": "Python 3", |
| 50 | + "language": "python", |
| 51 | + "name": "python3" |
| 52 | + }, |
| 53 | + "language_info": { |
| 54 | + "codemirror_mode": { |
| 55 | + "name": "ipython", |
| 56 | + "version": 3 |
| 57 | + }, |
| 58 | + "file_extension": ".py", |
| 59 | + "mimetype": "text/x-python", |
| 60 | + "name": "python", |
| 61 | + "nbconvert_exporter": "python", |
| 62 | + "pygments_lexer": "ipython3", |
| 63 | + "version": "3.6.1" |
| 64 | + } |
| 65 | + }, |
| 66 | + "nbformat": 4, |
| 67 | + "nbformat_minor": 1 |
| 68 | +} |
0 commit comments