Skip to content

Commit c8af7c6

Browse files
committed
Finishing book 2
1 parent a47ee3f commit c8af7c6

File tree

1 file changed

+207
-12
lines changed

1 file changed

+207
-12
lines changed

notebook/Python-2.ipynb

Lines changed: 207 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,7 @@
11151115
},
11161116
{
11171117
"cell_type": "code",
1118-
"execution_count": 40,
1118+
"execution_count": 50,
11191119
"metadata": {
11201120
"collapsed": false
11211121
},
@@ -1401,7 +1401,7 @@
14011401
},
14021402
{
14031403
"cell_type": "code",
1404-
"execution_count": 19,
1404+
"execution_count": 4,
14051405
"metadata": {
14061406
"collapsed": false
14071407
},
@@ -1424,7 +1424,7 @@
14241424
}
14251425
],
14261426
"source": [
1427-
"F = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 10]\n",
1427+
"\n",
14281428
"for f in range(0,100,10):\n",
14291429
" C=(f-32)/(9.0/5.0)\n",
14301430
" print f, C"
@@ -1803,7 +1803,7 @@
18031803
},
18041804
{
18051805
"cell_type": "code",
1806-
"execution_count": 60,
1806+
"execution_count": 1,
18071807
"metadata": {
18081808
"collapsed": false
18091809
},
@@ -1938,17 +1938,55 @@
19381938
"Step 2: Once step 1 is working, modify the program so that the $t$ and $y$ values are stored in two lists *t* and *y*. Thereafter, transverse the lists with a *for* loop and write out a nicely formatted table of *t* and *y* values using a *zip* construction."
19391939
]
19401940
},
1941+
{
1942+
"cell_type": "code",
1943+
"execution_count": 68,
1944+
"metadata": {
1945+
"collapsed": false
1946+
},
1947+
"outputs": [
1948+
{
1949+
"name": "stdout",
1950+
"output_type": "stream",
1951+
"text": [
1952+
"0.02 0.017\n",
1953+
"0.04 0.030\n",
1954+
"0.06 0.040\n",
1955+
"0.07 0.047\n",
1956+
"0.09 0.051\n",
1957+
"0.11 0.051\n",
1958+
"0.13 0.047\n",
1959+
"0.15 0.040\n",
1960+
"0.17 0.030\n",
1961+
"0.19 0.017\n"
1962+
]
1963+
}
1964+
],
1965+
"source": [
1966+
"v=1\n",
1967+
"n=11\n",
1968+
"g=9.81\n",
1969+
"step=(2*v/g)/n\n",
1970+
"i=[t for t in range(1,n)]\n",
1971+
"t = [step*T for T in i]\n",
1972+
"y = [v*T - 0.5*g*T*T for T in t]\n",
1973+
"table = [[T, Y] for T, Y in zip(t, y)]\n",
1974+
"for T,Y in table:\n",
1975+
" #print \"\"%5.d, %3.2f\"\" % (T,Y)\n",
1976+
" print '%.2f %10.3f' % (T, Y)"
1977+
]
1978+
},
19411979
{
19421980
"cell_type": "markdown",
19431981
"metadata": {},
19441982
"source": [
1945-
"##Functions\n",
1983+
"## Functions\n",
19461984
"We have already used many Python functions, e.g. mathematical functions:"
19471985
]
19481986
},
19491987
{
19501988
"cell_type": "code",
1951-
"execution_count": 65,
1989+
"execution_count": 2,
19521990
"metadata": {
19531991
"collapsed": false
19541992
},
@@ -1976,11 +2014,23 @@
19762014
},
19772015
{
19782016
"cell_type": "code",
1979-
"execution_count": 66,
2017+
"execution_count": 3,
19802018
"metadata": {
19812019
"collapsed": false
19822020
},
1983-
"outputs": [],
2021+
"outputs": [
2022+
{
2023+
"ename": "NameError",
2024+
"evalue": "name 'somelist' is not defined",
2025+
"output_type": "error",
2026+
"traceback": [
2027+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
2028+
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
2029+
"\u001b[1;32m<ipython-input-3-0751f24efd2a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mn\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msomelist\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mints\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mn\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
2030+
"\u001b[1;31mNameError\u001b[0m: name 'somelist' is not defined"
2031+
]
2032+
}
2033+
],
19842034
"source": [
19852035
"n = len(somelist)\n",
19862036
"ints = range(5, n, 2)"
@@ -2396,12 +2446,136 @@
23962446
},
23972447
{
23982448
"cell_type": "code",
2399-
"execution_count": null,
2449+
"execution_count": 113,
24002450
"metadata": {
24012451
"collapsed": false
24022452
},
2403-
"outputs": [],
2404-
"source": []
2453+
"outputs": [
2454+
{
2455+
"name": "stdout",
2456+
"output_type": "stream",
2457+
"text": [
2458+
"0.0000024390\n",
2459+
"0.0000039613\n",
2460+
"0.0000063698\n",
2461+
"0.0000101409\n",
2462+
"0.0000159837\n",
2463+
"0.0000249425\n",
2464+
"0.0000385352\n",
2465+
"0.0000589431\n",
2466+
"0.0000892617\n",
2467+
"0.0001338302\n",
2468+
"0.0001986555\n",
2469+
"0.0002919469\n",
2470+
"0.0004247803\n",
2471+
"0.0006119019\n",
2472+
"0.0008726827\n",
2473+
"0.0012322192\n",
2474+
"0.0017225689\n",
2475+
"0.0023840882\n",
2476+
"0.0032668191\n",
2477+
"0.0044318484\n",
2478+
"0.0059525324\n",
2479+
"0.0079154516\n",
2480+
"0.0104209348\n",
2481+
"0.0135829692\n",
2482+
"0.0175283005\n",
2483+
"0.0223945303\n",
2484+
"0.0283270377\n",
2485+
"0.0354745928\n",
2486+
"0.0439835960\n",
2487+
"0.0539909665\n",
2488+
"0.0656158148\n",
2489+
"0.0789501583\n",
2490+
"0.0940490774\n",
2491+
"0.1109208347\n",
2492+
"0.1295175957\n",
2493+
"0.1497274656\n",
2494+
"0.1713685920\n",
2495+
"0.1941860550\n",
2496+
"0.2178521770\n",
2497+
"0.2419707245\n",
2498+
"0.2660852499\n",
2499+
"0.2896915528\n",
2500+
"0.3122539334\n",
2501+
"0.3332246029\n",
2502+
"0.3520653268\n",
2503+
"0.3682701403\n",
2504+
"0.3813878155\n",
2505+
"0.3910426940\n",
2506+
"0.3969525475\n",
2507+
"0.3989422804\n",
2508+
"0.3969525475\n",
2509+
"0.3910426940\n",
2510+
"0.3813878155\n",
2511+
"0.3682701403\n",
2512+
"0.3520653268\n",
2513+
"0.3332246029\n",
2514+
"0.3122539334\n",
2515+
"0.2896915528\n",
2516+
"0.2660852499\n",
2517+
"0.2419707245\n",
2518+
"0.2178521770\n",
2519+
"0.1941860550\n",
2520+
"0.1713685920\n",
2521+
"0.1497274656\n",
2522+
"0.1295175957\n",
2523+
"0.1109208347\n",
2524+
"0.0940490774\n",
2525+
"0.0789501583\n",
2526+
"0.0656158148\n",
2527+
"0.0539909665\n",
2528+
"0.0439835960\n",
2529+
"0.0354745928\n",
2530+
"0.0283270377\n",
2531+
"0.0223945303\n",
2532+
"0.0175283005\n",
2533+
"0.0135829692\n",
2534+
"0.0104209348\n",
2535+
"0.0079154516\n",
2536+
"0.0059525324\n",
2537+
"0.0044318484\n",
2538+
"0.0032668191\n",
2539+
"0.0023840882\n",
2540+
"0.0017225689\n",
2541+
"0.0012322192\n",
2542+
"0.0008726827\n",
2543+
"0.0006119019\n",
2544+
"0.0004247803\n",
2545+
"0.0002919469\n",
2546+
"0.0001986555\n",
2547+
"0.0001338302\n",
2548+
"0.0000892617\n",
2549+
"0.0000589431\n",
2550+
"0.0000385352\n",
2551+
"0.0000249425\n",
2552+
"0.0000159837\n",
2553+
"0.0000101409\n",
2554+
"0.0000063698\n",
2555+
"0.0000039613\n",
2556+
"0.0000024390\n",
2557+
"0.0000014867\n"
2558+
]
2559+
}
2560+
],
2561+
"source": [
2562+
"import math\n",
2563+
"from math import exp, pi\n",
2564+
"import numpy as np\n",
2565+
"\n",
2566+
"def gauss (x, m=0, s=1):\n",
2567+
" r=1/(((2*pi)**0.5)*s)\n",
2568+
" return exp(-0.5*((x-m)/s)**2)*r\n",
2569+
"\n",
2570+
"n=int(10/0.1)\n",
2571+
"N=[i for i in range(1,n+1)]\n",
2572+
"x = [-5+0.1*i for i in N]\n",
2573+
"\n",
2574+
"F=[gauss(X) for X in x]\n",
2575+
"\n",
2576+
"for i in N:\n",
2577+
" print \"%.10f\" % F[i-1]\n"
2578+
]
24052579
},
24062580
{
24072581
"cell_type": "markdown",
@@ -2516,10 +2690,31 @@
25162690
},
25172691
{
25182692
"cell_type": "code",
2519-
"execution_count": null,
2693+
"execution_count": 114,
25202694
"metadata": {
25212695
"collapsed": false
25222696
},
2697+
"outputs": [
2698+
{
2699+
"name": "stdout",
2700+
"output_type": "stream",
2701+
"text": [
2702+
"0 1 1\n"
2703+
]
2704+
}
2705+
],
2706+
"source": [
2707+
"def H(x):\n",
2708+
" return 0 if x <0 else 1\n",
2709+
"print H(-3), H(0), H(5)"
2710+
]
2711+
},
2712+
{
2713+
"cell_type": "code",
2714+
"execution_count": null,
2715+
"metadata": {
2716+
"collapsed": true
2717+
},
25232718
"outputs": [],
25242719
"source": []
25252720
}

0 commit comments

Comments
 (0)