Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Método de Euler

Views: 246
# -*- coding: utf-8 -*- # Método de Euler import numpy as np import matplotlib.pyplot as plt #import math def function(x,y): f=-x/y return f def sol_exacta(x,y): g=sqrt(64-x**2) return g x0=0 xf=8 y0=8 n=10 h=(xf-x0)/n x=np.zeros(n) y=np.zeros(n) ye=np.zeros(n) x[0]=x0 y[0]=y0 ye[0]=y0 print('x','y_euler','y_exacta') print(x[0],y[0],ye[0]) for i in range(n-1): x[i+1]=x[i]+h y[i+1]=y[i]+h*function(x[i],y[i]) ye[i+1]=sol_exacta(x[i+1],y[i+1]) print(x[i+1],y[i+1],ye[i+1]) t=np.linspace(x0,xf,1000) plt.plot(x,y,'b-') plt.plot(t,sol_exacta(t,y),'g-') plt.grid(true) plt.show()
x y_euler y_exacta 0.0 8.0 8.0 0.8 8.0 7.95989949685296 1.6 7.92 7.83836717690617 2.4000000000000004 7.758383838383838 7.631513611335565 3.2 7.510909616948571 7.332121111929344 4.0 7.170072071223977 6.928203230275509 4.8 6.723772512696145 6.4 5.6 6.152664553176526 5.71314274283428 6.3999999999999995 5.424524742972336 4.800000000000001 7.199999999999999 4.480663254160225 3.4871191548325404 [<matplotlib.lines.Line2D object at 0x7f15c8469670>] [<matplotlib.lines.Line2D object at 0x7f15c84fe9d0>]