Wednesday, 26 August 2020

python: Initial condition in solving differential equation

I want to solve this differential equation: y′′+2y′+2y=cos(2x) with initial conditions:

  1. y(1)=2,y′(2)=0.5

  2. y′(1)=1,y′(2)=0.8

  3. y(1)=0,y(2)=1

and it's code is:

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
def dU_dx(U, x):
    return [U[1], -2*U[1] - 2*U[0] + np.cos(2*x)]
U0 = [1,0]
xs = np.linspace(0, 10, 200)
Us = odeint(dU_dx, U0, xs)
ys = Us[:,0]
plt.xlabel("x")
plt.ylabel("y")
plt.title("Damped harmonic oscillator")
plt.plot(xs,ys);

how can I fulfill it?



from python: Initial condition in solving differential equation

No comments:

Post a Comment