trajectory of bullet, when there is a drag force - graph

i tried to express the trajectory of bullet when there is a drag force.
however, i am not able to express the graph precisely.
how to depict trajectory from ode equation?.
this is my graph. this graph does not plausible. although i struggled setting different sign of vydot value, this is not working correctly.
from pylab import*
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import numpy as np
g=10
m=1
k=0.01
y=zeros([2])
vy0=0
vydot=200
vx0=0
vxdot=200
y[0]=vy0
y[1]=vydot
x=zeros([2])
x[0]=vx0
x[1]=vxdot
t=linspace(0,1000,5000)
def fy(y,t):
g0=y[1]
g1=-k*y[1]
return array([g0,g1])
def fx(z,t):
g0=-x[1]
g1=-k*(x[1])-g
return array([g0,g1])
ans1=odeint(fy,y,t)
ans2=odeint(fx,x,t)
ydata=(ans1[:,])
xdata=(ans2[:,])
plt.plot(ydata,xdata)
show()"""

In air, as opposed to liquids, the bullet not only displaces the volume along its path, but also increases the impulse of the displaced air molecules proportional to the velocity. Thus the drag force is
vn=sqrt(vx²+vy²)
dragx = -k*vn*vx
dragy = -k*vn*vy
Thus use
def f(z,t):
x,y,vx,vy = z
vn = sqrt(vx*vx+vy*vy)
return array([vx, vy, -k*vn*vx, -k*vn*vy-g ])
For a first overview, consider the problem without drag. Then the solution is
x(t) = vx*t = 200m/s*t
y(t) = vy*t-g/2*t² = 200m/s*t - 5m/s²*t²
y(t)=0 is again met for t=2*vy/g at the x coordinate 2*vx*vy/g = 8000m. Maximum height is reached for t=vy/g at height vy²/(2g)=2000m.

Related

How to get the length of lines representing edges in the plot of graph after layout out using networkx

For a graph in networkx, I have made a layout to draw a network graph using code below:
data = pd.read_csv('data\\email-dept3.csv')
edges = [edge for edge in zip(data['source'],data['target'])]
print(len(edges))
G = nx.Graph()
G.add_edges_from(edges)
node_pos = nx.kamada_kawai_layout(G)
#I want to get the edge length as one attributes, but I don't know how to code this function
edge_length = calculate_edge_length()
nx.draw_networkx_nodes(G,node_pos,**options)#draw nodes
[nx.draw_networkx_edges(G,node_pos,edgelist=[key],alpha=np.amin([1,value*100]),width=2) for key,value in cent.items()]
plt.show()
And the result is:
What I want to do is get the every edge's length in this graph. Because after layout, every node has a position in screen, and the edge has its length according to its two nodes' position. But in networkx's API, I can't find the method to get the edge's length. And I also don't know how to calculate this value.
If you need more information, please contact me.
I am trying all kinds of methods to adjust the transparency of edges. The length of line is one of my consideration.
Interesting idea! Seems like a worthwhile experiment; I'll let you decide if it works well or not. :-)
But in networkx's API, I can't find the method to get the edge's length
I think you have to compute them yourself. Fortunately, that's not too hard. Here's an example.
import numpy as np
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (10,10)
def example_graph():
"""
Return the classic Karate Club network, but give text labels to the nodes.
"""
labels = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJZKLMNOPQRSTUVWXYZ'
kg = nx.karate_club_graph()
edges = [(labels[i], labels[j]) for i,j in kg.edges()]
G = nx.Graph()
G.add_edges_from(edges)
return G
# Test network
G = example_graph()
# Determine layout node positions
node_pos = nx.kamada_kawai_layout(G)
# Determine edge distances (from the node positions)
node_pos_df = pd.DataFrame(node_pos.values(), columns=['x', 'y'], index=node_pos.keys())
node_pos_df = node_pos_df.rename_axis('label').sort_index()
edges = np.array(G.edges())
u_pos = node_pos_df.loc[edges[:, 0]].values
v_pos = node_pos_df.loc[edges[:, 1]].values
distances = np.linalg.norm(u_pos - v_pos, axis=1)
## Optional: Add the distances as edge attributes
#edge_distances = {(u,v): d for (u,v), d in zip(G.edges(), distances)}
#nx.set_edge_attributes(G, edge_distances, "layout_distance")
# Compute alpha: Set 0.15 as minimum alpha, 1.0 as maximum alpha
d_min, d_max = distances.min(), distances.max()
alphas = 1.0 - 0.85 * (distances - d_min) / (d_max - d_min)
# Draw graph
nx.draw_networkx_nodes(G, node_pos)
nx.draw_networkx_edges(G, node_pos, edgelist=G.edges(), alpha=alphas, width=2)
plt.show()

Is it possible to scale QGraphicsItem geometry but not pen width? [duplicate]

I'm trying to plot on a QGraphicScene data which can, depending on situation, vary by orders of magnitude. Since the pen I'm using is cosmetic I'd expect the view to be independent of the magnitude of the data. But what I get instead is this:
Sine wave with noise multiplied by 50000:
Sine wave with noise multiplied by 50:
However, if I zoom in to either of these plots (same amount of zooming in both), I eventually reach a level when both images look the same:
What is going on here? Why is the width of the pen changing just because the data values are bigger. And why does the scaling disappear when zoomed in?
The code to reproduce this follows. Left clicking on the plot zooms in, right clicking zooms out.
import sys
from PyQt4 import QtGui as QG
from PyQt4 import QtCore as QC
import numpy as n
class ZoomView(QG.QGraphicsView):
"""Zoomable QGraphicsView"""
def mouseReleaseEvent(self,event):
if event.button() == QC.Qt.LeftButton:
self.scale(1.5,1)
elif event.button() == QC.Qt.RightButton:
self.scale(1/1.5,1)
class MainUI(QG.QDialog):
def __init__(self, parent=None):
super(MainUI, self).__init__(parent)
layout = QG.QVBoxLayout()
self.setLayout(layout)
button_layout = QG.QHBoxLayout()
pb3 = QG.QPushButton('add plot')
button_layout.addWidget(pb3)
layout.addLayout(button_layout)
pb3.clicked.connect(self.scene_maker_singleshot)
scene = QG.QGraphicsScene()
view = ZoomView(self)
view.setTransformationAnchor(QG.QGraphicsView.AnchorUnderMouse)
view.setRenderHint(QG.QPainter.Antialiasing)
layout.addWidget(view)
view.setScene(scene)
self.view = view
self.scene = scene
def scene_maker_singleshot(self):
"""Draw scene and fit in view"""
t1 = 50
t2 = 100
QC.QTimer.singleShot(t1, self.make_scene)
QC.QTimer.singleShot(t2, lambda: self.view.fitInView(self.view.sceneRect()))
def make_scene(self):
scale = 50
#scale = 50000
noise_amp = 0.2*scale
points = 1000
xdata = n.arange(points)
#generate sine data and random noise
ydata = n.sin(xdata/(points/10.))*scale +\
n.random.randint(noise_amp, size=points)
pen = QG.QPen(QG.QColor("red"))
for i in xrange(1, xdata.size):
self.scene.addLine(xdata[i-1], ydata[i-1], xdata[i], ydata[i], pen)
if __name__=="__main__":
app = QG.QApplication(sys.argv)
gui = MainUI()
gui.setFixedSize(500,500)
gui.show()
app.exec_()
Just like Mad Physicist told you, set the pen to cosmetic or line-width of 0 (are equal in the behavior) and you have a non-scaling pen.
>> pen.setCosmetic(True);

How to prevent recursion with interactive plot in bqplot?

I created an interactive scatterplot using bqplot where you are allowed to drag points around (using enable_move=True).
I don't want the user to drag points above the line y=x.
If they do, I want the point to snap back to where it was most recently.
The problem is that I'm not sure how to avoid infinite recursion here.
The scatterplot needs to be aware of when its points are moved in order to check the move and possibly snap back.
However, when it begins to snap back, this change (of the point positions) seems to trigger that same callback.
Can anyone tell me the "correct" way to deal with this basic issue?
import bqplot.pyplot as plt
import numpy as np
def on_point_move(change, scat):
if np.any(newx < scat.y):
scat.x = change['old']
fig = plt.figure(animation_duration=400)
xs = 1.0*np.arange(3) # make sure these are floats
ys = 1.0*np.arange(3)
scat = plt.scatter(xs, ys, colors=['Red'], default_size=400, enable_move=True)
scat.observe(lambda change: on_point_move(change, scat), names=['x'])
fig
You can temporarily disable the observe in the on_point_move function. I've changed the logic a bit too.
import bqplot.pyplot as plt
import numpy as np
def on_point_move(change):
if np.any(scat.x < scat.y):
scat.unobserve_all()
if change['name'] == 'x':
scat.x = change['old']
elif change['name'] == 'y':
scat.y = change['old']
scat.observe(on_point_move, names=['x','y'])
fig = plt.figure(animation_duration=400)
xs = 1.0*np.arange(3) # make sure these are floats
ys = 1.0*np.arange(3)
scat = plt.scatter(xs, ys, colors=['Red'], default_size=400, enable_move=True)
scat.observe(on_point_move, names=['x','y'])
fig

Only getting bottom of blackbody curve rather than whole function

I'm trying to plot the blackbody distribution at T = 2.73, I'm getting a curve but not a complete one and can only see the very bottom of the curve. Any suggestions? Here is my code:
import matplotlib.pyplot as plt
from numpy import *
from scipy import *
h=6.62606957e-34
c=2.998e8
k=1.3806488e-23
T = 100
f = arange(0,10000)
E = (8*h/(c**3.0))*(f**3.0)*(1/(exp((h*f)/(k*T))-1))
plt.plot(f, E*10000)
plt.title('Black-Body spectrum for T = 2.725K')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Energy Density (f)')
plt.show()
Use something like
x = linspace(0,12,1501)
f = 10**x
eterm = exp(-(h*f)/(k*T))
E = (2*h)/(c**2) * f**3 * eterm/(1-eterm)
I introduced the eterm exponential term to convert the overflow in the exponential into a less harmful underflow.
Check your sources and compare to wikipedia on Planck's law to get the right formula.
You can now also use
plot(x,E)
to get the x axis in log-scale, since if you get the right end of the x interval wrong, the graph will be uninformatively squished to the y axis.

How to refer the center of a giant component to an external coordinate system?

I am working in Python (package: NetworkX) with a network of, say, 100 nodes. I create it and then fragment it by removing a fraction of its nodes (removal), as shown below. The script computes the length of the largest component and its center node(s).
import networkx as nx
import matplotlib.pyplot as plt
import numpy
N = 10
G=nx.grid_2d_graph(N,N)
pos = dict( (n, n) for n in G.nodes() )
labels = dict( ((i, j), i + (N-1-j) * N ) for i, j in G.nodes() )
nx.relabel_nodes(G,labels,False)
pos = {y:x for x,y in labels.iteritems()}
nx.draw_networkx(G, pos=pos, with_labels=True, node_size = 300)
plt.axis('off')
plt.show()
plt.close()
removal=numpy.array([1,5,18,23,54,8,36,95,41,75,77,56,29,39,81,76,27,34,52,50,53,64,45,85])
G.remove_nodes_from(removal)
nx.draw_networkx(G, pos=pos, with_labels=True, node_size = 300)
plt.axis('off')
plt.show()
giant = max(nx.connected_component_subgraphs(G), key=len) #The largest component
center_nodes = nx.center(giant) #The center node(s)
print len(giant)
print center_nodes
This gives me:
len(giant)=29 and center_nodes=[12,13].
What the network looks like after removal:
My network is embedded in a 2D grid which measures (N+1)x(N+1), and has its own coordinate system. Every node of the network is to be seen as if it was placed at the intersection of each cell in the grid below:
My problem: How can I "translate" the result given by center_nodes=[12,13] into the location of cell A in the grid? In this case, I would like to have center_nodes=[12,13] -> center_coord=13.
PS: if I change removal, len(center_nodes) changes, as does the shape of the connected subgraphs. Thus, cell A will not be in the same position as above. To account for this, I would like to be able to always get the grid coordinates of the cell at the top left corner of the center_nodes cluster, regardless of its shape and location within the network.

Resources