使用Matplotlib对具有不同升力和阻力的球的弹丸运动进行建模

问题描述

我正在尝试使用Python和Matplotlib对球的弹丸运动进行建模,包括不同的升力和阻力值。

这是我到目前为止所写的。

import matplotlib.pyplot as plt
import numpy as np

class Golfball ():
    def __init__(self,x0,y0,v0,angle,m,g,l,d):
    
    # x0 and y0 are the initial positions of the golfball
    # v0 is the initial velocity of the golfball
    # angle is the initial angle of inclination of the golfball
    # m is the mass of the golf ball
    # g is the gravitational acceleration acting on the golfball
    # l is the lift force acting on the golf ball due to the magnus effect
    # d is the drag force acting on the golf ball due to airflow
    
    # constant variables (initial)
        self.angle = angle
        self.mass = m
        self.gravaccel = g
    # lift and drag force
        self.liftforce = l
        self.dragforce = d
    # x and y positions of the golf ball
        self.x = x0
        self.y = y0
    # x and y positions as discrete sets
        self.xset = [self.x]
        self.yset = [self.y]
    # x and y velocity components of the golf ball
        self.vx = v0*np.cos(self.angle/180*np.pi)
        self.vy = v0*np.sin(self.angle/180*np.pi)
    # x and y accelration components of the golf ball
        self.ax = -((self.liftforce*np.sin(self.angle/180*np.pi))+(self.dragforce*np.cos(self.angle/180*np.pi)))/self.mass
        self.ay = ((self.liftforce*np.cos(self.angle/180*np.pi))-(self.dragforce*np.sin(self.angle/180*np.pi))/m)-self.gravaccel
    # time
        self.time = 0
    # update velocity components
    def updateVx(self,dt):
        self.vx = self.vx + self.ax*dt
        return self.vx
    def updateVy(self,dt):
        self.vy = self.vy + self.ax*dt
        return self.vy
    # update positions of golfball
    def updateX(self,dt):
        self.x = self.x +((self.vx*dt)+(0.5*self.ax*dt**2))
        return self.x
    def updateY(self,dt):
        self.y = self.y +((self.vy*dt)+(0.5*self.ay*dt**2))
        return self.y
    # update angle of inclination
    def updateangle(self):
        self.angle = np.arctan(self.vy/self.vx)
        return self.angle
    # update positions sets of golfball at every timestep
    def step(self,dt):
        self.xset.append(self.updateX(dt))
        self.yset.append(self.updateY(dt))
        self.time = self.time + dt

# using Golfball class   
def release (x0,d):
    """
    Returns a tuple with x and y positions of the golf ball
    """
    golfball = Golfball(x0,d)
    dt = 0.01 # time step
    t = 0 # start time
    golfball.step(dt)

    while golfball.y >= 0:
        golfball.step(dt)
        t = t + dt
    # update velocity components
        golfball.updateVx(dt)
        golfball.updateVy(dt)
    # update position
        golfball.updateX(dt)
        golfball.updateY(dt)
    # update angle
        golfball.updateangle()
        
    return (golfball.xset,golfball.yset)

def main():
    x0 = 0
    y0 = 0
    v0 = 60
    angle = 45
    m = 10
    g = 9.81
    x1,y1 = release(x0,12357,23546)
    x2,y2 = release(x0,26573,27534)
    x3,y3 = release(x0,33492,34538)
    plt.plot(x1,y1,x2,y2,x3,y3)
    plt.show()

if __name__ == '__main__':
    main()

我已将此链接用作参考:({https://www.assignmentexpert.com/blog/modeling-projectile-motion-using-python/

所有反馈将不胜感激,如果还有其他方法可以完成此任务,我也想知道。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)