{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 303 - Arbeiten mit Physikalischen Größen" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Numpy und Matplotlib brauchen wir immer\n", "import numpy as np\n", "%matplotlib inline\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def plot_data(xdata, ydata, xerr=None, yerr=None, ax=plt, **kwargs):\n", " # retrieve errors from data\n", " xdata, xerr = unp.nominal_values(xdata), unp.std_devs(xdata)\n", " if (xerr == 0).all():\n", " xerr = None\n", " ydata, yerr = unp.nominal_values(ydata), unp.std_devs(ydata)\n", " if (yerr == 0).all():\n", " yerr = None\n", " # default plot style\n", " if not ('ls' in kwargs or 'linestyle' in kwargs):\n", " kwargs['ls'] = 'none'\n", " if not 'marker' in kwargs:\n", " kwargs['marker'] = '.'\n", " # plot\n", " return ax.errorbar(xdata, ydata, xerr=xerr, yerr=yerr, **kwargs)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot_data(x, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Physikalische Einheiten" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pint\n", "units = pint.UnitRegistry()\n", "Q_ = units.Quantity\n", "units.default_format = '~P' # Abgekürzte Einheiten-Namen" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import uncertainties.unumpy as unp\n", "t = Q_(unp.uarray([1,2,3], 2), units.seconds)\n", "l = Q_(unp.uarray([1,2,3], 2), units.meters)\n", "print(format(l/t, \"~L\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = (10 * units.m/units.s).plus_minus(2)\n", "print(a)\n", "print(\"{:~L}\".format(a))\n", "print(\"{:Lx}\".format(a))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t = np.linspace(0, 10, 100) * units.seconds\n", "a = -9.81 * units.meter / units.seconds**2\n", "s = a / 2 * t**2\n", "plt.plot(t, s)\n", "plt.xlabel(\"Zeit $t \\, [{:~L}]$\".format(t.units))\n", "plt.ylabel(\"Position $s \\, [{:~L}]$\".format(s.units))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot_data(date, T)\n", "plt.ylabel('Temperatur $T [{:~L}]$'.format(T.units))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "T_0 = Q_(23.5, units.degC)\n", "print(T_0, T_0.to(units.K))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "heating_rate = 0.5 * units.delta_degC / units.s\n", "print(heating_rate, heating_rate.to(units.K / units.s))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(T_0 + 10 * units.minute * heating_rate)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "u = x * units.s\n", "v = y * units.m" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def plot_labels(xdata, ydata):\n", " plt.xlabel(\"${:~P}$\".format(xdata.units))\n", " plt.ylabel(\"${:~P}$\".format(ydata.units))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot_data(u.magnitude, v.magnitude)\n", "plot_labels(u, v)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tabellen" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from tabulate import tabulate" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(tabulate([x, y]))" ] } ], "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.0" } }, "nbformat": 4, "nbformat_minor": 1 }