Graphing relativistic kinetic energy - python

I wrote a function that calculates relativistic kinetic energy but I'm having some problems graphing it. I'm working in Python Jupyter.
I'm using Taylor for very small values of v (so I don't end up with E=0 because of cancellation) and the original expression for the biggest values.
I have to calculate it for v between 1e-6 and 2.88e8.
m=1
c=3e8
def E(v):
if (v/c) <= 0.8860:
return ((m*v**2)/2) + ((3*m*v**4)/(8*c**2)) + ((5*m*v**6)/(16*c**4)) + ((35*m*v**8)/(128*c**6))
else:
return (m*c**2)*((1/sp.sqrt(1-(v**2/c**2)))-1)
This gives me very accurate results.
Now the problem is graphing E(v). I have to use matplotlib (+ numpy and scipy for everything else). Here is what I wrote for a log-log graph.
xs = np.logspace(-6, 8.5)
fig, ax = plt.subplots(figsize=[10,10])
ax.plot(xs, E(xs))
ax.set_xscale('log')
ax.set_yscale('log')
I am getting a value error :
"ValueError: The truth value of an array with more than one element
is ambiguous. Use a.any() or a.all()", and it points at "if (v/c) <= 0.8860:".
what's this error means and what I did wrong. I'd be very grateful if you could help me graph or change my function.

You need to create a loop rather than just a condition:
m=1
c=3e8
def E(v):
for i in range(len(v)):
if (v[i]/c) <= 0.8860:
return ((m*v**2)/2) + ((3*m*v**4)/(8*c**2)) + ((5*m*v**6)/(16*c**4)) + ((35*m*v**8)/(128*c**6))
else:
return (m*c**2)*((1/sp.sqrt(1-(v**2/c**2)))-1)
xs = np.logspace(-6, 8.5)
fig, ax = plt.subplots(figsize=[10,10])
ax.plot(xs, E(xs))
ax.set_xscale('log')
ax.set_yscale('log')
The condition is expected a value for 'v' to compare to the 0.8860.
when presented with an array that has multiple values it says that it can't give True or False for an array.
all() and any() are methods to extract True or False from a list or an array:
any() will give True if any is True and all() will only if all is True.

The statement if (v/c) <= 0.8860 is not well defined, because for some values in v this is True for others it is False. This is a bit like asking "Are people taller than 1.8 meters?" There is no definitive answer; some are, some are not. The error tells you to specify exactly that: Do you want to know if all people are bigger or if any one person is bigger than 1.8m? Here you want to actually know something else: You want for each entry a different result, depending on the condition.
You may use numpy.piecewise to define a function differently for certain conditions. Also mind that no entry of v should be larger than c as this would result in an infinite energy.
import numpy as np
import matplotlib.pyplot as plt
m=1
c=3.e8
lowv = lambda v:((m*v**2)/2) + ((3*m*v**4)/(8*c**2)) + ((5*m*v**6)/(16*c**4)) + ((35*m*v**8)/(128*c**6))
highv = lambda v:(m*c**2)*(1./np.sqrt(1-(v/c)**2)-1)
E = lambda v : np.piecewise(v, [(v/c) <= 0.8860, (v/c) > 0.8860], [lowv,highv])
xs = 2.9999*np.logspace(-6, 8, num=1000)
fig, ax = plt.subplots(figsize=[10,10])
ax.plot(xs, E(xs))
ax.set_xscale('log')
ax.set_yscale('log')
plt.show()
Alternatively you may use numpy.vectorize to make the existing function evaluated for each entry in v individually. This is of course less efficient and should not be used for large arrays. Here is does not matter too much.
import numpy as np
import matplotlib.pyplot as plt
m=1
c=3.e8
def E(v):
if (v/c) <= 0.8860:
return ((m*v**2)/2) + ((3*m*v**4)/(8*c**2)) + ((5*m*v**6)/(16*c**4)) + ((35*m*v**8)/(128*c**6))
else:
return (m*c**2)*((1/np.sqrt(1-(v**2/c**2)))-1)
Ev = np.vectorize(E)
xs = 2.9999*np.logspace(-6, 8, num=1000)
fig, ax = plt.subplots(figsize=[10,10])
ax.plot(xs, Ev(xs))
ax.set_xscale('log')
ax.set_yscale('log')
plt.show()

Related

Solve nonlinear equation in python

I am trying to find the fundamental TE mode of the dielectric waveguide. The way I try to solve it is to compute two function and try to find their intersection on graph. However, I am having trouble get the intersect point on the plot.
My code:
def LHS(w):
theta = 2*np.pi*1.455*10*10**(-6)*np.cos(np.deg2rad(w))/(900*10**(-9))
if(theta>(np.pi/2) or theta < 0):
y1 = 0
else:
y1 = np.tan(theta)
return y1
def RHS(w):
y = ((np.sin(np.deg2rad(w)))**2-(1.440/1.455)**2)**0.5/np.cos(np.deg2rad(w))
return y
x = np.linspace(80, 90, 500)
LHS_vals = [LHS(v) for v in x]
RHS_vals = [RHS(v) for v in x]
# plot
fig, ax = plt.subplots(1, 1, figsize=(6,3))
ax.plot(x,LHS_vals)
ax.plot(x,RHS_vals)
ax.legend(['LHS','RHS'],loc='center left', bbox_to_anchor=(1, 0.5))
plt.ylim(0,20)
plt.xlabel('degree')
plt.ylabel('magnitude')
plt.show()
I got plot like this:
The intersect point is around 89 degree, however, I am having trouble to compute the exact value of x. I have tried fsolve, solve to find the solution but still in vain. It seems not able to print out solution if it is not the only solution. Is it possible to only find solution that x is in certain range? Could someone give me any suggestion here? Thanks!
edit:
the equation is like this (m=0):
and I am trying to solve theta here by finding the intersection point
edit:
One of the way I tried is as this:
from scipy.optimize import fsolve
def f(wy):
w, y = wy
z = np.array([y - LHS(w), y - RHS(w)])
return z
fsolve(f,[85, 90])
However it gives me the wrong answer.
I also tried something like this:
import matplotlib.pyplot as plt
x = np.arange(85, 90, 0.1)
f = LHS(x)
g = RHS(x)
plt.plot(x, f, '-')
plt.plot(x, g, '-')
idx = np.argwhere(np.diff(np.sign(f - g)) != 0).reshape(-1) + 0
plt.plot(x[idx], f[idx], 'ro')
print(x[idx])
plt.show()
But it shows:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
First, you need to make sure that the function can actually handle numpy arrays. Several options for defining piecewise functions are shown in
Plot Discrete Distribution using np.linspace(). E.g.
def LHS(w):
theta = 2*np.pi*1.455*10e-6*np.cos(np.deg2rad(w))/(900e-9)
y1 = np.select([theta < 0, theta <= np.pi/2, theta>np.pi/2], [np.nan, np.tan(theta), np.nan])
return y1
This already allows to use the second approach, plotting a point at the index which is closest to the minimum of the difference between the two curves.
import numpy as np
import matplotlib.pyplot as plt
def LHS(w):
theta = 2*np.pi*1.455*10e-6*np.cos(np.deg2rad(w))/(900e-9)
y1 = np.select([theta < 0, theta <= np.pi/2, theta>np.pi/2], [np.nan, np.tan(theta), np.nan])
return y1
def RHS(w):
y = ((np.sin(np.deg2rad(w)))**2-(1.440/1.455)**2)**0.5/np.cos(np.deg2rad(w))
return y
x = np.linspace(82.1, 89.8, 5000)
f = LHS(x)
g = RHS(x)
plt.plot(x, f, '-')
plt.plot(x, g, '-')
idx = np.argwhere(np.diff(np.sign(f - g)) != 0).reshape(-1) + 0
plt.plot(x[idx], f[idx], 'ro')
plt.ylim(0,40)
plt.show()
One may then also use scipy.optimize.fsolve to get the actual solution.
idx = np.argwhere(np.diff(np.sign(f - g)) != 0)[-1]
from scipy.optimize import fsolve
h = lambda x: LHS(x)-RHS(x)
sol = fsolve(h,x[idx])
plt.plot(sol, LHS(sol), 'ro')
plt.ylim(0,40)
plt.show()
Something quick and (very) dirty that seems to work (at least it gave theta value of ~89 for your parameters) - add the following to your code before the figure, after RHS_vals = [RHS(v) for v in x] line:
# build a list of differences between the values of RHS and LHS
diff = [abs(r_val- l_val) for r_val, l_val in zip(RHS_vals, LHS_vals)]
# find the minimum of absolute values of the differences
# find the index of this minimum difference, then find at which angle it occured
min_diff = min(diff)
print "Minimum difference {}".format(min_diff)
print "Theta = {}".format(x[diff.index(min_diff)])
I looked in range of 85-90:
x = np.linspace(85, 90, 500)

Complete turn x angle values are mapped to half turn using asin function, how to mirror them back?

I have angles that form a complete turn in an array x, from -90 to 270 e.g. (it may be defined otherwise, like from 0 to 360 or -180 to 180) with step 1 or whatever.
asin function is valid only between -90 and +90.
Thus, angles < -90 or > 90 would be "mapped" between these values.
E.g. y = some_asin_func(over_sin(x)) will end up in an y value that is always between -90 and +90. So y is stuck between -90 and +90.
I do need to retrieve to which x-input is y related, because it's ambiguous yet: for example, the function over (x) will give the same y values for x = 120 and x = 60, or x = -47 and x = 223. Which is not what I want.
Put an other way; I need y making a complete turn as x does, ranging from where x starts up to where x ends.
An image will be better:
Here, x ranges between -90 (left) to 270 (right of the graph).
The valid part of the curve is between x=-90 and x=+90 (left half of the graph).
All other values are like mirrored about y=90 or y=-90.
For x=180 for example, I got y=0 and it should be y=180.
For x=270, I have y=-90 but it should be y=270, thus +360.
Here's a code sample:
A = 50 # you can make this value vary to have different curves like in the images, when A=0 -> shape is triangle-like, when A=90-> shape is square-like.
x = np.linspace(-90,270,int(1e3))
u = np.sin(math.pi*A/180)*np.cos(math.pi*x/180)
v = 180*(np.arcsin(u))/math.pi
y = 180*np.arcsin(np.sin(math.pi*x/180)/np.cos(math.pi*v/180))/math.pi
plt.plot(x,y)
plt.grid(True)
Once again, first left half of the graph is completely correct.
The right half is also correct in its behavior, but in final, here, it must be mirrored about an horizontal axis at position y=+90 when x>90, like this:
That is, it's like the function is mirrored about y=-90 and y=+90 for y where x is out of the range [-90,+90] and only where where x is out of the range [-90,+90].
I want to un-mirror it outside the valid [-90,+90] range:
about y=-90 where y is lower than -90
about y=+90 where y is greater than +90
And of course, modulo each complete turn.
Here an other example where x ranges from -180 to 180 and the desired behavior:
Yet:
Wanted:
I have first tested some simple thing up now:
A = 50
x = np.linspace(-180,180,int(1e3))
u = np.sin(math.pi*A/180)*np.cos(math.pi*x/180)
v = 180*(np.arcsin(u))/math.pi
y = 180*np.arcsin(np.sin(math.pi*x/180)/np.cos(math.pi*v/180))/math.pi
for i,j in np.ndenumerate(x):
xval = (j-180)%180-180
if (xval < -90):
y[i] = y[i]-val
elif (xval > 90):
y[i] = y[i]+val
plt.plot(x,y);
plt.grid(True)
plt.show()
which doesn't work at all but I think the background idea is there...
I guess it may be some kind of modulo trick but can't figure it out.
Here a solution that fixes the periodicity of the cos function 'brute force' by calculating an offset and a sign correction based on the x value. I'm sure there is something better out there, but I would almost need a drawing with the angles and distances involved.
from matplotlib import pyplot as plt
import numpy as np
fig, ax = plt.subplots(1,1, figsize=(4,4))
x = np.linspace(-540,540,1000)
sign = np.sign(np.cos(np.pi*x/180))
offset = ((x-90)//180)*180
for A in range(1,91,9):
u = np.sin(np.pi*A/180)*np.cos(np.pi*x/180)
v = 180*(np.arcsin(u))/np.pi
y = 180*np.arcsin(np.sin(np.pi*x/180)/np.cos(np.pi*v/180))/np.pi
y = sign*y + offset
ax.plot(x,y)
ax.grid(True)
plt.show()
The result for the interval [-540, 540] looks like this:
Note that you can get pi also from numpy, so you don't need to import math -- I altered the code accordingly.
EDIT:
Apparently I first slightly misunderstood the OP's desired output. If the calculation of offset is just slightly changed, the result is as requested:
from matplotlib import pyplot as plt
import numpy as np
fig, ax = plt.subplots(1,1, figsize=(4,4))
x = np.linspace(-720,720,1000)
sign = np.sign(np.cos(np.pi*x/180))
offset = ((x-90)//180 +1 )*180 - ((x-180)//360+1)*360
for A in range(1,91,9):
u = np.sin(np.pi*A/180)*np.cos(np.pi*x/180)
v = 180*(np.arcsin(u))/np.pi
y = 180*np.arcsin(np.sin(np.pi*x/180)/np.cos(np.pi*v/180))/np.pi
y = sign*y + offset
ax.plot(x,y)
ax.grid(True)
plt.show()
The result now looks like this:
Thank you #Thomas Kühn, it seems fine except I wanted to restrict the function in a single same turn in respect to y-values. Anyway, it's only aesthetics.
Here's what I found by my side. It's maybe not perfect but it works:
A = 50
u = np.sin(math.pi*A/180)*np.cos(math.pi*x/180)
v = 180*(np.arcsin(u))/math.pi
y = 180*np.arcsin(np.sin(math.pi*x/180)/np.cos(math.pi*v/180))/math.pi
for i,j in np.ndenumerate(x):
val = (j-180)%360-180
if (val < -90):
y[i] = -180-y[i]
elif (val > 90):
y[i] = 180-y[i]
Here are some expected results:
Range from -180 to +180
Range from 0 to +360
Range from -720 to +720
Range from -360 to +360 with some different A values.
Funny thing is that it reminds me some electronics diagrams as well.
Periodic phenomenons are everywhere!

if y>0.0 and x -y>=-Q1: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I have been trying to get this to work for a while now, but still not finding a way. I am trying to compute the Look ahead estimate density of a piecewise gaussian function. I'm trying to estimate the stationary distribution of a piecewise normally distributed function. is there a way to avoid the error type:
Error-type: the truth value of an array with more than one element is ambiguous. Use a.any() or a.all().
for instance y=np.linspace(-200.0,200.0,100) and x = np,linspace(-200.0,200.0,100). then verify the condition as stated in the code below?
import numpy as np
import sympy as sp
from numpy import exp,sqrt,pi
from sympy import Integral, log, exp, sqrt, pi
import math
import matplotlib.pyplot as plt
import scipy.integrate
from scipy.special import erf
from scipy.stats import norm, gaussian_kde
from quantecon import LAE
from sympy.abc import q
#from sympy import symbols
#var('q')
#q= symbols('q')
## == Define parameters == #
mu=80
sigma=20
b=0.2
Q=80
Q1=Q*(1-b)
Q2=Q*(1+b)
d = (sigma*np.sqrt(2*np.pi))
phi = norm()
n = 500
#Phi(z) = 1/2[1 + erf(z/sqrt(2))].
def p(x, y):
# x, y = np.array(x, dtype=float), np.array(y, dtype=float)
Positive_RG = norm.pdf(x-y+Q1, mu, sigma)
print('Positive_R = ', Positive_RG)
Negative_RG = norm.pdf(x-y+Q2, mu, sigma)
print('Negative_RG = ', Negative_RG)
pdf_0= (1/(2*math.sqrt(2*math.pi)))*(erf((x+Q2-mu)/(sigma*np.sqrt(2)))-erf((x+Q1-mu)/(sigma*np.sqrt(2))))
Zero_RG =norm.pdf
print('Zero_RG',Zero_RG)
print ('y',y)
if y>0.0 and x -y>=-Q1:
#print('printA', Positive_RG)
return Positive_RG
elif y<0.0 and x -y>=-Q2:
#print('printC', Negative_RG)
return Negative_RG
elif y==0.0 and x >=-Q1:
#print('printB', Zero_RG)
return Zero_RG
return 0.0
Z = phi.rvs(n)
X = np.empty(n)
for t in range(n-1):
X[t+1] = X[t] + Z[t]
#X[t+1] = np.abs(X[t]) + Z[t]
psi_est = LAE(p, X)
k_est = gaussian_kde(X)
fig, ax = plt.subplots(figsize=(10,7))
ys = np.linspace(-200.0, 200.0, 200)
ax.plot(ys, psi_est(ys), 'g-', lw=2, alpha=0.6, label='look ahead estimate')
ax.plot(ys, k_est(ys), 'k-', lw=2, alpha=0.6, label='kernel based estimate')
ax.legend(loc='upper left')
plt.show()
See all those ValueError questions in the side bar????
This error is produced when a boolean array is used in a scalar boolean context, such as if or or/and.
Try your y or x in this test, or even simpler one. Experiment in a interactive shell.
if y>0.0 and x -y>=-Q1: ....
if y>0:
(y>0.0) and (x-y>=10)
will all produce this error with your x and y.
Notice also that I edited your question for clarity.
Error starts with quantecon.LAE(p, X), which expects a vectorized function p. Your function isn't vectorized, which is why everything else doesn't work. You copied some vectorized code, but left a lot of things as sympy style functions which is why the numpy folks were confused about what you wanted.
In this case "vectorized" means transforming two 1D arrays with length n into a 2D n x n array. In this case, you don't want to return 0.0, you want to return out a 2d ndArray which has the value 0.0 in locations out[i,j] where a boolean mask based on a function of x[i], y[j] is false.
You can do this by broadcasting:
def sum_function(x,y):
return x[:, None] + y[None, :] # or however you want to add them, broadcasted to 2D
def myFilter(x,y):
x, y = x.squeeze(), y.squeeze()
out=np.zeros((x.size,y.size))
xyDiff = x[:, None] - y[None, :]
out=np.where(np.bitwise_and(y[None, :] => 0.0, xyDiff >= -Q1), sum_function(x, y), out) # unless the sum functions are different
out=np.where(np.bitwise_and(y[None, :] < 0.0, xyDiff >= -Q2), sum_function(x, y), out)
return out

Why the two arguments of graphs are not consider as equal?

Let's say i have an object which I have to destroy by shooting (projectile motion). The position of the object is random (as for now, just integers, to make it easier). Even when my 'bullet' looks to be just in place, the loop doesn't break. Probably the program doesn't consider graph 1 and graph 2 as equal at any point.
I tried few things about that if condition but it nothing worked.
Can anyone tell me what I must add/change?
import matplotlib.pylab as plt
import numpy as np
import random
g = 10
c = []
d = []
fig = plt.figure()
L = random.randint(5.0,18.0)
while True:
try:
#velocity
v = float(input("What is the velocity?\n>"))
#angle
a = np.radians(float(input("What is the angle?\n>")))
z = np.sin(2*a)*v**2/g #max range
h = ((v**2*(np.sin(a))**2)/(2*g)) #max. height
x= np.linspace(0, z, 1000)
#y values
y = (x*np.tan(a) - (g*x**2)/(2*v**2*((np.cos(a))**2)))
ax = plt.axes(xlim=(0, 1.5*L), ylim=(0, 1.2*h))
plt.ion() #interactive graph
#previous tries
plt.plot(c,d, '.', color = 'lightgrey')
plt.pause(0.01)
#enemy object
graph1 = plt.plot(L, 0, 'o', color = 'r', markersize=30)
plt.pause(0.01)
#actual shoot
graph2 = plt.plot(x,y, '.', color = 'b', ms = 7)
plt.pause(0.01)
if np.any(graph2) == np.any(graph1):
print("You have destroyed the enemy object!")
plt.show()
break
else:
print("You've missed. Keep shooting!")
c.append(x)
d.append(y)
plt.show()
continue
except ValueError:
print("Sorry, I can't understand.")
You don't need to plot at all to find the intersection. In fact, I don't think matplotlib can help here. The returned value of plt.plot is a list containing a single Line2D object - you could get back your original x and y values by doing
x_new = graph1[0].get_xdata()
y_new = graph1[0].get_ydata()
However, you'd only do that if your code was for some reason analyzing plots generated by a completely different function and wasn't allowed to have the original data. In your case, just use your x and y directly and find the intersection. It looks like you might be trying to do
if np.any((y == 0) * (x == L)):
print "You have destroyed the enemy object!"
Which should check if any point (x,y) is located at (0,L). The * acts as an element-by-element 'and' operator for boolean arrays. Here are some more comprehensive answers about finding intersections of two arrays.
If you're trying to build a game, look at pygame. It has all kinds of methods to detect collisions much more easily than in numpy.

Plot a piecewise function in 3D

I am new to python and trying to 3d plot a piecewise function. I am trying to 3d plot the 'mainformula' function below on the z-axis as it varies with x and y ranging from 0 to 10, and constant = 1. But I can't quite seem to figure out the plotting method here.
from sympy import *
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np
def mainformula(x,y,constant):
return Piecewise((subformula1(x,y,constant), y >= 0 and y < 3),(subformula2(x,y,constant),y>=3 and y <= 10))
def subformula1(x,y,constant):
return x + y + constant
def subformula2(x,y,constant):
return x - y - constant
fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(0, 10, 0.25)
Y = np.arange(0, 10, 0.25)
constant = 1
X, Y = np.meshgrid(X, Y)
Z = mainformula(X,Y,constant)
surf = ax.plot_surface(X, Y, Z)
plt.show()
The error I get when I run that code is: "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"
You are working on arrays so it's never going to work to use array > 3 in a boolean context (for example with and) this will always give you the error you received. But you can always define your conditions as boolean masks and operate your formula on the appropriate elements:
def mainformula(x,y,constant):
z = np.zeros_like(x)
# Condition 1 indexes all elements where subformula 1 is valid
condition1 = np.logical_and(y >= 0, y < 3)
# condition1 = (y >= 0) & (y < 3) # is another way of writing it
z[condition1] = x[condition1] + y[condition1] + constant
# now do it in the range where subformula 2 is valid
condition2 = np.logical_and(y >= 3, y <= 10)
# condition1 = (y >= 3) & (y <= 10) # is another way of writing it
z[condition2] = x[condition2] - y[condition2] - constant
return z
This doesn't use sympy.Piecewise but works alright when only trying to plot. If you want seperate functions instead of doing it all in the main formula you need to change it a bit:
z[condition1] = subformula1(x[condition1], y[condition1], constant)
and similar for condition2.
The problem is you are trying to make logic assertions into arrays that do not have a direct one. It's difficult for Python to assert what this is:
y >= 0 and y < 3
So you'll have to change somewhat your code into something it can understand:
def mainformula(x,y,constant):
y2 = y[y>=0]
y2 = y2[y2<3]
y3 = y[y>=3]
y3 = y2[y2<=10]
return Piecewise((subformula1(x,y,constant), y2),(subformula2(x,y,constant),y3))
The problem is Piecewise function also does not seem to accept some arrays into one of the arguments you have. You'll have to rethink your problem, perhaps by building a loop for the Piecewise function in order to be launched at every element.

Categories