Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
# Hello World program in Python
# Euler method in Python -
from __future__ import division
from math import exp
import matplotlib.pyplot as plt
import numpy as np
h=0.01
x0=0
y0=1
xmax=4
x=x0
y=y0
print "Euler method\n"
puntsEuler=[]
while x<=xmax:
puntsEuler.append((x,y))
print x,y
k1=(-2*x**3+12*x**2-20*x+8.5)
y = y + h*k1
x = x + h
#puntsEuler.append((x,y))
print x,y
print puntsEuler
print '=== Euler ===='
for i in range(len(puntsEuler)):
print puntsEuler[i][0],' --> ',puntsEuler[i][1]
print
print '============='
# dibuixant els punts i la solucio
def f(z):
return -0.5*z**4+4*z**3-10*z**2+8.5*z+1
te, eu = zip(*puntsEuler)
#tr, rk = zip(*puntsRK)
t1 = np.arange(x0, xmax, h/10)
plt.plot(te,eu,'b-',marker='o')
#plt.plot(tr,rk,'g-',marker='*')
plt.plot(t1,f(t1),'r')
plt.show()
Advertisements
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
AcceptLearn more