首页 > 解决方案 > 如何过量 scipy.solve_ivp solution.y_events?

问题描述

我需要模拟一个球向各个方向射击的点球。为此,我使用solve_ivp,并在球越过背线时终止积分。发生这种情况时,我想使用积分停止时找到的值来查看该点的球是否在目标的尺寸范围内(并将其计为目标)。但是,solution.y 没有提供我想要的所需精度。所需的值在 solution.y_events 中,但是,我似乎无法接收它们。我也无法在网上找到有关此的信息。我的代码:

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



#### Constants

# Number of simulations
number_of_penalty_shots = 10

# Angle of the shots
theta = np.random.uniform(0, 2.0*np.pi, number_of_penalty_shots)
phi = np.random.uniform(0, np.pi, number_of_penalty_shots)

# Velocity of the ball
v_magnitude = 80

### Starting Position Ball (defined as the penalty stip)
pos_x = 0.0
pos_y = 0.0
pos_z = 0.0

in_position = np.array([pos_x, pos_y, pos_z]) # Inital position in m


def homo_magnetic_field(t, vector):
    vx = vector[3] # dx/dt = vx
    vy = vector[4] # dy/dt = vy
    vz = vector[5] # dz/dt = vz   
        
    # ax = -0.05*vector[3] # dvx/dt = ax
    # ay = -0.05*vector[4] # dvy/dy = ay
    # az = -0.05*vector[5] - constants.g #dvz/dt = az
    
    ax = 0
    ay = 0
    az = 0
        
    dvectordt = (vx,vy,vz,ax,ay,az)
    return(dvectordt)

def goal(t, vector):
    return vector[1] - 11

def own_goal(t,vector):
    return vector[1] + 100

def ground(t,vector):
    return vector[2]

goal.terminal=True
own_goal.terminal=True

def is_it_goal(vector):
    if vector.status == 1:
        if (vector.y[1][len(vector.y[1])-1] > 0) and (-3.36 < vector.y[0][len(vector.y[1])-1] < 3.36) and (vector.y[2][len(vector.y[1])-1] < 2.44):
            print("GOAAAAAAAAAAAAL!")
        elif (vector.y[1][len(vector.y[1])-1] < 0) and (-3.36 < vector.y[0][len(vector.y[1])-1] < 3.36) and (vector.y[2][len(vector.y[1])-1] < 2.44):
            print("Own goal?! Why?")
        else:  print("Awwwwh")
    else: print("Not even close, lol")


# Integrating

time_range = (0.0, 10**2)

for i in range(number_of_penalty_shots):
    v_x = v_magnitude*np.sin(phi[i])*np.cos(theta[i])
    v_y = v_magnitude*np.sin(phi[i])*np.sin(theta[i])
    v_z = v_magnitude*np.cos(phi[i])
    in_velocity = np.array([v_x, v_y, v_z])
    initial_point = np.array([in_position, in_velocity])
    start_point = initial_point.reshape(6,)
    
    solution = integrate.solve_ivp(homo_magnetic_field , time_range, start_point,events=(goal, own_goal))
    is_it_goal(solution)

在这里,我想将 vector.y[1][len(vector.y[1])-1] 更改为类似 vector.y_events...

标签: pythoneventsscipyodemontecarlo

解决方案


推荐阅读