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.
Tutorialspoint

euler2

# Euler method in Python - 

from __future__ import division
from math import exp
import matplotlib.pyplot as plt
import numpy as np

h=0.1
x0=0
y0=0
xmax=0.5
x=x0
y=y0

print "Euler method\n"
puntsEuler=[]
while x<=xmax:
    puntsEuler.append((x,y))
    print x,y
    k1=(-2*x*y+2*x*exp(-x**2))
    y = y + h*k1
    x = x + h

# dibuixant els punts i la solucio 
def f(z):
    return np.exp(-z*z)*z*z

te, eu = zip(*puntsEuler)
t1 = np.arange(x0, xmax, h/10)

plt.plot(te,eu,'b-',marker='o')
plt.plot(t1,f(t1),'r')
plt.show()

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.