操作数无法使用 scipy.integrate.solve_BVP 与形状 (12,999) (12,1000) 错误一起广播 [英] Operands could not be broadcast together with shapes (12,999) (12,1000) error using scipy.integrate.solve_BVP

查看:45
本文介绍了操作数无法使用 scipy.integrate.solve_BVP 与形状 (12,999) (12,1000) 错误一起广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前问过一个问题,我在这里询问了我在尝试解决 BVP 时通常会出错的地方:使用函数返回数组的 scipy.solve_bvp 求解 BVP

I have previously asked a question where I asked about where I was generally going wrong with my attempts to solve the BVP here: Solving a BVP using scipy.solve_bvp where the function returns an array

我在回答这个问题时收到的一个回复指出,根据文档,我传递给 solve_bvp() 的函数不是形状 (12,1000).我通过创建一个函数来纠正这个问题,该函数生成具有所需形状的解,首先初始化一个 12 x 1000 的数组,然后将 12 个形状为 1000 的数组中的每一个设置为等于其相关微分方程的值,在该时间点我的功能被调用.我写的代码在下面,新代码有明确的注释.

One of the responses I received while answering this stated that the function that i was passing to solve_bvp() was not of shape (12,1000) as per the documentation. I have rectified this by creating a function which generates solutions with the required shape, by first initialising a 12 by 1000 array and then setting each of the 12 arrays of shape 1000 equal to the value of its associated differential equation for each time point at which my functon is called. The code I have written is below, with the new code clearly annotated.

##find our velocity at time t=-1000, where the BVP is started from 
def find_vel_past():
    daynum=1000
    ts=np.linspace(0,-daynum*day2sec,daynum)
    angles=np.zeros([daynum,2])
    trange =(ts[0],ts[-1])
    fi=np.ndarray.flatten(np.array(rs+vs))
    sol= integrate.solve_ivp(earth_mars_motion,trange,fi,t_eval=ts, max_step=3*day2sec,dense_output=True)
    return(sol.y[0:6][:,-1])
##return an array of six velocities at this time 
def estimate_errors_improved():
    daynum=1000
    ##generating np arrays for bouundary conditions
    a=np.ndarray.flatten(np.array(find_vel_past()))
    rpast=np.ndarray.flatten(np.array(rspast))
    acond=np.concatenate([rpast,a])
    bcond=np.ndarray.flatten(np.array(rs+vs))
    t=np.linspace(0,daynum*day2sec,daynum)
    y=np.zeros(([12,daynum]))
    y[:,0]=acond
    ## THE FUNCTION I HAVE UPDATED AS PER ADVICE RECIEVED
    def earth_mars_motion1(t,y):
        x=np.zeros([12,daynum])
        y1=earth_mars_motion(t,y[:,0])
        for i in range(daynum):
            for i in range(len(y)):
                x[i]=y1[i] 
        return x
    def bc(ya,yb):
        a=ya.ravel()[0:6]-bcond[0:6]
        b=yb.ravel()[0:6]-rpast
        c=np.array([a,b])
        return np.reshape(c,(12))
        #return np.concatenate(ya.ravel()[0:6]-bcond[0:6],yb.ravel()[0:6]-rpast)
    sol = integrate.solve_bvp(earth_mars_motion1,bc,t,y,verbose=1)
    data1=np.transpose(sol.sol(t))
    angles=np.zeros(daynum)
    print(sol.sol(t))
    for i in range(daynum):      
        angles[i]=angle_between_planets(np.transpose(sol.sol(t)[:,0]))
        x = t/day2sec
    plt.plot(x,angles)
    plt.show()
estimate_errors_improved()

我得到的输出是:ValueError: 操作数无法与形状一起广播 (12,999) (12,1000).任何人都可以提供一个快速解决这个问题的方法.谢谢.

The output I am getting is: ValueError: operands could not be broadcast together with shapes (12,999) (12,1000). Could anyone please provide a quick solution to this problem. Thanks.

推荐答案

我相信 integrate.solve.bvp(fun,bc,t,x) 通过迭代问题和插入乐趣.每次这样做时,t 的输入都会变短.您返回的 x 也应在每一步缩短.尝试将 x=np.zeros([12,daynum]) 替换为 x=np.zeros([12,len(t)]),并调整以下代码这个要匹配.

I believe that integrate.solve.bvp(fun,bc,t,x) works by iterating over the time steps in the problem and plugging into fun. Each time it does this, the input for t gets shorter. Your x that is returned should also be shortened at each step. Try replacing x=np.zeros([12,daynum]) with x=np.zeros([12,len(t)]), and adjust the code following this to match.

这篇关于操作数无法使用 scipy.integrate.solve_BVP 与形状 (12,999) (12,1000) 错误一起广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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