python求积分的几种原理,在Python中使用odeint实现积分数学方程

I am trying to solve the following equation in python using the scipy.odeint function.

61d372f36b7a65c8efbe5b6cd3022726.png

Currently I am able to implement this form of the equation

e1c4bf264cf46bfe541fb2871ee81356.png

in python using the following script:

def dY(y1, x):

a = 0.001

yin = 1

C = 0.01

N = 1

dC = C/N

b1 = 0

return (a/dC)*(yin-y1)+b1*dC

x = np.linspace(0,20,1000)

y0 = 0

res = odeint(dY, y0, x)

plt.plot(t,res, '-')

plt.show()

My problem with the first equation is 'i'. I don't know how to integrate the equation and still be able to provide the current and previous 'y'(yi-1 and yi) values. 'i' is simply a sequence number that is within a range of 0..100.

Edit 1:

The original equation is:

e09a02736a14ea81a0040d6c0d0c62ad.png

Which I rewrote using y,x,a,b and C

Edit2:

I edited Pierre de Buyl' code and changed the N value. Luckily I have a validation table to validate the outcome against. Unfortunately, the results are not equal.

Here is my validation table:

da939486c5a156fa1c036d1aecb70c9f.png

and here is the numpy output:

1f8b5ae8a226a560eaaedc7a475085c6.png

Used code:

def dY(y, x):

a = 0.001

yin = 1

C = 0.01

N = 3

dC = C/N

b1 = 0.01

y_diff = -np.copy(y)

y_diff[0] += yin

y_diff[1:] += y[:-1]

return (a/dC)*(y_diff)+b1*dC

x = np.linspace(0,20,11)

y0 = np.zeros(3)

res = odeint(dY, y0, x)

plt.plot(x,res, '-')

as you can see the values are different by an offset of 0.02..

Am I missing something that results in this offset?

解决方案

The equation is a "coupled" ordinary differential equation (see "System of ODEs" on Wikipedia.

The variable is a vector containing y[0], y[1], etc. To solve the ODE you must feed a vector as the initial condition and the function dY must return a vector as well.

I have modified your code to achieve this result:

def dY(y, x):

a = 0.001

yin = 1

C = 0.01

N = 1

dC = C/N

b1 = 0

y_diff = -np.copy(y)

y_diff[0] += yin

y_diff[1:] += y[:-1]

return (a/dC)*y_diff+b1*dC

I have written the part y[i-1] - y[i] as a NumPy vector operation and special cased the coordinate y[0] (that is the y1 in your notation but arrays start at 0 in Python).

The solution, using an initial value of 0 for all yi is

x = np.linspace(0,20,1000)

y0 = np.zeros(4)

res = odeint(dY, y0, x)

plt.plot(x,res, '-')

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值