使用scipy.integrate.solve\ivp求解数组&初始条件比数组大小少

2024-04-28 11:06:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我想积分dv/dz=f(z,v)=drho(z)。drho是一个1D数组

以下代码不起作用,因为drho.shape=(50,)和v0.shape=(1,)**。但我在底部和顶部只有一个初始条件,我知道v[0]=v[-1]=0

**我收到以下错误消息: ValueError: could not broadcast input array from shape (50) into shape (1)

import numpy as np
from scipy.integrate import solve_ivp

depth = np.linspace(-5500,0,50) # Depth of the ocean in meters.
drho  = np.linspace(0,-3,50)    # Density gradient. Filled with arbitrary values for now.

def eqn(z,v): return rho

tspan = (-5500,0)
v0    = [0]                       # At -5500 meters, v=0

I = solve_ivp(eqn,tspan,v0,method='RK45',t_eval=depth)

Tags: fromimportnpshapedepthsolvedzdv