Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How would I go about programming a gravity simulator? I am making a kind of 2d space-simulator and what I want is to have a planet (a center of gravity) to pull objects towards it. The object is a spaceship (basically just x and y-coordinates).
Use Newton's laws - the forces two objects feel are attractive (from one objects mass center point to the other's), equal to each other, and in value equal to g*m1*m2/(d*d) (where g is a constant, m1 and m2 are the masses and d is the distance of the center points.
However, if you have a planet, the effect of the spaceship's attraction to the planet is negligible, so one usually does not do the computations for the planet; it's just stationary.
Keep in mind that F=m*a, where F is the force calculated above, m the mass of the spaceship and a is the acceleration of the object. Based on the acceleration you calculate the speed, and based on the speed the position.
Check out Princeton's N-Body assignment. It describes what you want.
However, in the interest of quick summaries, you can derive the equations from basic trigonometry and Newton's Law of Universal Gravitation:
F = GMm/(r^2)
where F = force between two objects, G = gravitational constant, M and m are the relevant masses, and r is the distance between them.
A little mathemagic and you get the following results:
F_x = F(x_2 - x_1)/r
F_y = F(y_2 - y_1)/r
where F_x is the gravitational force in the x direction (same for F_y, but in y direction), x_2 and y_2 is the position of one of your objects, x_1 and y_1 the position of the other, F is as defined above, and r is the distance between them.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm taking algorithms class this semester and we have a midterm coming up. There's a question on the past midterm. We are given a list of n dogs and a list of m assertions of the form R(x,y) which means x and y have the same breed. It's given that R is an equivalence relation. Given 2 dogs x and y, we need to formulate an algorithm to find if they have the same breed or not.
The most trivial way of doing this would be going over the list of assertions and checking if the pair (x,y) exists or not but that won't work in all the cases.
Let's say we have Dogs = {a,b,c,d} and assertions = {(a,b),(b,d)}. We are asked to check if a and d have the same breed. What would be a good starting point for designing this algorithm?
Let’s interpret this input as a graph, and each dog will be vertex and input will be edge between two dogs and your answer will be true if it is in one component, obviously you can find all components with DFS algorithm.
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 5 years ago.
Improve this question
I'm looking for an algorithm to combine two values;
Where the first value indicates a positive result when the value is higher. For example, 0.98 is 'good' and 0.15 is 'bad'.
Where the second value indicates a positive result when the value is lower. For example, 10,000 is 'bad', whereas 1000 is 'good'.
I need a method of determining a value that can represent both of these scales with one number, so that I can sort my findings on my application from high to low accordingly. I'm not sure if anyone knows of such an algorithm, or any advice, but any help is greatly appreciated. Thank you.
P.S. I am aware I can 'negate' one of the two values, to have them appear on a similar scale, however I'm not sure how this would work in Java.
EDIT: Sorry, so to elaborate, I'm sorting images based on similarity to a user input image. Each of my algorithms that I'm using to return a value of similarity, function on a different scale. The first being a value between 0.00 and 1.00, with numbers being closer to 1.00, indicating the image is more similar to the original. Whereas, my second algorithm returns values from 1000+, with higher values indicating the image is less similar to the original. I need to take these two values and combine them to allow me to sort the resulting images in order of similarity, with the most similar image being shown at the top of my list, and the least similar at the bottom. Hopefully this helps clear up any confusion. Thanks again.
If your only goal is sorting, you need to come up with a function g(x,y) that represents the "goodness" of your pair of values. A pair (x1,y1) is better than (x2,y2) if and only if g(x1,y1) > g(x2,y2).
The function must represent what you consider "good". A simple example would be:
g(x,y) = x - y / 10000
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I'm looking into absolute function minimization problem in Java. Can anyone suggest libraries and solutions?
I downloaded apache common math3, however, could not find related functions to create linear function to minimize absolute functions.
Simply, I am looking into writing below functions in Java.
f(x) = b + | x - a | + d + | x - c | + f + | x - e |
I've edited this problem a bit for my second problem, here b, d, f are also linear functions.
b = a1x + b1
d = a2x + b2
f = a3x + b3
If you are summing N terms of the form |x - a_i|, consider the gradient as x increases from negative infinity:
At negative infinity, and for most of the left-hand number line, the gradient is -N;
As you pass the minimum of the a_i values, the gradient increases slightly, to -N+2;
As you pass the next smallest of the a_i values, the gradient increases again, to -N+4.
Each further a_i passed, the gradient increases by 2;
At positive infinity, the gradient is +N.
So, the gradient starts negative, increases in steps at each of the a_i positions, and ends up positive; you're looking for the point or range where the gradient is zero. This will occur "in the middle", i.e. at the median of the values a_i.
If there are an odd number of points, the median will be at a point. This is the case in the question: the minimum is at median(a, c, e).
If there are an even number of points, the median is between two points. In this case, the function is minimised anywhere between those points.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I'm trying to understand exactly why the Bellman-Ford Algorithm would not work with a negative weight cycle. I do understand that negative weight cycles will prevent the program from giving the right answer. But what exactly happens in the program if there is negative weight cycle?
Thanks for your help
The Bellman-Ford algorithm finds the shortest path from a source vertex to all other vertices in a weighted graph.
The issue with negative-weight cycles is that there is no shortest-path.
Without drawing, consider the case of 4 nodes, where A is the source, and nodes B,C,D are other vertices.
Weights of all the edges are as follows:
w(A,B) = 1
w(B,C) = 1
w(C,D) = 1
Bellman-Ford would conclude the following shortest path lengths
path(A~B) = 1
path(A~C) = 2
path(A~D) = 3
But what if we added an edge that created a negative weight cycle. For example an edge from C to B with a weight of -2.
w(C,B) = -2
Now there is a negative weight cycle. By walking (B,C,B) we get a total path weight of -1 (1 + -2).
If we were to run Bellman-Ford again, it would give us what it thinks is a shortest-path from A to D just as it did previously.[Note 1]
But this time, it would be wrong.
In fact, it wouldn't matter what integer shortest path weight the algorithm gave us because we could always find one that was shorter.
For example, say the algorithm gave us the original path weight of 3 (A,B,C,D). Well, it's easy to see that we could construct a shorter path: (A,B,C,B,C,D) which gives us a path weight of 2.
But that's not the shortest path either. For example (A,B,C,B,C,B,C,D) gives us a path weight of 1.
As you can see, we can construct an arbitrarily short path weight by repeatedly looping between vertices B and C. This is only true because our graph contains a negative weight cycle.
So it's not that Bellman-Ford doesn't correctly find the shortest path.
... More accurately, there is no shortest path.
[1] It's not difficult to detect negative weight cycles in Bellman-Ford, so this assumes a naive implementation.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have four points that make concave quad:
a(3 , 11)
b(11 , 9)
c(18 , 10)
d(8 , 1)
now i wanna get the inner angle of the (b) corner in degrees.
note: the inner angle is greater than 180 degree.
The safest way is to use inner product and cross product , the inner product can be calculated using the 4 points of the two vectors ( a->b , b->c) and using the inner product formula
<ab,bc> = |ab|*|bc|* cos (abc)
cos(abc) = |ab|*|bc| / <ab,bc>
this is not enough to allocate the angel uniquely since an angle and its complement has the same cos but has different sins , and here is where the cross product comes to solve the problem
ab * bc = |ab|*|bc| * sin(abc)
the left part can be calculated using ending points coordinates so you can calculate the sin , once sin and cos is calculated you can specify the angel appropriately .
Point d is just a distraction here. Consider the (abc) triangle:
It is easy to determine the length of ab, bc and ca from the coordinates.
you can then determine the (ab, bc) internal angle with sin, cos or tan
the angle you look for is 360 minus the (ab, bc) internal angle.