简体   繁体   中英

Modulating object's trajectory with Euler method in python

I try to modulate object's trajectory using Euler method. It looks like my program works when I input 45 degrees, but with any other value it doesn't work (I'm not sure if it works with 45 degrees, but it at least looks like trajectory). Maybe someone could see what I'm doing wrong, here's my code:

import numpy as np
import matplotlib.pyplot as plt

dt=0.005  # step
t=0
X=[0]
Y=[0]  # X and Y are arrays for storing coordinates
x=0
y=0
v=10  # initial velocity
F=100 # force given to object
m=10  # object's mass
r=1.5  # viscosity
alpha=float(input())
vx=v*np.cos(np.rad2deg(alpha))  # projections to axises
vy=v*np.sin(np.rad2deg(alpha))
Fx=F*np.cos(np.rad2deg(alpha))
Fy=F*np.sin(np.rad2deg(alpha))
while t<20:  # Euler method

    ax=(Fx-r*vx)/m  # F=ma, F=-rv so a=(F-rv)/m
    ay=(Fy-r*vy)/m
    vx=vx+ax*dt
    vy=vy+ay*dt
    x=x+vx*dt
    y=y+vy*dt
    X.append(x)
    Y.append(y)
    t=t+dt
    if y<0:
        break

plt.plot(X,Y)
plt.autoscale(enable=True,axis='both',tight=None)
plt.xlabel(X)
plt.ylabel(Y)
plt.show()

Here's what I get when I input 45 degrees:

截图

It seems to work now, I forgot to add F=mg:

ax=(Fx-r*vx)/m
ay=(Fy-r*vy-m*g)/m  # g=9.8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM