{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# Introduction to Scientific Python programming.\n", "\n", "# Adapted to TKT4140 Numerical Methods\n", "\n", " **Hans Petter Langtangen Center for Biomedical Computing, Simula Research Laboratory & Department of Informatics, University of Oslo**\n", "\n", "\n", " **Leif Rune Hellevik** (email: `leif.r.hellevik@ntnu.no`), Biomechanichs Group, Department of Structural Engineering NTNU and Center for Biomedical Computing, Simula Research Laboratory\n", "\n", "Date: **Sep 18, 2017**\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "## This is a very quick intro to Python programming\n", "\n", " * variables for numbers, lists, and arrays\n", "\n", " * while loops and for loops\n", "\n", " * functions\n", "\n", " * if tests\n", "\n", " * plotting\n", "\n", " * files\n", "\n", " * classes\n", "\n", "Method: show program code through math examples\n", "\n", "\n", "# Variables, loops, lists, and arrays\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "## Do you have access to Python?\n", "\n", "\n", "Many methods:\n", "\n", " * Mac and Windows: [Anaconda](https://store.continuum.io/cshop/anaconda/)\n", "\n", " * Ubuntu: `sudo apt-get install`\n", "\n", " * Web browser ([Wakari](https://wakari.io/) or [SageMathCloud](https://cloud.sagemath.com/))\n", "\n", "See [How to access Python for doing scientific computing](http://hplgit.github.io/edu/accesspy/accesspy.html) for more details!\n", "\n", "## Mathematical example\n", "\n", "Most examples will involve this formula:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "\n", "$$\n", "\\begin{equation}\n", "\\label{basics:seq} \\tag{1}\n", "s = v_0t + \\frac{1}{2}at^2\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We may view $s$ as a function of $t$: $s(t)$, and also include the\n", "parameters in the notation: $s(t;v_0,a)$.\n", "\n", "## A program for evaluating a formula\n", "\n", "\n", "\n", "**Task.**\n", "\n", "Compute $s$ for $t=0.5$, $v_0=2$, and $a=0.2$.\n", "\n", "\n", "\n", "**Python code.**" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "t = 0.5\n", "v0 = 2\n", "a = 0.2\n", "s = v0*t + 0.5*a*t**2\n", "print s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution.**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Terminal> python distance.py\n", " 1.025\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Assignment statements assign a name to an object" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "t = 0.5 # real number makes float object\n", "v0 = 2 # integer makes int object\n", "a = 0.2 # float object\n", "s = v0*t + 0.5*a*t**2 # float object" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Rule:\n", "\n", " * evaluate right-hand side; it results in an *object*\n", "\n", " * left-hand side is a name for that object\n", "\n", "## Formatted output with text and numbers\n", "\n", "\n", " * Task: write out text with a number (3 decimals): `s=1.025`\n", "\n", " * Method: printf syntax" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print 's=%g' % s # g: compact notation\n", "print 's=%.2f' % s # f: decimal notation, .2f: 2 decimals" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Modern alternative: format string syntax" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print 's={s:.2f}'.format(s=s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Programming with a while loop\n", "\n", "\n", "\n", " * Task: write out a table of $t$ and $s(t)$ values (two columns),\n", " for $t\\in [0,2]$ in steps of 0.1\n", "\n", " * Method: while loop" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "v0 = 2\n", "a = 0.2\n", "dt = 0.1 # Increment\n", "t = 0 # Start value\n", "while t <= 2:\n", " s = v0*t + 0.5*a*t**2\n", " print t, s\n", " t = t + dt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Output of the previous program" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Terminal> python while.py\n", " 0 0.0\n", " 0.1 0.201\n", " 0.2 0.404\n", " 0.3 0.609\n", " 0.4 0.816\n", " 0.5 1.025\n", " 0.6 1.236\n", " 0.7 1.449\n", " 0.8 1.664\n", " 0.9 1.881\n", " 1.0 2.1\n", " 1.1 2.321\n", " 1.2 2.544\n", " 1.3 2.769\n", " 1.4 2.996\n", " 1.5 3.225\n", " 1.6 3.456\n", " 1.7 3.689\n", " 1.8 3.924\n", " 1.9 4.161\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Structure of a while loop" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "while condition:\n", "