Having trouble using scipy.integrate.odeint with python

I tried to use odeint to solve the problem. My code is as follows:

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

eta=1.24e-9/2
def fun(x):
    f=1.05e-8*eta*x**(1.5)*np.exp(13.6/x)
    return (np.sqrt(1.+4*f)-1)/2./f
x=np.arange(0,1,0.001)
y=odeint(fun,x,0)[0]
plt.plot(x,y)
plt.plot(x,x)
plt.show()

These two curves are the same, which is obviously wrong. If I build a function, it will look like a step function, which is very small to 0.3 and exponentially moves to 1. Can you help me figure out what's wrong with it? Thanks!

+2
source share
1 answer

There are several problems with your code, most of which you can solve yourself if you read the docstring forodeint more details carefully.

, - odeint. , (, , ) , . dy/dt = a * y, y (0) = 100. , , fun, .

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


def fun(y, t, a):
    """Define the right-hand side of equation dy/dt = a*y""" 
    f = a * y
    return f


# Initial condition
y0 = 100.0

# Times at which the solution is to be computed.
t = np.linspace(0, 1, 51)

# Parameter value to use in `fun`.
a = -2.5

# Solve the equation.
y = odeint(fun, y0, t, args=(a,))

# Plot the solution.  `odeint` is generally used to solve a system
# of equations, so it returns an array with shape (len(t), len(y0)).
# In this case, len(y0) is 1, so y[:,0] gives us the solution.
plt.plot(t, y[:,0])
plt.xlabel('t')
plt.ylabel('y')
plt.show()

:

plot generated by the example

odeint SciPy Cookbook ( " " ).

+9

Source: https://habr.com/ru/post/1621678/


All Articles