Closed. This question is not about programming or software development. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 17 days ago.
Improve this question
assuming that it has a database with all the longitudinal degrees of each planet inside it.
So for every day of the year I would have a similar result:
Sun: 135.45°
Mars: 29.72°
Jupiter: 56.89°
Uranus: 45.27°
Pluto: 165.78°
Venus: 89.34°
Mercury: 312.22°
Saturn: 267.86°
Moon: 92.78°
Earth: 33.23°
in which way could I calculate mathematically the aspects between every planet? By aspects I mean when a planet is 90° opposite from its current position, or -90°.
Same situation for an aspect from 60°, 30° and so on.
More than a python problem is a logic problem, I'm asking someone with more experience for help. I state that I am not using any astrological library, up to this point.
I can convert database values to any form (list, tuple, dictionary)
To calculate the aspects between planets, you need to compare the longitudinal degrees to which you have access with the other planets. This can be done by subtracting the degrees from one planet from another and then modulo with 360. If I understood what you want correctly, something like this should work:
# get values from database into a dictionary
planets = {
"Sun": 135.45,
"Mars": 29.72,
"Jupiter": 56.89,
"Uranus": 45.27,
"Pluto": 165.78,
"Venus": 89.34,
"Mercury": 312.22,
"Saturn": 267.86,
"Moon": 92.78,
"Earth": 33.23
}
# this function returns true if the difference between two planets' degrees is
# withing the range from 0 to degrees or within 360 - degrees and 360
def aspect(planet1, planet2, degrees):
diff = abs(planets[planet1] - planets[planet2]) % 360
return diff <= degrees or diff >= 360 - degrees
for planet1 in planets:
for planet2 in planets:
if planet1 != planet2:
if aspect(planet1, planet2, 90):
print(f"{planet1} and {planet2} are in a square aspect")
elif aspect(planet1, planet2, 60):
print(f"{planet1} and {planet2} are in a sextile aspect")
elif aspect(planet1, planet2, 30):
print(f"{planet1} and {planet2} are in a trine aspect")
This will output the aspects between all the planets in the dictionary planets. You can adjust the degrees parameter to match the aspect you want to calculate.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am trying to mimic a chance encounter and I'm not sure how to do this. I would like to use a poisson distribution, but am open to other suggestions.
The idea is this: there is a central place where you can meet people. If there are 10 (the minimum) people at this place, the chance is 100% you will meet a specific person (e.g., chance = 1). If there are 6000 (the maximum) people at this place, the chance you will meet everyone is 3.5% you will meet a specific person (e.g. Chance = 0.035). How can I implement this using Python?
Not exactly sure if you can do Poisson distribution without μ.
This function will return a chance based on n, which is the number of people, and the given maximum and minimum values. In this calculation, the probability will be halved for each 1 point increase in the normalized value up to a certain point which is log(0.035)/log(0.5) or about 4.83650 because 0.5 ** 4.83650 ≈ 0.035. Meaning whenever n = maximum, the probability will be 0.035 and whenever n = minimum, the probability will be 1.
import math
def chance(n): # n is the number of people
maximum = 6000
minimum = 10
normalized = ((n - minimum) / (maximum - minimum)) * (math.log(0.035) / math.log(0.5))
probability = 0.5 ** normalized
return probability
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have to create a code for the classic physics problem. link to the problem's explanation: Link
but in my code, the user inputs the speed of the trains, the distance between them and the speed of the bird.It needs to output the total distance traveled by the bird and the trains and the birds' position each 0,01 second. Below is my code and I want some help on improving it.
t=0
v1= input ("Insert the trains' speed= ")
d= input ("Insert the distance between the trains= ")
va= input ("Insert the bird's speed= ")
v1= float(v1)
d=float(d)
va=float(va)
s1=0
s2=d
while s1<=s2:
s1= v1*t
s2=d-v1*t
sa=va*t
t1=d/(2*v1)
da=t1*va
tx1= ("Position of train 1= %sm")
tx2= ("Position of train 2= %sm")
tx3= ("Bird's position= %sm")
print(tx1 % s1)
print(tx2 % s2)
print(tx3 % sa)
t=t+0.01
if s1==s2:
print ("The bird died")
txt4=("Total distance traveled by the bird= %sm")
print (txt4 % da)
Finally the bird is dead. Here is how it dies. The main modification is if abs(s1-s2) < 1e-8: to make your code work. The problem was that when s1 and s2 were getting equal, the condition if s1==s2: was not working because it was comparing numbers which were something like 14.9999999 and 15.0000000 which are though equal but represented in marginally different floating points. The simple solution for this is to check if the absolute difference between these two numbers is below some very small number such as 1e-8.
I also added a break in your code when the bird dies to come out of the loop. In case you don't want this, just remove it. I also shortened your code by removing unnecessary lines required for printing.
while s1<=s2:
s1 = v1*t
s2 = d-v1*t
sa = va*t
t1 = d/(2*v1)
da = t1*va
print("Position of train 1 = %s m"%s1)
print("Position of train 2 = %s m"%s2)
print("Bird's position = %s m"%sa)
t=t+0.01
if abs(s1-s2) < 1e-8:
print ("The bird died")
print("Total distance traveled by the bird= %s m" %da)
break
Output
Insert the trains' speed= 30
Insert the distance between the trains= 30
Insert the bird's speed= 20
Position of train 1 = 0.0 m
Position of train 2 = 30.0 m
Bird's position = 0.0 m
.
.
.
Bird's position = 10.000000000000004 m
The bird died
Total distance traveled by the bird= 10.0 m
In general, when doing comparisons between floating point numbers you want to avoid testing for exact equality.
For example, here you probably would like s1==s2 to return True when comparing, say, 2.001010191012393 == 2.001010191012394.
In this case, you could solve this by using if s1>=s2 instead of if s1==s2.
You could also use an else in the while like this,
x, y = 1, 5
while x<y:
x = x + 1
print x
else:
print "done"
More generally (that is, when the comparison is not one sided) you can use an expression like abs(s2-s1)<epsilon, where epsilon is an appropriately small number. In your case, this is a bit difficult because epsilon will be a function of the parameters in your simulation, and probably much larger than the machine precision but related instead to your timestep (0.01, in your case). Therefore, the one-sided inequality is better and easier.
it's my first time asking a question here and since i only started some days ago with coding i may need your help defining my problem a bit more precise.
I would like to simulate some keplerian orbits, but i have reoccurring problems with the required precision. For example when my point comes to close to the center of gravity it slings it out of every sensible orbit. (My theory is that the acceleration increases so drastically compared to the time-window dt, that it reaches escape velocity before gravity 'has a chance' to pull it back.) Another problem is that the orbit itself rotates around the center of gravity. I have not studied this problem in detail but i guess, this time small errors sum up thus creating this flower-like effect.
First thought why these errors occur:
As written in the title i use a simple Verlet integration to approximate the true solution. I know there are other possibilities like Runge-Kutta method, but i saw people doing some nice simulations with a simple Euler-Approximation. So i thought a second order Verlet-Approximation should be sufficient. Maybe this is not the case, so should i use another method of approximation?
Second idea:
I simply coded it badly and there are better ways to handle it, keeping numerical errors minimal. For further analysis i upload a snip of my small code here:
t = t + dt
i = 1
while t <= T and i <= n:
#A_list was the idea of calculating the acceleration more efficient, i append the necessary entries, calculate the acceleration and delete the entries again to repeat the process
A_list.append(x_list[i])
A_list.append(y_list[i])
r = np.sqrt(A_list[0]**2 + A_list[1]**2)
A_x = -(G*M/r**2) * A_list[0] * 1/r
A_y = -(G*M/r**2) * A_list[1] * 1/r
x_i1 = 2*A_list[0] - x_list[i-1] + A_x * dt**2
y_i1 = 2*A_list[1] - y_list[i-1] + A_y * dt**2
del A_list[1]
del A_list[0]
i = i + 1
t = t + dt
x_list.append(x_i1)
y_list.append(y_i1)
t_list.append(t)
I hope this was somewhat readable and there is someone who would like to help a young programmer out. :)
Pictures for clarification:
enter image description here
enter image description here
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
i want the code to ask for the radius and length then the program will calculate piradiusradius*length from the definition area the print out the radius for example if at asks me the radius i put 5 same with the length it should say the volume is 392.75
def calculate_volume(radius, length):
pi=3.142
vol=pi*radius*radius*length
#main program
radius = int(input("enter radius: "))
length = int(input("enter length: "))
volume = calculate_volume(radius, length)
print("the volume: ",volume)
You are missing the return statement:
def calculate_volume(radius, length):
pi = 3.142
vol = pi * radius * radius * length
return vol
The example is almost correct, you just haven't returned a result from your function!
def calculate_volume(radius, length):
pi=3.142
vol=pi*radius*radius*length
return(vol)
When you put volume = calculate_volume(radius, length) right now volume is being set to None instead of vol from inside of the function.
Or better yet -- Get in the habit of using type hints - then use a decent IDE (such as PyCharm), or a type checker (such as MyPy). PyCharm would have given you a warning that you didn't have a return statement -- and anything calling your function would know the types of parameters and the return type.
def calculate_volume(radius: float, length: float) -> float:
pi = 3.142
vol = pi*radius*radius*length
return vol
I am looking to do this for a community sublet advertising website, but theoretically the algorithm would be similar for any local search.
The less populated the area searched, the higher should be the default radius to search.
On the other hand areas with high population density should have low default radius to keep things locally relevant.
This might be more of a mathematical question than a programming one but code is very welcome. So far I have calculated amount of sublets within 15 miles of each town or village and saved this in the database as an approximation for density. I intended to use this number to figure out how far to go with the search when someone searches for the town or village.
To test any proposed solution, I pulled out some approximate numbers I would want the algorithm to come up with. If there is a lot of sublets within 15 miles of a point, say 30k, I would want the default radius to search to be around 3 miles. If there is very little say 1 or 2, the default radius should go high up to 25 miles, even more miles if there are no places around. A mid range area with say ~1k sublets would have a default radius of 15 miles. These are just examples, the density will off course grow or shrink a with number of things in the database.
Population -> Default search radius
0 -> very high (~60 miles or more)
1 -> 25 miles
1k -> 15 miles
30k -> 3 miles
Am I going in the right direction? Python or PHP would be preferred for code centric answers.
Thank you
A reasonable approach would be to define regions so that they contain the same number of people, and then there will be approximately the same number of available apartments in each region.
To write this mathematically:
N = total number of people within a region
d = population density of the region (taken to be what you list as population)
A = Area of region
R = Radius of the region A
So, N = d*A = d*pi*R*R, and we want N to be constant, so R = K*sqrt(1/D), where K is a constant chosen to match your numbers, or approximately 500 miles. Then,
30K -> 2.9 miles
1K -> 16 miles
1 -> 500 miles
So it works for the first two, though not the extreme case of a population of 1 (but it's not clear that 1 is truly an important case to consider, rather than a special case all of it's own). Anyway, I think this approach makes some sense and at least gives something to consider.