def main():
    d = {}
    x_0 = 1.0
    y_0 = 1.0
    x_n = 2.0
    step_size = 0.1
    list0 = euler(x_0, y_0, x_n, 0.1)
    list1 = euler(x_0, y_0, x_n, 0.05)
    list2 = euler(x_0, y_0, x_n, 0.025)
    list3 = improved_euler(x_0, y_0, x_n, 0.1)
    list4 = improved_euler(x_0, y_0, x_n, 0.05)
    list5 = improved_euler(x_0, y_0, x_n, 0.025)
    for i in np.arange(x_0, x_n + step_size, step_size):
        y0 = [point[1] for point in list0 if math.isclose(point[0], i)][0]
        y1 = [point[1] for point in list1 if math.isclose(point[0], i)][0]
        y2 = [point[1] for point in list2 if math.isclose(point[0], i)][0]
        y3 = [point[1] for point in list3 if math.isclose(point[0], i)][0]
        y4 = [point[1] for point in list4 if math.isclose(point[0], i)][0]
        y5 = [point[1] for point in list5 if math.isclose(point[0], i)][0]
        y6 = exact(i)
        entry = [y0, y1, y2, y3, y4, y5, y6]
        d[i] = entry

    with pd.option_context('display.precision', 9):
        datFrame = pd.DataFrame.from_dict(d, orient='index', 
                                          columns=['h = 0.1', 'h = 0.05', 
                                                   'h = 0.025', 'h = 0.1',
                                                   'h = 0.05', 'h = 0.025',
                                                   'Exact'])
        print(datFrame.to_latex())
Esempio n. 2
0
 def test_euler_20(self):
     self.assertEqual(euler(0, 0, 0, 0.1), (0, 0))
Esempio n. 3
0
 def test_euler_10(self):
     self.assertEqual(euler(0.2, 0, 1, 0.1),
                      (0.9999999999999999, 0.3522851585103483))
Esempio n. 4
0
 def test_euler_50(self):
     self.assertEqual(euler(0, 0, 0.1, 0.0001),
                      (0.10000000000000184, 0.0009990199006611853))
Esempio n. 5
0
 def test_euler_40(self):
     self.assertEqual(euler(-5, 2.1, 0, 0.1),
                      (4.200000000000001, 0.42884703254758155))
Esempio n. 6
0
 def test_euler_30(self):
     self.assertEqual(euler(0, 0, -1, 0.1),
                      (0.9999999999999999, 0.09117146625980643))
Esempio n. 7
0
while 1:
    try:
        y0 = float(input("Enter y0 (from -1000000000 to 1000000000): "))
        if y0 < -1000000000 or y0 > 1000000000:
            print("You entered wrong data\n")
        else:
            break
    except ValueError:
        print("You entered wrong data\n")

while 1:
    try:
        h = float(input("Enter step (from 0.0000001 to 10): "))
        if h < 0.0000001 or h > 10 or h > abs(xn - x0):
            print("You entered wrong data\n")
        else:
            break
    except ValueError:
        print("You entered wrong data\n")

if xn < x0:
    h = -h

x, y = euler(y0, x0, xn, h)

print(
    "\nSolution of differential equation y' = 0.2*x + y^2 with initial condition y(",
    x0, ") = ", y0, " :")
print("\n x = ", x)
print("\n y = ", y)