用numpy表示一阶微分方程 [英] Represent a first order differential equation in numpy

查看:568
本文介绍了用numpy表示一阶微分方程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个等式dy/dx = x + y/5和一个初始值y(0) = -3.

I have an equation dy/dx = x + y/5 and an initial value, y(0) = -3.

我想知道如何使用pyplot绘制此函数的精确图形.

I would like to know how to plot the exact graph of this function using pyplot.

我还有一个x = np.linspace(0, interval, steps+1),我想用作x轴.所以我只在寻找y轴值.

I also have a x = np.linspace(0, interval, steps+1) which I would like to use as the x axis. So I'm only looking for the y axis values.

提前谢谢.

推荐答案

仅出于完整性考虑,可以使用scipy.integrate.odeint轻松地对此类方程进行数值积分.

Just for completeness, this kind of equation can easily be integrated numerically, using scipy.integrate.odeint.

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

# function dy/dx = x + y/5.
func = lambda y,x : x + y/5.
# Initial condition
y0 = -3  # at x=0
# values at which to compute the solution (needs to start at x=0)
x = np.linspace(0, 4, 101)
# solution
y = odeint(func, y0, x)
# plot the solution, note that y is a column vector
plt.plot(x, y[:,0])
plt.xlabel('x')
plt.ylabel('y')
plt.show()

这篇关于用numpy表示一阶微分方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆