Moderator: physicalattraction
Code: Selecteer alles
def run(m):
dt=0.001
t=0
x=0
y=0
g=9.81
k=0.0006
kk=m*g*k
v0=50
th=40
thr=np.deg2rad(th)
vx=v0*np.cos(thr)
vy=v0*np.sin(thr)
tr=[0]
xr=[0]
yr=[0]
while y>=0:
v2=vx**2+vy**2
Fw=kk*v2
Fx=-Fw*vx/np.sqrt(v2)
Fy=-Fw*vx/np.sqrt(v2)-g*m
vx+=dt*Fx/m
vy+=dt*Fy/m
x+=dt*vx
y+=dt*vy
t+=dt
xr.append(x)
yr.append(y)
tr.append(t)
return tr, xr,yr
Code: Selecteer alles
from scipy.integrate import odeint
import numpy as np
import matplotlib.pyplot as plt
def vectorfield(w, t):
x, dxdt, y, dydt = w
# Create f = (x1',y1',x2',y2'):
f = [dxdt,
-9.81 * 0.0006 * dxdt * (dxdt ** 2 + dydt ** 2),
dydt,
-9.81 * 0.0006 * dydt * (dxdt ** 2 + dydt ** 2) - 9.81]
return f
# Initial conditions
# x1 and x2 are the initial displacements; y1 and y2 are the initial velocities
x = 0
dxdt = 50*np.cos(np.deg2rad(40))
y = 0
dydt = 50*np.sin(np.deg2rad(40))
# ODE solver parameters
abserr = 1.0e-8
relerr = 1.0e-6
stoptime = 10.0
numpoints = 250
# Create the time samples for the output of the ODE solver.
# I use a large number of points, only because I want to make
# a plot of the solution that looks nice.
t = [stoptime * float(i) / (numpoints - 1) for i in range(numpoints)]
# Pack up the parameters and initial conditions:
w0 = [x, dxdt, y, dydt]
# Call the ODE solver.
wsol = odeint(vectorfield, w0, t,
atol=abserr, rtol=relerr)
print(wsol)
x = [row[0] for row in wsol]
y = [row[2] for row in wsol]
plt.plot(x,y)
plt.show()
Code: Selecteer alles
f = @(t,y) [y(2); -9.81*0.0006*y(2)*(y(2)^2+y(4)^2)^0.5;y(4);-9.81*0.0006*y(4)*(y(2)^2+y(4)^2)^0.5 - 9.81 ];
tspan = [0, 15];
yinit = [0, 50*cosd(40), 0, 50*sind(40)];
[t,y]=ode45(f, tspan, xinit)
plot(y(:,1),y(:,3))
Code: Selecteer alles
from scipy.integrate import odeint
import numpy as np
import matplotlib.pyplot as plt
def vectorfield(w, t):
x, dxdt, y, dydt = w
f = [dxdt,
-9.81 * 0.0006 * dxdt * (dxdt ** 2 + dydt ** 2)**0.5,
dydt,
-9.81 * 0.0006 * dydt * (dxdt ** 2 + dydt ** 2)**0.5 - 9.81]
return f
# Initial conditions
x = 0
dxdt = 50*np.cos(np.deg2rad(40))
y = 0
dydt = 50*np.sin(np.deg2rad(40))
# ODE solver parameters
abserr = 1.0e-8
relerr = 1.0e-6
stoptime = 15.0
numpoints = 250
# Create the time samples for the output of the ODE solver.
# I use a large number of points, only because I want to make
# a plot of the solution that looks nice.
t = [stoptime * float(i) / (numpoints - 1) for i in range(numpoints)]
# Pack up the parameters and initial conditions:
w0 = [x, dxdt, y, dydt]
# Call the ODE solver.
wsol = odeint(vectorfield, w0, t,
atol=abserr, rtol=relerr)
print(wsol)
x = [row[0] for row in wsol]
y = [row[2] for row in wsol]
plt.plot(x,y)
plt.show()