1
0
Fork 0
mirror of https://gitlab.rlp.net/pgp/pgp1-python-einfuehrung synced 2024-11-16 13:48:11 +00:00

Merge branch 'master' of gitlab.rlp.net:hoek/pgp1-python-einfuehrung

This commit is contained in:
Matthias Hoek 2019-10-31 15:38:15 +01:00
commit de7125acec

View file

@ -33,7 +33,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:02.778729Z",
"start_time": "2019-10-31T12:32:02.747485Z"
}
},
"outputs": [],
"source": [
"3 + 2"
@ -159,7 +164,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:02.794349Z",
"start_time": "2019-10-31T12:32:02.778729Z"
}
},
"outputs": [],
"source": [
"2 / 3 - 2"
@ -168,7 +178,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:02.809971Z",
"start_time": "2019-10-31T12:32:02.794349Z"
}
},
"outputs": [],
"source": [
"3**2 * 2 - 8 "
@ -177,7 +192,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:02.825592Z",
"start_time": "2019-10-31T12:32:02.809971Z"
}
},
"outputs": [],
"source": [
"3**2**2"
@ -193,7 +213,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:02.841214Z",
"start_time": "2019-10-31T12:32:02.825592Z"
}
},
"outputs": [],
"source": [
"3**2 * 2 - 8 "
@ -202,7 +227,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:02.856849Z",
"start_time": "2019-10-31T12:32:02.841214Z"
}
},
"outputs": [],
"source": [
"3**2 * (2 - 8 ) "
@ -218,7 +248,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:02.872456Z",
"start_time": "2019-10-31T12:32:02.856849Z"
}
},
"outputs": [],
"source": [
"a = 5"
@ -227,7 +262,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:02.888078Z",
"start_time": "2019-10-31T12:32:02.872456Z"
}
},
"outputs": [],
"source": [
"a"
@ -236,7 +276,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:02.903701Z",
"start_time": "2019-10-31T12:32:02.888078Z"
}
},
"outputs": [],
"source": [
"variable = 2"
@ -245,7 +290,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:02.919320Z",
"start_time": "2019-10-31T12:32:02.903701Z"
}
},
"outputs": [],
"source": [
"a * variable"
@ -261,7 +311,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:02.934941Z",
"start_time": "2019-10-31T12:32:02.919320Z"
}
},
"outputs": [],
"source": [
"a = 4\n",
@ -281,7 +336,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:02.950562Z",
"start_time": "2019-10-31T12:32:02.934941Z"
}
},
"outputs": [],
"source": [
"a = 7"
@ -290,7 +350,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:02.966183Z",
"start_time": "2019-10-31T12:32:02.950562Z"
}
},
"outputs": [],
"source": [
"a = 4"
@ -299,7 +364,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:02.981808Z",
"start_time": "2019-10-31T12:32:02.966183Z"
}
},
"outputs": [],
"source": [
"a * b"
@ -315,7 +385,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:02.997429Z",
"start_time": "2019-10-31T12:32:02.981808Z"
}
},
"outputs": [],
"source": [
"a = 2\n",
@ -327,7 +402,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.013048Z",
"start_time": "2019-10-31T12:32:02.997429Z"
}
},
"outputs": [],
"source": [
"a = 2\n",
@ -363,7 +443,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.028669Z",
"start_time": "2019-10-31T12:32:03.013048Z"
}
},
"outputs": [],
"source": [
"'Dies ist eine Zeichenkette'"
@ -379,7 +464,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.044292Z",
"start_time": "2019-10-31T12:32:03.028669Z"
}
},
"outputs": [],
"source": [
"s1 = '0123456789'\n",
@ -400,7 +490,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.059912Z",
"start_time": "2019-10-31T12:32:03.044292Z"
}
},
"outputs": [],
"source": [
"a = 'eins'\n",
@ -421,7 +516,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.075545Z",
"start_time": "2019-10-31T12:32:03.059912Z"
}
},
"outputs": [],
"source": [
"pi = 3.1415926535\n",
@ -441,7 +541,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.091155Z",
"start_time": "2019-10-31T12:32:03.075545Z"
}
},
"outputs": [],
"source": [
"U = 12.0 #V\n",
@ -474,7 +579,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.106777Z",
"start_time": "2019-10-31T12:32:03.091155Z"
}
},
"outputs": [],
"source": [
"def Spannung(Widerstand, Strom): # U(R,I)\n",
@ -491,7 +601,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.122400Z",
"start_time": "2019-10-31T12:32:03.106777Z"
}
},
"outputs": [],
"source": [
"# Leider müssen wir hier auf die Einheiten selbst achten.\n",
@ -522,7 +637,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.138020Z",
"start_time": "2019-10-31T12:32:03.122400Z"
}
},
"outputs": [],
"source": [
"def Spannung(Strom, Widerstand):\n",
@ -556,7 +676,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.153639Z",
"start_time": "2019-10-31T12:32:03.138020Z"
}
},
"outputs": [],
"source": [
"Messwerte1 = ['Wert1', 'Wert2', 'Wert3'] # Variante 1\n",
@ -566,7 +691,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.169261Z",
"start_time": "2019-10-31T12:32:03.153639Z"
}
},
"outputs": [],
"source": [
"Messwerte2 = list([2, 0.9, '1']) # Variante 2\n",
@ -585,7 +715,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.184883Z",
"start_time": "2019-10-31T12:32:03.169261Z"
}
},
"outputs": [],
"source": [
"Messwerte1.append('Wert5')\n",
@ -595,7 +730,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.200504Z",
"start_time": "2019-10-31T12:32:03.184883Z"
}
},
"outputs": [],
"source": [
"Messwerte1.insert(4, 'Wert4')\n",
@ -614,7 +754,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.216125Z",
"start_time": "2019-10-31T12:32:03.200504Z"
}
},
"outputs": [],
"source": [
"NeueWerte = ['Wert1', 'Wert2', 'Wert3', 'Wert4', 'Wert5', 'Wert6'] "
@ -630,7 +775,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.231747Z",
"start_time": "2019-10-31T12:32:03.216125Z"
}
},
"outputs": [],
"source": [
"NeueWerte[0] # Hier seht ihr, dass der erste Wert den Index 0 hat."
@ -639,7 +789,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.247367Z",
"start_time": "2019-10-31T12:32:03.231747Z"
}
},
"outputs": [],
"source": [
"wert_index_2 = NeueWerte[2] \n",
@ -656,7 +811,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.262990Z",
"start_time": "2019-10-31T12:32:03.247367Z"
}
},
"outputs": [],
"source": [
"NeueWerte[0:3] "
@ -665,7 +825,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.278612Z",
"start_time": "2019-10-31T12:32:03.262990Z"
}
},
"outputs": [],
"source": [
"NeueWerte[2:5] # Ihr seht Python behandelt den letzten Wert wie in einem offenen Intervall [2,5)"
@ -674,7 +839,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.294235Z",
"start_time": "2019-10-31T12:32:03.278612Z"
}
},
"outputs": [],
"source": [
"NeueWerte[2:] # Hier werden alle Werte mit dem Index >= 2 zurück gegeben"
@ -683,7 +853,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.309853Z",
"start_time": "2019-10-31T12:32:03.294235Z"
}
},
"outputs": [],
"source": [
"NeueWerte[-3:] # Mit negativen Zahlen fangt ihr vom Ende der Liste an"
@ -699,7 +874,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.325477Z",
"start_time": "2019-10-31T12:32:03.309853Z"
}
},
"outputs": [],
"source": [
"Messwerte1 + NeueWerte"
@ -715,7 +895,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.341096Z",
"start_time": "2019-10-31T12:32:03.325477Z"
}
},
"outputs": [],
"source": [
"Messwerte1.append(NeueWerte)\n",
@ -732,7 +917,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.356717Z",
"start_time": "2019-10-31T12:32:03.341096Z"
}
},
"outputs": [],
"source": [
"Messwerte1 = Messwerte1 + NeueWerte\n",
@ -753,7 +943,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.372339Z",
"start_time": "2019-10-31T12:32:03.356717Z"
}
},
"outputs": [],
"source": [
"print(Messwerte1)\n",
@ -770,7 +965,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.387962Z",
"start_time": "2019-10-31T12:32:03.372339Z"
}
},
"outputs": [],
"source": [
"range(0, # <-- Startwert\n",
@ -789,7 +989,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.403581Z",
"start_time": "2019-10-31T12:32:03.387962Z"
}
},
"outputs": [],
"source": [
"list(range(0,5,2))"
@ -807,7 +1012,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.434825Z",
"start_time": "2019-10-31T12:32:03.403581Z"
}
},
"outputs": [],
"source": [
"liste = [1, 2, 3, 4]\n",
@ -828,7 +1038,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.466066Z",
"start_time": "2019-10-31T12:32:03.434825Z"
}
},
"outputs": [],
"source": [
"liste = [1, 2, 3, 4]\n",
@ -854,7 +1069,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.481687Z",
"start_time": "2019-10-31T12:32:03.466066Z"
}
},
"outputs": [],
"source": [
"Stromwerte = [101, 105, 98, 87, 112] # mA\n",
@ -877,7 +1097,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.497309Z",
"start_time": "2019-10-31T12:32:03.481687Z"
}
},
"outputs": [],
"source": [
"Spannungswerte = [Spannung(Strom, 1000) for Strom in Stromwerte]"
@ -893,7 +1118,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.512930Z",
"start_time": "2019-10-31T12:32:03.497309Z"
}
},
"outputs": [],
"source": [
"Werte1 = ['A', 'B', 'C', 'D']\n",
@ -913,7 +1143,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.528551Z",
"start_time": "2019-10-31T12:32:03.512930Z"
}
},
"outputs": [],
"source": [
"# Gemessene Werte:\n",
@ -943,7 +1178,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:03.544173Z",
"start_time": "2019-10-31T12:32:03.528551Z"
}
},
"outputs": [],
"source": [
"l1 = ['a', 'b', 'c']\n",
@ -998,7 +1238,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:05.934236Z",
"start_time": "2019-10-31T12:32:03.544173Z"
}
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt "
@ -1014,7 +1259,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:06.418498Z",
"start_time": "2019-10-31T12:32:05.934236Z"
}
},
"outputs": [],
"source": [
"plt.plot([1,2,3,4,5], # <-- x-Daten\n",
@ -1042,7 +1292,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:06.902759Z",
"start_time": "2019-10-31T12:32:06.418498Z"
}
},
"outputs": [],
"source": [
"xdaten = [1,2,3,4,5]\n",
@ -1086,7 +1341,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:07.340156Z",
"start_time": "2019-10-31T12:32:06.902759Z"
}
},
"outputs": [],
"source": [
"xdaten = [1,2,3,4,5]\n",
@ -1123,7 +1383,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:07.699447Z",
"start_time": "2019-10-31T12:32:07.340156Z"
}
},
"outputs": [],
"source": [
"xdaten = [-3, -2, -1, 0, 1, 2, 3]\n",
@ -1151,7 +1416,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:08.214951Z",
"start_time": "2019-10-31T12:32:07.699447Z"
}
},
"outputs": [],
"source": [
"def cubic(x):\n",
@ -1188,7 +1458,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:08.543000Z",
"start_time": "2019-10-31T12:32:08.214951Z"
}
},
"outputs": [],
"source": [
"spannung = [0.9, 2.0, 3.0, 4.1, 4.9, 6.2] # [V]\n",
@ -1233,7 +1508,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:08.949153Z",
"start_time": "2019-10-31T12:32:08.543000Z"
}
},
"outputs": [],
"source": [
"#plt.errorbar(, \n",
@ -1274,7 +1554,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:09.511521Z",
"start_time": "2019-10-31T12:32:08.949153Z"
}
},
"outputs": [],
"source": [
"plt.errorbar(strom, \n",
@ -1310,7 +1595,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:09.527151Z",
"start_time": "2019-10-31T12:32:09.511521Z"
}
},
"outputs": [],
"source": [
"import numpy as np"
@ -1319,7 +1609,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:09.558386Z",
"start_time": "2019-10-31T12:32:09.527151Z"
}
},
"outputs": [],
"source": [
"rnd_numbers = np.random.normal(0,1,1000) # <-- Hier werden 1000 gausförmig verteile Zufallszahlen\n",
@ -1337,7 +1632,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:10.167624Z",
"start_time": "2019-10-31T12:32:09.558386Z"
}
},
"outputs": [],
"source": [
"plt.hist(rnd_numbers)\n",
@ -1357,7 +1657,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:10.917447Z",
"start_time": "2019-10-31T12:32:10.167624Z"
}
},
"outputs": [],
"source": [
"rnd_numbers2 = np.random.normal(1,2,1000)\n",
@ -1395,7 +1700,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:12.307739Z",
"start_time": "2019-10-31T12:32:10.917447Z"
}
},
"outputs": [],
"source": [
"plt.hist(rnd_numbers, \n",
@ -1531,13 +1841,18 @@
"source": [
"Es gibt verschiedene Arten von Algorithmen um Minimierungsprobleme zu lösen. Wie diese genau Aufgebaut sind lernt ihr in anderen Progrmmierkursen wie zum Beispiel *Programmieren für Physiker* oder *Computer in der Wissenschaft*. Zum Glück haben uns bereits in Python andere Menschen diese Arbeit abgenommen und wir können aus dem Package `scipy.optimize` die Funktion `curve_fit` verwenden. \n",
"\n",
"Hierbei stellt curve_fit eine Methode dar, Fit-Funktionen nach der obigen vorgestellten Methode der *kleinsten Quadraten* zu bestimmen. Dies hat zur folge, dass lediglich die y-Fehler eurer Messwerte für den Fit verwendet werden können. "
"Hierbei stellt curve_fit eine Methode dar, Fit-Funktionen nach der obigen vorgestellten Methode der *kleinsten Quadraten* zu bestimmen. Dies hat zur Folge, dass lediglich die y-Fehler eurer Messwerte für den Fit verwendet werden können. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:13.947978Z",
"start_time": "2019-10-31T12:32:12.307739Z"
}
},
"outputs": [],
"source": [
"from scipy.optimize import curve_fit"
@ -1553,7 +1868,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:13.979221Z",
"start_time": "2019-10-31T12:32:13.947978Z"
}
},
"outputs": [],
"source": [
"# Und jetzt fitten wir:\n",
@ -1570,19 +1890,32 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Ihr seht die `curve_fit` gibt uns zwei unterschiedliche Listen zurück. Die erste Liste `para` beinhaltet die berechneten Fitparameter. `pcov` hingegen ist eine [Kovarianzmatrix](https://de.wikipedia.org/wiki/Kovarianzmatrix) auf deren Diagonalen ihr die Varianzen ($\\sigma^2$) der einzelnen Parameter findet (auf der Nebendiagonalen befinden sich die Kovarianzen, die euch aber im PGP nicht weiter zu kümmern brauchen). Im Hintergrund macht also curve_fit das folgende. Es probiert nach der Methode der kleinsten Quadrate etliche verschiedene Varianten mit unterschiedlichen Werten für R aus bis es den Wert gefunden hat, welcher am besten zu unsere Daten passt: \n",
"Ihr seht `curve_fit` gibt uns zwei unterschiedliche Listen zurück. Die erste Liste `para` beinhaltet die berechneten Fitparameter. `pcov` hingegen ist eine [Kovarianzmatrix](https://de.wikipedia.org/wiki/Kovarianzmatrix) auf deren Diagonalen ihr die Varianzen ($\\sigma^2$) der einzelnen Parameter findet (auf der Nebendiagonalen befinden sich die Kovarianzen). D.h. bei einer Funktion mit drei Parametern `def f(x, p1, p2, p3):` würde `para` und `pcov` allgemein so aussehen:\n",
"\n",
"```\n",
"para = [p1, p2, p3]\n",
"pcov = [[cov_1,1, cov_1,2, cov_1,3], \n",
" [cov_2,1, cov_2,2, cov_2,3],\n",
" [cov_3,1, cov_3,2, cov_3,3]]\n",
"```\n",
"wobei `cov_i,i` wie bereits erwähnt die einzelnen Kovarianzen bzw. Varianzen sind. Aber was genau macht jetzt curve_fit eigentlich um auf diese Werte zu kommen? Wie bereits erklärt basiert `curve_fit` auf der Methode der kleinsten Quadrate. D.h. die Funktion probiert etliche verschiedene Varianten eurer Parameter durch bis es die Kombination gefunden hat bei der das $\\chi^2$ klein wird. Gucken wir uns doch mal ein paar Zwischenschritte für unser Beispiel des ohm'schen Widerstandes an: \n",
"\n",
"<figure class=\"image\">\n",
"<img src=\"images/MaterialPythonkurs092018/Fitting_gif.gif\" alt=\"{{ Least Square Beispiel }}\" width=100%>\n",
"</figure>\n",
"\n",
"Lasst uns das Resultat mal darstellen:"
"Nach dem wir nun wissen, was genau `curve_fit` macht wollen wir unser Resultat etwas schöner darstellen:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:14.463482Z",
"start_time": "2019-10-31T12:32:13.979221Z"
}
},
"outputs": [],
"source": [
"plt.plot(strom, \n",
@ -1617,7 +1950,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:32:15.041472Z",
"start_time": "2019-10-31T12:32:14.463482Z"
}
},
"outputs": [],
"source": [
"para2, pcov2 = curve_fit(Spannung, \n",
@ -1628,7 +1966,6 @@
" # wir in der Regel absolute und keine relativen \n",
" # Unsicherheiten messen.\n",
" )\n",
"\n",
"plt.errorbar(strom, \n",
" spannung,\n",
" xerr=strom_error,\n",
@ -1679,12 +2016,17 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:54:51.185843Z",
"start_time": "2019-10-31T12:54:51.152000Z"
}
},
"outputs": [],
"source": [
"chi_2 = [ (u - Spannung(I, para2[0])/du)**2 for I,u,du in zip(strom, spannung, spannung_error)]\n",
"chi_2 = [ (u - Spannung(i, para2[0]))**2/du**2 for i,u,du in zip(strom, spannung, spannung_error)]\n",
"chi_2 = sum(chi_2)\n",
"print(f'Das chi-qudrat ist {chi_2:.0f}')"
"print(f'Das chi-qudrat ist {chi_2:.2f}')"
]
},
{
@ -1693,19 +2035,24 @@
"source": [
"Wie vergleicht sich dieses $\\chi^2$ nun mit einer Funktion welche unsere Daten schlechter beschreibt. Zum Beispiel sofern wir die Spannung über die Funktion \n",
"\n",
"$$ U(R,I) = R \\cdot I + C $$\n",
"$$ U(R,I) = R \\cdot I^2 $$\n",
"\n",
"beschreiben würden, also zusätzlich einem konstantem Offset."
"beschreiben würden."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2019-10-31T12:55:52.889042Z",
"start_time": "2019-10-31T12:55:52.873421Z"
}
},
"outputs": [],
"source": [
"def Spannung2(I, R, C):\n",
" return R * I + C \n",
"def Spannung2(I, R):\n",
" return R * I**2\n",
"\n",
"\n",
"\n",
@ -1716,9 +2063,9 @@
" absolute_sigma=True \n",
" )\n",
"\n",
"chi_2_new = [ (u - Spannung2(I, *para3)/du)**2 for I,u,du in zip(strom, spannung, spannung_error)]\n",
"chi_2_new = [ (u - Spannung2(I, *para3))**2/du**2 for I,u,du in zip(strom, spannung, spannung_error)]\n",
"chi_2_new = sum(chi_2_new)\n",
"print(f'Chi-qudrat nach URI: {chi_2:.0f}\\nChi-qudrat nach URIC: {chi_2_new:.0f}')"
"print(f'Chi-qudrat nach URI: {chi_2:.2f}\\nChi-qudrat nach URI-Parabel: {chi_2_new:.2f}')"
]
},
{