<jupyter_start><jupyter_text><jupyter_code>import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-poster')<jupyter_output><empty_output><jupyter_text>Sistemas de ordem superiorNeste tutorial, estenderemos as técnicas vistas até aqui para resolver alguns tipos de problemas de EDOs de ordem superior.  Exemplo 1Resolva a EDO de segunda ordem dada por:\begin{equation}\begin{aligned}y^{\prime \prime}+y^{\prime}+y &=\cos (t) \\y(0) &=1 \\y^{\prime}(0) &=0\end{aligned}\end{equation}Queremos transformar a equação diferencial em um sistema de equações de primeira ordem, portanto, fazemos $y^{\prime} =w$ e obtemos um sistema de duas EDOs de primeira ordem:\begin{equation}\begin{aligned}y^{\prime} &=w \\w^{\prime} &=-w-y+\cos (t) \\y(0) &=1 \\w(0) &=0\end{aligned}\end{equation}<jupyter_code># Escrevemos o sistema de EDOs na nossa função derivadas:
def derivadas(t,X):
  F = np.zeros(2)
  F[0] = -X[1]
  F[1] = -X[1]-X[0]+np.cos(t)
  return F
# Parametros 

h = 1e-2
tfim = 10
nt = int(tfim/h)
y0 = 1
w0 = 0
s0 = [y0,w0]
def Euler_geral(derivadas,h,s0,tmax=10):
  nt = int(tmax/h)
  X = np.zeros([nt,2])
  tempo = np.linspace(0,tmax,nt)
  X[0,:] = s0
    
  for k in np.arange(1,nt):
      F = derivadas(tempo[k-1],X[k-1,:])
      X[k,0] = X[k-1,0] + h*F[0] 
      X[k,1] = X[k-1,1] + h*F[1]

  return tempo,X
tempo,X = Euler_geral(derivadas,h,[1,0],3)
  plt.figure(figsize = (12, 8))
  plt.plot(tempo,X[:,0], 'bo--', label='y')
  plt.plot(tempo,X[:,1], 'ko--', label='w')
  plt.xlabel('t')
  plt.ylabel('x,y')
  plt.grid()
  plt.legend(loc='lower left') 

  plt.figure(figsize = (12, 8))
  plt.plot(X[:,0],X[:,1], 'bo--', label='Numérica com h={}'.format(h))
  plt.xlabel('x')
  plt.ylabel('y')
  plt.grid()
  plt.legend(loc='lower left')
  plt.show() 
def Euler_mod_geral(derivadas,h,s0,tmax=10):
  nt = int(tmax/h)
  X = np.zeros([nt,2])
  tempo = np.linspace(0,tmax,nt)
  X[0,:] = s0
    
  for k in np.arange(1,nt):
      F = derivadas(tempo[k-1],X[k-1,:])
      kx1 = F[0] 
      ky1 = F[1]

      x_euler = X[k-1,0] + h*kx1
      y_euler = X[k-1,1] + h*ky1

      Fnew = derivadas(tempo[k-1],[x_euler,y_euler])

      kx2 = Fnew[0] 
      ky2 = Fnew[1]

      X[k,0] = X[k-1,0] + h*(kx1+kx2)/2 
      X[k,1] = X[k-1,1] + h*(ky1+ky2)/2
  return tempo,X
tempo,X = Euler_mod_geral(derivadas,h,[1,0],3)
  plt.figure(figsize = (12, 8))
  plt.plot(tempo,X[:,0], 'bo--', label='y')
  plt.plot(tempo,X[:,1], 'ko--', label='w')
  plt.xlabel('t')
  plt.ylabel('x,y')
  plt.grid()
  plt.legend(loc='lower left') 
  
  plt.figure(figsize = (12, 8))
  plt.plot(X[:,0],X[:,1], 'bo--', label='Numérica com h={}'.format(h))
  plt.xlabel('x')
  plt.ylabel('y')
  plt.grid()
  plt.legend(loc='lower left')
  plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.21988388969521044, 'fraction_numerical': 0.038824383164005806, 'mean_word_length': 3.7698961937716264, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '258832', 'license': 'MIT', 'n_tokens_mistral': 1334, 'n_tokens_neox': 1291, 'n_words': 236, 'path': 'SC_EDOs_OrdemSuperior.ipynb', 'repo_name': 'aschelin/SimulacoesAGFE', 'size': '2756'}