Systems of differential equations

1

I'm doing the system of differential equations in Python but I do not understand how to make the solution appear.

%matplotlib inline 

import matplotlib.pyplot as plt
import sympy 
import numpy as np 
from scipy import integrate 

x = sympy.Symbol('x')
y = sympy.Function('y')

a=-5
b=-1
c=4
d=-1

    def ODE(a,b,c,t):

        f = a*x+b*y
        g = c*x+c*d
         init_printing(use_unicode=True)
             return f,g




sympy.Eq(x(x).diff(t), f)
sympy.Eq(y(x).diff(t), g)

Do I have something wrong with the code? and how could I solve it?

    
asked by Alejandro Fandiño 18.02.2018 в 03:45
source

1 answer

1

It is difficult to know the type of ODE you are trying to solve. If you add a .tex image of your ODE, it would help a lot.

However, I get the impression that you are trying to solve ODE coupled, in this case here you find a very simple example of how to solve it

# Very simple example showing how to solve ODE systems

# 1. Import libraries
import numpy as np
import scipy as sci
import matplotlib.pyplot as plt

# 2. Preliminaries. The ODE system
# dy/dt = v
# dv/dt = -g

# 3. Include the ODE in a function


def ode(x, t):
    v = x[0]
    g = x[1]
    dydt = v
    dvdt = -g
    return(dydt, dvdt)


# print(ode([1, 2], 1))          # Just for test the function
# 4. Set up the initial conditions
x0 = [1, 1]                      # Initial values of the functions
t = np.linspace(0, 10, 100)      # Time

# 5. Solving the ODE
sol = sci.integrate.odeint(ode, x0, t)

# 6. Plot the solution
plt.plot(t, sol[:, 0])           # Plot v
plt.plot(t, sol[:, 1])           # Plot g
plt.show()

The general idea is that you must define your ODE system in a function, where the variables of your system must be in the form of a vector defined in the function (see point 3 of the example). After that, it assigns initial conditions (point 4) and finally solves the system sci.integrate.odeint

Greetings,

    
answered by 14.03.2018 / 02:56
source