Plotting planet(comet) movement arund start in matplotlib - python

I am trying to create trajectory plot in matplotlib to show how planet(comet or any body with mass) moving around the star(sun). In my context it is assumed that sun is on position (0, 0). Unfortunately, there is something off in my calculation or code or maybe both, thus I cant get the final result right.
This code below producing this
Which is clearly wrong. I have tried to google and playing around with code, but still cannot achieve the expected plot.
The expected result should be
These are the formulas I was using.
import matplotlib.pyplot as plt
from math import cos, sin, hypot
# constants
G = 6.67
dt = 0.005
# planet
planet_m = 5.97
planet_x = 1
planet_y = 0
# star
star_m = 1.98
star_x = 0
star_y = 0
# velocities
vx = 0
vy = 8
#
dx = 0
dy = 0
F = 0
t = 0
fxl = []
fyl = []
for i in range(1, 50):
t += i * dt
dx += (star_x-planet_x)
dy += (star_y-planet_y)
d = hypot(dx, dy)# the same as sqrt(dx**2 + dy**2)
F += G * planet_x * star_m / (d**2)
fx = cos(dx) * -F
fy = sin(dy) * -F
vx += fx / planet_m * t
vy += fy / planet_m * t
planet_x += vx * dt
planet_y += vy * dt
fyl.append(fx)
fxl.append(fy)
print(f'Position: {planet_x} {planet_y}')
print(f'Velocities: {vx} {vy}')
plt.plot(star_x, star_y, 'yo', fxl, fyl, '-')
plt.grid()
plt.show()

Well, I think there are a lot of strange things going on in your code. I tried to fix it in a minimal way, but there's still a very big room for improvements:
import matplotlib.pyplot as plt
from math import cos, sin, hypot
# constants
G = 6.67
dt = 0.0001
# planet
planet_m = 5.97
planet_x = 1
planet_y = 0
# star
star_m = 1.98
star_x = 0
star_y = 0
# velocities
vx = 0
vy = 1
pos_x = []
pos_y = []
for i in range(1, 100000):
dx = (star_x-planet_x)
dy = (star_y-planet_y)
d = hypot(dx, dy)
F = G * star_m / (d**2)
fx = F * dx / d
fy = F * dy / d
vx += fx / planet_m * dt
vy += fy / planet_m * dt
planet_x += vx * dt
planet_y += vy * dt
pos_x.append(planet_x)
pos_y.append(planet_y)
plt.plot(star_x, star_y, 'yo', pos_x, pos_y, '-')
plt.show()

Related

need help plotting coordinates of position vector array within loop

im trying to plot the x and y coordinate of the r-vector in a loop, but don't get how to call the coordinates to iterate in loop. If i could get some help that would be great.
import numpy as np
import math as m
import matplotlib as plt
g = np.array([0,-9.8,0])
r = np.array([0,1.2,0])
theta = 35 * m.pi / 180
v1 = 3.3
v = v1 * np.array([np.cos(theta),np.sin(theta),0])
a = g
t = 0
dt = .01
while r[1] > 0:
v = v + a * dt
r = r + v * dt
t = t + dt
plt.plot(r)
print("r = ",r , "m")
print("t = ",t, "s")
print("v = ",v, "m/s")

Is it possible to create a graph using MatPlotLib with data from a scientific model?

To preface, I am completely new to Python and MatPlotLib, and I am working on a school project to calculate the trajectory of a satellite around the earth. I was wondering if it was possible to grab the data from every point the model calculates and put it inside the graph, plotting time on the x-axis and height on the y-axis. I tried to directly plot the two values, but it doesn't seem to appear on the graph. Is it possible to put every value from the calculations in the graph, or will I need to have a direct formula for the line to plot it in a graph.
The air resistance also changes for certain heights, so it makes it harder to make a direct formula. Here's the code, I am aware it is terribly formatted but it being my first project all I need from it is to get a t,h-graph. Thanks in advance for the help!
from typing import Any, Union
import matplotlib.pyplot as plt
import numpy as np
data = []
t = 0
dt = 10
A = 10
Cw = 2.7
h = 300000
h0 = h
G = 6.67384 * 10 ** -11
M = 5.972 * 10 ** 24
m = 209.4 * A ** (3 / 2)
r = 6371000 + h
v: Union[float, Any] = (G * M / r) ** .5
vx = v
vy = 0
Px = 0
Py = r
while 0 < h < h0 + 100000:
# for _ in range(2592000):
t = t + dt
r = (Px ** 2 + Py ** 2) ** .5
Fg = G * M * m / r ** 2
Fgx = -Fg * Px / r
Fgy = -Fg * Py / r
h = r - 6371000
if h > 600000:
z = 1.607 * 10 ** -11 * 0.991169 ** (h / 1000)
else:
if h > 139000:
z = 3.848 * 10 ** -8 * 0.978294 ** (h / 1000)
else:
z = 1.225 * 0.863697 ** (h / 1000)
v = (vx ** 2 + vy ** 2) ** .5
Fwl = 0.5 * z * Cw * A * v ** 2
Fwlx = -Fwl * vx / v
Fwly = -Fwl * vy / v
ax = (Fgx + Fwlx) / m
ay = (Fgy + Fwly) / m
vx = vx + ax * dt
vy = vy + ay * dt
Px = Px + vx * dt
Py = Py + vy * dt
data += [[h, t]]
print(h)
print(t)
fig, ax = plt.subplots()
ax.plot(t, h)
plt.show()
As suggested by Michael Szczesny in the comment, you should indent data += [[h, t]] within the while loop.
Then I suggest you to plot with:
ax.plot(*zip(*data))
in order to separate h for t and get h(t) curve.
An other suggestion is to flip the order in which you store h and t in data:
data += [[h, t]]
in this way you will get a plot of the height with respect to time (and not the inverse).
Complete Code
from typing import Any, Union
import matplotlib.pyplot as plt
data = []
t = 0
dt = 10
A = 10
Cw = 2.7
h = 300000
h0 = h
G = 6.67384 * 10 ** -11
M = 5.972 * 10 ** 24
m = 209.4 * A ** (3 / 2)
r = 6371000 + h
v: Union[float, Any] = (G * M / r) ** .5
vx = v
vy = 0
Px = 0
Py = r
while 0 < h < h0 + 100000:
# for _ in range(2592000):
t = t + dt
r = (Px ** 2 + Py ** 2) ** .5
Fg = G * M * m / r ** 2
Fgx = -Fg * Px / r
Fgy = -Fg * Py / r
h = r - 6371000
if h > 600000:
z = 1.607 * 10 ** -11 * 0.991169 ** (h / 1000)
else:
if h > 139000:
z = 3.848 * 10 ** -8 * 0.978294 ** (h / 1000)
else:
z = 1.225 * 0.863697 ** (h / 1000)
v = (vx ** 2 + vy ** 2) ** .5
Fwl = 0.5 * z * Cw * A * v ** 2
Fwlx = -Fwl * vx / v
Fwly = -Fwl * vy / v
ax = (Fgx + Fwlx) / m
ay = (Fgy + Fwly) / m
vx = vx + ax * dt
vy = vy + ay * dt
Px = Px + vx * dt
Py = Py + vy * dt
data += [[h, t]]
fig, ax = plt.subplots()
ax.plot(*zip(*data))
plt.show()

Matplotlib plot's title is missing for unknown reason in Python

Can anyone tell me what is wrong with this code? It is from https://jakevdp.github.io/blog/2012/09/05/quantum-python/ .
Everything in it worked out except the title of the plot.I can't figure it out.
It should look like this
but when the code is run, it polts this
Here is the code given:-
"""
General Numerical Solver for the 1D Time-Dependent Schrodinger's equation.
author: Jake Vanderplas
email: vanderplas#astro.washington.edu
website: http://jakevdp.github.com
license: BSD
Please feel free to use and modify this, but keep the above information. Thanks!
"""
import numpy as np
from matplotlib import pyplot as pl
from matplotlib import animation
from scipy.fftpack import fft,ifft
class Schrodinger(object):
"""
Class which implements a numerical solution of the time-dependent
Schrodinger equation for an arbitrary potential
"""
def __init__(self, x, psi_x0, V_x,
k0 = None, hbar=1, m=1, t0=0.0):
"""
Parameters
----------
x : array_like, float
length-N array of evenly spaced spatial coordinates
psi_x0 : array_like, complex
length-N array of the initial wave function at time t0
V_x : array_like, float
length-N array giving the potential at each x
k0 : float
the minimum value of k. Note that, because of the workings of the
fast fourier transform, the momentum wave-number will be defined
in the range
k0 < k < 2*pi / dx
where dx = x[1]-x[0]. If you expect nonzero momentum outside this
range, you must modify the inputs accordingly. If not specified,
k0 will be calculated such that the range is [-k0,k0]
hbar : float
value of planck's constant (default = 1)
m : float
particle mass (default = 1)
t0 : float
initial tile (default = 0)
"""
# Validation of array inputs
self.x, psi_x0, self.V_x = map(np.asarray, (x, psi_x0, V_x))
N = self.x.size
assert self.x.shape == (N,)
assert psi_x0.shape == (N,)
assert self.V_x.shape == (N,)
# Set internal parameters
self.hbar = hbar
self.m = m
self.t = t0
self.dt_ = None
self.N = len(x)
self.dx = self.x[1] - self.x[0]
self.dk = 2 * np.pi / (self.N * self.dx)
# set momentum scale
if k0 == None:
self.k0 = -0.5 * self.N * self.dk
else:
self.k0 = k0
self.k = self.k0 + self.dk * np.arange(self.N)
self.psi_x = psi_x0
self.compute_k_from_x()
# variables which hold steps in evolution of the
self.x_evolve_half = None
self.x_evolve = None
self.k_evolve = None
# attributes used for dynamic plotting
self.psi_x_line = None
self.psi_k_line = None
self.V_x_line = None
def _set_psi_x(self, psi_x):
self.psi_mod_x = (psi_x * np.exp(-1j * self.k[0] * self.x)
* self.dx / np.sqrt(2 * np.pi))
def _get_psi_x(self):
return (self.psi_mod_x * np.exp(1j * self.k[0] * self.x)
* np.sqrt(2 * np.pi) / self.dx)
def _set_psi_k(self, psi_k):
self.psi_mod_k = psi_k * np.exp(1j * self.x[0]
* self.dk * np.arange(self.N))
def _get_psi_k(self):
return self.psi_mod_k * np.exp(-1j * self.x[0] *
self.dk * np.arange(self.N))
def _get_dt(self):
return self.dt_
def _set_dt(self, dt):
if dt != self.dt_:
self.dt_ = dt
self.x_evolve_half = np.exp(-0.5 * 1j * self.V_x
/ self.hbar * dt )
self.x_evolve = self.x_evolve_half * self.x_evolve_half
self.k_evolve = np.exp(-0.5 * 1j * self.hbar /
self.m * (self.k * self.k) * dt)
psi_x = property(_get_psi_x, _set_psi_x)
psi_k = property(_get_psi_k, _set_psi_k)
dt = property(_get_dt, _set_dt)
def compute_k_from_x(self):
self.psi_mod_k = fft(self.psi_mod_x)
def compute_x_from_k(self):
self.psi_mod_x = ifft(self.psi_mod_k)
def time_step(self, dt, Nsteps = 1):
"""
Perform a series of time-steps via the time-dependent
Schrodinger Equation.
Parameters
----------
dt : float
the small time interval over which to integrate
Nsteps : float, optional
the number of intervals to compute. The total change
in time at the end of this method will be dt * Nsteps.
default is N = 1
"""
self.dt = dt
if Nsteps > 0:
self.psi_mod_x *= self.x_evolve_half
for i in xrange(Nsteps - 1):
self.compute_k_from_x()
self.psi_mod_k *= self.k_evolve
self.compute_x_from_k()
self.psi_mod_x *= self.x_evolve
self.compute_k_from_x()
self.psi_mod_k *= self.k_evolve
self.compute_x_from_k()
self.psi_mod_x *= self.x_evolve_half
self.compute_k_from_x()
self.t += dt * Nsteps
######################################################################
# Helper functions for gaussian wave-packets
def gauss_x(x, a, x0, k0):
"""
a gaussian wave packet of width a, centered at x0, with momentum k0
"""
return ((a * np.sqrt(np.pi)) ** (-0.5)
* np.exp(-0.5 * ((x - x0) * 1. / a) ** 2 + 1j * x * k0))
def gauss_k(k,a,x0,k0):
"""
analytical fourier transform of gauss_x(x), above
"""
return ((a / np.sqrt(np.pi))**0.5
* np.exp(-0.5 * (a * (k - k0)) ** 2 - 1j * (k - k0) * x0))
######################################################################
# Utility functions for running the animation
def theta(x):
"""
theta function :
returns 0 if x<=0, and 1 if x>0
"""
x = np.asarray(x)
y = np.zeros(x.shape)
y[x > 0] = 1.0
return y
def square_barrier(x, width, height):
return height * (theta(x) - theta(x - width))
######################################################################
# Create the animation
# specify time steps and duration
dt = 0.01
N_steps = 50
t_max = 120
frames = int(t_max / float(N_steps * dt))
# specify constants
hbar = 1.0 # planck's constant
m = 1.9 # particle mass
# specify range in x coordinate
N = 2 ** 11
dx = 0.1
x = dx * (np.arange(N) - 0.5 * N)
# specify potential
V0 = 1.5
L = hbar / np.sqrt(2 * m * V0)
a = 3 * L
x0 = -60 * L
V_x = square_barrier(x, a, V0)
V_x[x < -98] = 1E6
V_x[x > 98] = 1E6
# specify initial momentum and quantities derived from it
p0 = np.sqrt(2 * m * 0.2 * V0)
dp2 = p0 * p0 * 1./80
d = hbar / np.sqrt(2 * dp2)
k0 = p0 / hbar
v0 = p0 / m
psi_x0 = gauss_x(x, d, x0, k0)
# define the Schrodinger object which performs the calculations
S = Schrodinger(x=x,
psi_x0=psi_x0,
V_x=V_x,
hbar=hbar,
m=m,
k0=-28)
######################################################################
# Set up plot
fig = pl.figure()
# plotting limits
xlim = (-100, 100)
klim = (-5, 5)
# top axes show the x-space data
ymin = 0
ymax = V0
ax1 = fig.add_subplot(211, xlim=xlim,
ylim=(ymin - 0.2 * (ymax - ymin),
ymax + 0.2 * (ymax - ymin)))
psi_x_line, = ax1.plot([], [], c='r', label=r'$|\psi(x)|$')
V_x_line, = ax1.plot([], [], c='k', label=r'$V(x)$')
center_line = ax1.axvline(0, c='k', ls=':',
label = r"$x_0 + v_0t$")
title = ax1.set_title("")
ax1.legend(prop=dict(size=12))
ax1.set_xlabel('$x$')
ax1.set_ylabel(r'$|\psi(x)|$')
# bottom axes show the k-space data
ymin = abs(S.psi_k).min()
ymax = abs(S.psi_k).max()
ax2 = fig.add_subplot(212, xlim=klim,
ylim=(ymin - 0.2 * (ymax - ymin),
ymax + 0.2 * (ymax - ymin)))
psi_k_line, = ax2.plot([], [], c='r', label=r'$|\psi(k)|$')
p0_line1 = ax2.axvline(-p0 / hbar, c='k', ls=':', label=r'$\pm p_0$')
p0_line2 = ax2.axvline(p0 / hbar, c='k', ls=':')
mV_line = ax2.axvline(np.sqrt(2 * V0) / hbar, c='k', ls='--',
label=r'$\sqrt{2mV_0}$')
ax2.legend(prop=dict(size=12))
ax2.set_xlabel('$k$')
ax2.set_ylabel(r'$|\psi(k)|$')
V_x_line.set_data(S.x, S.V_x)
######################################################################
# Animate plot
def init():
psi_x_line.set_data([], [])
V_x_line.set_data([], [])
center_line.set_data([], [])
psi_k_line.set_data([], [])
title.set_text("")
return (psi_x_line, V_x_line, center_line, psi_k_line, title)
def animate(i):
S.time_step(dt, N_steps)
psi_x_line.set_data(S.x, 4 * abs(S.psi_x))
V_x_line.set_data(S.x, S.V_x)
center_line.set_data(2 * [x0 + S.t * p0 / m], [0, 1])
psi_k_line.set_data(S.k, abs(S.psi_k))
title.set_text("t = %.2f" % S.t)
return (psi_x_line, V_x_line, center_line, psi_k_line, title)
# call the animator. blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=frames, interval=30, blit=True)
# uncomment the following line to save the video in mp4 format. This
# requires either mencoder or ffmpeg to be installed on your system
#anim.save('schrodinger_barrier.mp4', fps=15, extra_args=['-vcodec', 'libx264'])
pl.show()
For your ease I mention the lines of this code which I do suspect for the error are line number 238,247,255,286,287,296,297
Thanks in advance
The problem is resolved when blit=False, though it may slow down your animation.
Just quoting from a previous answer:
"Possible solutions are:
Put the title inside the axes.
Don't use blitting"
See: How to update plot title with matplotlib using animation?
You also need ffmpeg installed. There are other answers on stackoverflow that help you through that installation. But for this script, here are my recommended new lines you need to add, assuming you're using Windows:
pl.rcParams['animation.ffmpeg_path'] = r"PUT_YOUR_FULL_PATH_TO_FFMPEG_HERE\ffmpeg.exe"
Writer = animation.writers['ffmpeg']
Then adjust the anim.save() line to:
anim.save('schrodinger_barrier.mp4', writer=Writer(fps=15, codec='libx264'))

Fixing projectile motion formulas in python

The code outputs a graph that is nowhere near what you'd expect of a projectile motion type of graph. In addition, if you change the step number to around 2, the graph doesn't output much of anything.
import numpy as np
import matplotlib.pyplot as plt
grav = 9.8
airDen = 1.225 # kg/m^3
areaProj = 0.8643 # m^2
v0 = 198.1 # m/s
angle = 60.0
dragCoef = 0.62 #Approximate value
mass = 15.0 #kg
step = 0.02 #time step in seconds
t = [0]
vx = [v0*np.cos(angle*np.pi/180)]
vy = [v0*np.sin(angle*np.pi/180)]
x = [0]
y = [0]
dragForce = 0.5*dragCoef*areaProj*(v0**2)*airDen
ax = [-(dragForce*np.cos(angle/180*np.pi))/mass]
ay = [-grav-(dragForce*np.sin(angle/180*np.pi)/mass)]
counter = 0
while(y[counter] >= 0):
t.append(t[counter]+step)
vx.append(vx[counter]+step*ax[counter])
vy.append(vy[counter]+step*ay[counter])
x.append(x[counter]+step*vx[counter])
y.append(y[counter]+step*vy[counter])
vel = np.sqrt(vx[counter+1]**2 + vy[counter+1]**2)
dragForce = 0.5*dragCoef*areaProj*(vel**2)*airDen
ax.append(-(dragForce*np.cos(angle/180*np.pi))/mass)
ay.append(-grav-(dragForce*np.sin(angle/180*np.pi)/mass))
counter=counter+1
plt.plot(x,y)
plt.ylabel("y (m)")
plt.xlabel("x (m)")
Graph-example
Looks like you're not updating your angle, so instead of reaching zero vx it accelerates backwards since it thinks you're still going at 60 degrees initial angle. Adding an updated angle based on the current velocity vector results in the following:
import numpy as np
import matplotlib.pyplot as plt
grav = 9.8
airDen = 1.225 # kg/m^3
areaProj = 0.8643 # m^2
v0 = 198.1 # m/s
angle = 60.0
dragCoef = 0.62 #Approximate value
mass = 15.0 #kg
step = 0.02 #time step in seconds
t = [0]
vx = [v0*np.cos(angle*np.pi/180)]
vy = [v0*np.sin(angle*np.pi/180)]
x = [0]
y = [0]
dragForce = 0.5*dragCoef*areaProj*(v0**2)*airDen
ax = [-(dragForce*np.cos(angle/180*np.pi))/mass]
ay = [-grav-(dragForce*np.sin(angle/180*np.pi)/mass)]
counter = 0
while(y[counter] >= 0):
t.append(t[counter]+step)
vx.append(vx[counter]+step*ax[counter])
vy.append(vy[counter]+step*ay[counter])
x.append(x[counter]+step*vx[counter])
y.append(y[counter]+step*vy[counter])
vel = np.sqrt(vx[counter+1]**2 + vy[counter+1]**2)
dragForce = 0.5*dragCoef*areaProj*(vel**2)*airDen
angle = np.arctan(vy[counter]/vx[counter]) * (180 / np.pi)
ax.append(-(dragForce*np.cos(angle/180*np.pi))/mass)
ay.append(-grav-(dragForce*np.sin(angle/180*np.pi)/mass))
counter=counter+1
plt.plot(x,y)
plt.ylabel("y (m)")
plt.xlabel("x (m)")

How to plot the motion of a projectile under the effect of gravity, buoyancy and air resistance?

I am trying to make a plot of a projectile motion of a mass which is under the effect of gravitational, buoyancy and drag force. Basically, I want to show effects of the buoyancy and drag force on flight distance, flight time and velocity change on plotting.
import matplotlib.pyplot as plt
import numpy as np
V_initial = 30 # m/s
theta = np.pi/6 # 30
g = 3.711
m =1
C = 0.47
r = 0.5
S = np.pi*pow(r, 2)
ro_mars = 0.0175
t_flight = 2*(V_initial*np.sin(theta)/g)
t = np.linspace(0, t_flight, 200)
# Drag force
Ft = 0.5*C*S*ro_mars*pow(V_initial, 2)
# Buoyant Force
Fb = ro_mars*g*(4/3*np.pi*pow(r, 3))
x_loc = []
y_loc = []
for time in t:
x = V_initial*time*np.cos(theta)
y = V_initial*time*np.sin(theta) - (1/2)*g*pow(time, 2)
x_loc.append(x)
y_loc.append(y)
x_vel = []
y_vel = []
for time in t:
vx = V_initial*np.cos(theta)
vy = V_initial*np.sin(theta) - g*time
x_vel.append(vx)
y_vel.append(vy)
v_ch = [pow(i**2+ii**2, 0.5) for i in x_vel for ii in y_vel]
tau = []
for velocity in v_ch:
Ft = 0.5*C*S*ro_mars*pow(velocity, 2)
tau.append(Ft)
buoy = []
for velocity in v_ch:
Fb = ro_mars*g*(4/3*np.pi*pow(r, 3))
buoy.append(Fb)
after this point, I couldn't figure out how to plot to a projectile motion under this forces. In other words, I'm trying to compare the projectile motion of the mass under three circumstances
Mass only under the effect of gravity
Mass under the effect of gravity and air resistance
Mass under the effect of gravity, air resistance, and buoyancy
You must calculate each location based on the sum of forces at the given time. For this it is better to start from calculating the net force at any time and using this to calculate the acceleration, velocity and then position. For the following calculations, it is assumed that buoyancy and gravity are constant (which is not true in reality but the effect of their variability is negligible in this case), it is also assumed that the initial position is (0,0) though this can be trivially changed to any initial position.
F_x = tau_x
F_y = tau_y + bouyancy + gravity
Where tau_x and tau_y are the drag forces in the x and y directions, respectively. The velocities, v_x and v_y, are then given by
v_x = v_x + (F_x / (2 * m)) * dt
v_y = v_y + (F_y / (2 * m)) * dt
So the x and y positions, r_x and r_y, at any time t are given by the summation of
r_x = r_x + v_x * dt
r_y = r_y + v_y * dt
In both cases this must be evaluated from 0 to t for some dt where dt * n = t if n is the number of steps in summation.
r_x = r_x + V_i * np.cos(theta) * dt + (F_x / (2 * m)) * dt**2
r_y = r_y + V_i * np.sin(theta) * dt + (F_y / (2 * m)) * dt**2
The entire calculation can actually be done in two lines,
r_x = r_x + V_i * np.cos(theta) * dt + (tau_x / (2 * m)) * dt**2
r_y = r_y + V_i * np.sin(theta) * dt + ((tau_y + bouyancy + gravity) / (2 * m)) * dt**2
Except that v_x and v_y require updating at every time step. To loop over this and calculate the x and y positions across a range of times you can simply follow the below (edited) example.
The following code includes corrections to prevent negative y positions, since the given value of g is for the surface or Mars I assume this is appropriate - when you hit zero y and try to keep going you may end up with a rapid unscheduled disassembly, as we physicists call it.
Edit
In response to the edited question, the following example has been modified to plot all three cases requested - gravity, gravity plus drag, and gravity plus drag and buoyancy. Plot setup code has also been added
Complete example (edited)
import numpy as np
import matplotlib.pyplot as plt
def projectile(V_initial, theta, bouyancy=True, drag=True):
g = 9.81
m = 1
C = 0.47
r = 0.5
S = np.pi*pow(r, 2)
ro_mars = 0.0175
time = np.linspace(0, 100, 10000)
tof = 0.0
dt = time[1] - time[0]
bouy = ro_mars*g*(4/3*np.pi*pow(r, 3))
gravity = -g * m
V_ix = V_initial * np.cos(theta)
V_iy = V_initial * np.sin(theta)
v_x = V_ix
v_y = V_iy
r_x = 0.0
r_y = 0.0
r_xs = list()
r_ys = list()
r_xs.append(r_x)
r_ys.append(r_y)
# This gets a bit 'hand-wavy' but as dt -> 0 it approaches the analytical solution.
# Just make sure you use sufficiently small dt (dt is change in time between steps)
for t in time:
F_x = 0.0
F_y = 0.0
if (bouyancy == True):
F_y = F_y + bouy
if (drag == True):
F_y = F_y - 0.5*C*S*ro_mars*pow(v_y, 2)
F_x = F_x - 0.5*C*S*ro_mars*pow(v_x, 2) * np.sign(v_y)
F_y = F_y + gravity
r_x = r_x + v_x * dt + (F_x / (2 * m)) * dt**2
r_y = r_y + v_y * dt + (F_y / (2 * m)) * dt**2
v_x = v_x + (F_x / m) * dt
v_y = v_y + (F_y / m) * dt
if (r_y >= 0.0):
r_xs.append(r_x)
r_ys.append(r_y)
else:
tof = t
r_xs.append(r_x)
r_ys.append(r_y)
break
return r_xs, r_ys, tof
v = 30
theta = np.pi/4
fig = plt.figure(figsize=(8,4), dpi=300)
r_xs, r_ys, tof = projectile(v, theta, True, True)
plt.plot(r_xs, r_ys, 'g:', label="Gravity, Buoyancy, and Drag")
r_xs, r_ys, tof = projectile(v, theta, False, True)
plt.plot(r_xs, r_ys, 'b:', label="Gravity and Drag")
r_xs, r_ys, tof = projectile(v, theta, False, False)
plt.plot(r_xs, r_ys, 'k:', label="Gravity")
plt.title("Trajectory", fontsize=14)
plt.xlabel("Displacement in x-direction (m)")
plt.ylabel("Displacement in y-direction (m)")
plt.ylim(bottom=0.0)
plt.legend()
plt.show()
Note that this preserves and returns the time-of-flight in the variable tof.
Using vector notation, and odeint.
import numpy as np
from scipy.integrate import odeint
import scipy.constants as SPC
import matplotlib.pyplot as plt
V_initial = 30 # m/s
theta = np.pi/6 # 30
g = 3.711
m = 1 # I assume this is your mass
C = 0.47
r = 0.5
ro_mars = 0.0175
t_flight = 2*(V_initial*np.sin(theta)/g)
t = np.linspace(0, t_flight, 200)
pos0 = [0, 0]
v0 = [np.cos(theta) * V_initial, np.sin(theta) * V_initial]
def f(vector, t, C, r, ro_mars, apply_bouyancy=True, apply_resistance=True):
x, y, x_prime, y_prime = vector
# volume and surface
V = np.pi * 4/3 * r**3
S = np.pi*pow(r, 2)
# net weight bouyancy
if apply_bouyancy:
Fb = (ro_mars * V - m) * g *np.array([0,1])
else:
Fb = -m * g * np.array([0,1])
# velocity vector
v = np.array([x_prime, y_prime])
# drag force - corrected to be updated based on current velocity
# Ft = -0.5*C*S*ro_mars*pow(V_initial, 2)
if apply_resistance:
Ft = -0.5*C*S*ro_mars* v *np.linalg.norm(v)
else:
Ft = np.array([0, 0])
# resulting acceleration
x_prime2, y_prime2 = (Fb + Ft) / m
return x_prime, y_prime, x_prime2, y_prime2
sol = odeint(f, pos0 + v0 , t, args=(C, r, ro_mars))
plt.plot(sol[:,0], sol[:, 1], 'g', label='tray')
plt.legend(loc='best')
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
plt.show()
Note that I corrected your drag force to use the actual (not initial) velocity, I do not know if that was your mistake or it was on purpose.
Also please check the documentation for odeint to understand better how to turn a second order ODE (like the one in your problem) to a first order vector ODE.
To remove air resistance or bouyancy, set apply_bouyancy and apply_resistance to True or False by adding them to the args=(...)

Categories