Vous êtes sur la page 1sur 3

20/2/2019 Kuta method.

ipynb - Colaboratory

import numpy as np
import matplotlib.pyplot as plt
import math

%matplotlib inline

def funcion_1(x,y):
dydx = 2*y* math.cos(x)
return dydx

def rk4(f,y0,a,b,h):
"""
f : funcion que calcula dYdt
y0: valor inicial

El tiempo en donde resolver de divide en un intervalo [a,b]


entonces, a y b son los extremos

h: el paso-incremento para llegar de "a" hasta "b"


"""
x,y = a,y0

x_record = []
y_record = []

while x <= b:
x_record.append(x)
y_record.append(y)

x += h
#k son pendientes
k1= f(x,y)
k2= f(x+h, y+h * k1)

y += h/2 * ( k1+k2)

return [np.array(x_record), np.array(y_record)]

y_inicial = 1
x_inicial = 0
x_final = 100
step = 0.05

x, y_rk4 =rk4(funcion_1,y_inicial,x_inicial,x_final,step)

y_analitico = np.exp(2*np.sin(x))

plt.plot(x,y_rk4,c='green', label='RK4')
plt.plot(x, y_analitico, c='pink', label='Analitico')
plt.legend()

https://colab.research.google.com/drive/1lIZ1rD4iPcs1YQ1IIAE3jko398CQrGwU#scrollTo=BBMArKrGVtRw&printMode=true 1/3
20/2/2019 Kuta method.ipynb - Colaboratory

<matplotlib.legend.Legend at 0x7f532aaa64a8>

y_inicial = 1
x_inicial = 0
x_final = 100
step = .25

x, y_rk4 =rk4(funcion_1,y_inicial,x_inicial,x_final,step)

y_analitico = np.exp(2*np.sin(x))

plt.plot(x,y_rk4,c='green', label='RK4')
plt.plot(x, y_analitico, c='pink', label='Analitico')
plt.legend()

<matplotlib.legend.Legend at 0x7f532aa0f358>

y_inicial = 1
x_inicial = 0
x_final = 100
step = .1

x, y_rk4 =rk4(funcion_1,y_inicial,x_inicial,x_final,step)

y_analitico = np.exp(2*np.sin(x))

plt.plot(x,y_rk4,c='green', label='RK4')
plt.plot(x, y_analitico, c='pink', label='Analitico')
plt.legend()

https://colab.research.google.com/drive/1lIZ1rD4iPcs1YQ1IIAE3jko398CQrGwU#scrollTo=BBMArKrGVtRw&printMode=true 2/3
20/2/2019 Kuta method.ipynb - Colaboratory

<matplotlib.legend.Legend at 0x7f532ac35828>

https://colab.research.google.com/drive/1lIZ1rD4iPcs1YQ1IIAE3jko398CQrGwU#scrollTo=BBMArKrGVtRw&printMode=true 3/3

Vous aimerez peut-être aussi