In [8]:
import numpy as numpy
import matplotlib as plt
import pandas as pd
In [3]:
#Euler = y(x+h) = y(x) + h*f(x, y(x))
#looking at table A4.2, step size of 0.001 seems optimal
#y = cos t, harmonic oscilator
In [9]:
step_vals = numpy.linspace(0.0001, 0.005, 100) #not quite sure how the step sizes - todo: ask TA
In [10]:
def deriv(x1, x2):
    return [x2, -x1]
In [11]:
avg_error = [];
final_error = [];
In [12]:
for step in step_vals:
    y_0 = 1
    y_1 = 0
    t = numpy.arange(0, 100*numpy.pi, step)
    yy_0 = []
    yy_1 = []
    true_vals = []
    errors = []
    for item in t:
        [dy_0, dy_1] = deriv(y_0, y_1)
        y_0 = y_0 + step*dy_0
        y_1 = y_1 + step*dy_1
        yy_0.append(y_0)
        yy_1.append(y_1)
        true_val = numpy.cos(item)
        true_vals.append(true_val)
        error = abs(y_0-true_val)
        errors.append(error)
    final_error.append(error)
    avg_error.append(numpy.mean(errors))
In [13]:
df = pd.DataFrame();
In [15]:
df['step-values'] = pd.Series(step_vals);
In [16]:
df['final-error'] = pd.Series(final_error);
In [17]:
df['avg-error'] = pd.Series(avg_error);
In [18]:
df #the results confirm the table
Out[18]:
step-values final-error avg-error
0 0.000100 0.015832 0.005028
1 0.000149 0.023761 0.007536
2 0.000199 0.031751 0.010057
3 0.000248 0.039804 0.012591
4 0.000298 0.047919 0.015138
5 0.000347 0.056098 0.017699
6 0.000397 0.064341 0.020273
7 0.000446 0.072648 0.022861
8 0.000496 0.081020 0.025462
9 0.000545 0.089458 0.028076
10 0.000595 0.097961 0.030704
11 0.000644 0.106530 0.033347
12 0.000694 0.115167 0.036002
13 0.000743 0.123870 0.038672
14 0.000793 0.132642 0.041356
15 0.000842 0.141483 0.044054
16 0.000892 0.150392 0.046766
17 0.000941 0.159371 0.049492
18 0.000991 0.168419 0.052233
19 0.001040 0.177539 0.054988
20 0.001090 0.186729 0.057758
21 0.001139 0.195992 0.060542
22 0.001189 0.205326 0.063341
23 0.001238 0.214734 0.066154
24 0.001288 0.224215 0.068983
25 0.001337 0.233770 0.071826
26 0.001387 0.243399 0.074685
27 0.001436 0.253104 0.077558
28 0.001486 0.262885 0.080447
29 0.001535 0.272742 0.083351
... ... ... ...
70 0.003565 0.750555 0.216776
71 0.003614 0.764219 0.220417
72 0.003664 0.777989 0.224069
73 0.003713 0.791866 0.227744
74 0.003763 0.805851 0.231439
75 0.003812 0.819945 0.235155
76 0.003862 0.834151 0.238893
77 0.003911 0.848464 0.242645
78 0.003961 0.862893 0.246428
79 0.004010 0.877429 0.250217
80 0.004060 0.892083 0.254036
81 0.004109 0.906852 0.257876
82 0.004159 0.921734 0.261735
83 0.004208 0.936736 0.265621
84 0.004258 0.951852 0.269522
85 0.004307 0.967086 0.273444
86 0.004357 0.982436 0.277386
87 0.004406 0.997909 0.281353
88 0.004456 1.013503 0.285342
89 0.004505 1.029213 0.289348
90 0.004555 1.045059 0.293389
91 0.004604 1.061017 0.297438
92 0.004654 1.077099 0.301511
93 0.004703 1.093317 0.305615
94 0.004753 1.109651 0.309733
95 0.004802 1.126120 0.313881
96 0.004852 1.142705 0.318041
97 0.004901 1.159427 0.322230
98 0.004951 1.176294 0.326454
99 0.005000 1.193268 0.330679

100 rows × 3 columns

In [ ]: