import matplotlib.pyplot as plt
h = 0.1 # 步长
yy = []
xx = []
yy.append(1)
xx.append(0)
def y1(m, n):
return m - 2 * n / m
for i in range(100):
k1 = y1(yy[i], xx[i])
y = yy[i] + h * y1(yy[i], xx[i])
xx.append(xx[i] + h)
k2 = y1(y, xx[i + 1])
yy.append(yy[i] + h * 1.0 / 2.0 * (k2 + k1))
print("y = ", yy[i], "x = %.1f" % xx[i])
plt.plot(xx, yy)
plt.show()
运行结果
"D:\py project\exer\venv\Scripts\python.exe" "D:/py project/exer/Euler.py"
y = 1 x = 0.0
y = 1.095909090909091 x = 0.1
y = 1.1840965692429972 x = 0.2
y = 1.2662013608757763 x = 0.3
y = 1.3433601514839986 x = 0.4
y = 1.4164019285369094 x = 0.5
y = 1.485955602415669 x = 0.6
y = 1.5525140913261455 x = 0.7
y = 1.6164747827520576 x = 0.8
y = 1.6781663636751858 x = 0.9
y = 1.7378674010354138 x = 1.0
y = 1.795819744910657 x = 1.1
y = 1.8522385990502925 x = 1.2
y = 1.9073204178377563 x = 1.3
y = 1.9612493906319577 x = 1.4
y = 2.0142030360599446 x = 1.5
y = 2.066357283013655 x = 1.6
y = 2.1178913249274696 x = 1.7
y = 2.1689924782789043 x = 1.8
y = 2.2198612426004756 x = 1.9
y = 2.2707167397657666 x = 2.0
y = 2.32180269966453 x = 2.1
y = 2.373394153728064 x = 2.2
y = 2.4258049938401265 x = 2.3
y = 2.479396548830104 x = 2.4
y = 2.5345873206656235 x = 2.5
y = 2.5918640039575958 x = 2.6
y = 2.651793881429891 x = 2.7
y = 2.715038640456334 x = 2.8
y = 2.7823695879838817 x = 2.9
y = 2.854684151012614 x = 3.0
y = 2.933023438205294 x = 3.1
y = 3.0185905110668165 x = 3.2
y = 3.1127688834370595 x = 3.3
y = 3.2171406572597903 x = 3.4
y = 3.3335036404708074 x = 3.5
y = 3.4638868141440735 x = 3.6
y = 3.6105636532638243 x = 3.7
y = 3.776063077959879 x = 3.8
y = 3.9631782135250067 x = 3.9
y = 4.17497362733908 x = 4.0
y = 4.414792214087622 x = 4.1
y = 4.686263321770158 x = 4.2
y = 4.993313958871075 x = 4.3
y = 5.340184940330469 x = 4.4
y = 5.731453613913819 x = 4.5
y = 6.172064414060677 x = 4.6
y = 6.667368012258599 x = 4.7
y = 7.223169376499369 x = 4.8
y = 7.845784702735553 x = 4.9
y = 8.54210698550867 x = 5.0
y = 9.319679958975971 x = 5.1
y = 10.186780237956588 x = 5.2
y = 11.152507679941843 x = 5.3
y = 12.226884230333251 x = 5.4
y = 13.420961769562055 x = 5.5
y = 14.746939729248727 x = 5.6
y = 16.21829347387518 x = 5.7
y = 17.84991465230495 x = 5.8
y = 19.658264913448832 x = 5.9
y = 21.661544558967204 x = 6.0
y = 23.879877880615012 x = 6.1
y = 26.33551710791327 x = 6.2
y = 29.053067079707077 x = 6.3
y = 32.05973295633575 x = 6.4
y = 35.385593512244014 x = 6.5
y = 39.06390279591596 x = 6.6
y = 43.13142321861966 x = 6.7
y = 47.628793439022616 x = 6.8
y = 52.60093475067547 x = 6.9
y = 58.097500057198566 x = 7.0
y = 64.17336993952766 x = 7.1
y = 70.88920078491002 x = 7.2
y = 78.31203046304113 x = 7.3
y = 86.51594760583606 x = 7.4
y = 95.58283117943135 x = 7.5
y = 105.60316773632505 x = 7.6
y = 116.67695450897538 x = 7.7
y = 128.9146973613376 x = 7.8
y = 142.4385135601939 x = 7.9
y = 157.38335037310645 x = 8.0
y = 173.89833165478166 x = 8.1
y = 192.14824586006776 x = 8.2
y = 212.315190332423 x = 8.3
y = 234.60038827554476 x = 8.4
y = 259.22619653847477 x = 8.5
y = 286.438324248066 x = 8.6
y = 316.50828442619735 x = 8.7
y = 349.7361030535278 x = 8.8
y = 386.4533126100826 x = 8.9
y = 427.0262599611741 x = 9.0
y = 471.8597615934031 x = 9.1
y = 521.4011426710401 x = 9.2
y = 576.1447002125335 x = 9.3
y = 636.6366349184326 x = 9.4
y = 703.4805008578703 x = 9.5
y = 777.3432273875706 x = 9.6
y = 858.9617733866818 x = 9.7
y = 949.1504801995546 x = 9.8
y = 1048.8091966498191 x = 9.9