Shared2017-10-29-145202_Equipo_8.ipynbOpen in CoCalc
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
def dx(x1,x2,t):
    A=np.array([[-1,0],[-1,2]])
    B=np.array([[x1],[x2]])
    return A.dot(B)+np.array([[4*t+1],[0]])
def euler(dx,t0,x0,y0,t1,n):
    return np.array([[x0],[y0]])+(float(t1-t0)/float(n))*dx(x0,y0,t0)
def x(t):
    return np.array([[4*np.exp(-t)+4*t-3],[(6**(-1))*np.exp(2*t)+(float(4)/float(3)*np.exp(-t)+2*t-(2**(-1)))]])
x0=1
y0=1
t1=1
t0=0
n=10
X=[1]
Y=[1]
for i in range (0,10,1):
    e=euler(dx,t0+i*(float(t1-t0)/float(n)),x0,y0,t1,n-i)
    print(e)
    x0=e[0][0]
    y0=e[1][0]
    X=X+[e[0][0]]
    Y=Y+[e[1][0]]
print(X)
print(Y)
plt.plot(X,Y,color="blue",label=u"Numérico")
X1=[]
Y1=[]
for j in range(0,11,1):
    a=x(t0+j*(.1))
    X1=X1+[a[0][0]]
    Y1=Y1+[a[1][0]]
plt.plot(X1,Y1,color="red",label=u"Analítico")
plt.xlabel("X")
plt.ylabel("Y")
leg = plt.legend(loc='best', ncol=1, fancybox=True)
plt.grid(True)
[[ 1. ] [ 1.1]] [[ 1.04] [ 1.22]] [[ 1.116] [ 1.36 ]] [[ 1.2244] [ 1.5204]] [[ 1.36196] [ 1.70204]] [[ 1.525764] [ 1.906252]] [[ 1.7131876] [ 2.134926 ]] [[ 1.92186884] [ 2.39059244]] [[ 2.14968196] [ 2.67652404]] [[ 2.39471376] [ 2.99686066]] [1, 1.0, 1.04, 1.1160000000000001, 1.2244000000000002, 1.3619600000000001, 1.5257640000000001, 1.7131876000000001, 1.9218688400000001, 2.1496819560000002, 2.3947137604000002] [1, 1.1000000000000001, 1.2200000000000002, 1.3600000000000003, 1.5204000000000004, 1.7020400000000004, 1.9062520000000005, 2.1349260000000005, 2.3905924400000007, 2.6765240440000007, 2.9968606572000009]
T=[]
for j in range(0,11,1):
    a=t0+j*(.1)
    T=T+[a]
plt.plot(T,X,color="blue",label=u"Numérico")
plt.plot(T,X1,color="red",label=u"Analítico")
leg = plt.legend(loc='best', ncol=1, fancybox=True)
plt.xlabel("T")
plt.ylabel("X")
plt.grid(True)
plt.plot(T,Y,color="blue",label=u"Numérico")
plt.plot(T,Y1,color="red",label=u"Analítico")
plt.xlabel("T")
plt.ylabel("Y")
leg = plt.legend(loc='best', ncol=1, fancybox=True)
plt.grid(True)
def dx(x1,x2,t):
    A=np.array([[2-t,2*t-2],[1-t,2*t-1]])
    B=np.array([[x1],[x2]])
    return A.dot(B)+np.array([[t],[t]])
def euler(dx,t0,x0,y0,t1,n):
    return np.array([[x0],[y0]])+(float(t1-t0)/float(n))*dx(x0,y0,t0)
def x(t):
    return np.array([[4*np.exp(t)-1],[2*np.exp(t)-1]])
x0=3
y0=1
t1=1
t0=0
n=10
X=[3]
Y=[1]
for i in range (0,n,1):
    e=euler(dx,t0+i*(float(t1-t0)/float(n)),x0,y0,t1,n-i)
    x0=e[0][0]
    y0=e[1][0]
    X=X+[e[0][0]]
    Y=Y+[e[1][0]]
print("Numérico")
print(X,Y)
plt.plot(X,Y,color="blue",label=u"Numérico")
X1=[]
Y1=[]
for j in range(0,n+1,1):
    a=x(t0+j*(.1))
    X1=X1+[a[0][0]]
    Y1=Y1+[a[1][0]]
print("Analítico")
print(X1,Y1)
plt.plot(X1,Y1,color="red",label=u"Analítico")
plt.xlabel("X")
plt.ylabel("Y")
leg = plt.legend(loc='best', ncol=1, fancybox=True)
plt.grid(True)
Numérico ([3, 3.3999999999999999, 3.8399999999999999, 4.3239999999999998, 4.8563999999999998, 5.4420399999999995, 6.0862439999999998, 6.7948683999999995, 7.5743552399999992, 8.4317907639999987, 9.3749698403999986], [1, 1.2, 1.4199999999999999, 1.6619999999999999, 1.9281999999999999, 2.2210199999999998, 2.5431219999999999, 2.8974341999999997, 3.2871776199999996, 3.7158953819999994, 4.1874849201999993]) Analítico ([3.0, 3.4206836723025908, 3.8856110326406794, 4.3994352303040127, 4.9672987905650814, 5.5948850828005128, 6.2884752015620364, 7.0550108298819065, 7.9021637139698715, 8.8384124446277994, 9.8731273138361804], [1.0, 1.2103418361512954, 1.4428055163203397, 1.6997176151520064, 1.9836493952825407, 2.2974425414002564, 2.6442376007810182, 3.0275054149409533, 3.4510818569849357, 3.9192062223138997, 4.4365636569180902])
T=[]
for j in range(0,11,1):
    a=t0+j*(.1)
    T=T+[a]
plt.plot(T,X,color="blue",label=u"Numérico")
plt.plot(T,X1,color="red",label=u"Analítico")
leg = plt.legend(loc='best', ncol=1, fancybox=True)
plt.xlabel("T")
plt.ylabel("X")
plt.grid(True)
plt.plot(T,Y,color="blue",label=u"Numérico")
plt.plot(T,Y1,color="red",label=u"Analítico")
plt.xlabel("T")
plt.ylabel("Y")
leg = plt.legend(loc='best', ncol=1, fancybox=True)
plt.grid(True)
ax = plt.subplot(111)
t1 = np.arange(0.0, 1.0, 0.01)
for n in [1, 2, 3, 4]:
    plt.plot(t1,t1**n,label="n=%d"%(n,))

leg = plt.legend(loc='best', ncol=2, mode="expand", shadow=True, fancybox=True)
leg.get_frame().set_alpha(0.5)
plt.xlabel("T")
plt.ylabel("X")
plt.grid(True)


plt.show()