Why doesn't this magnetic perpetual motion machine work? I know that this machine does not work, via thermodynamics. I am asking for an analysis in terms of mechanics and magnetism.
Anyway, so here is the machine:

(source: cabinetmagazine.org) 
The magnet (the red ball) pull the ball up the ramp, and then it drops the ball through the hole, which then rolls down, and go ups the ramp again. Thermodynamics shows that this can not work.
From a mechanics and magnetism perspective, what happens when you do this, and why can't it happen?
I have a source saying that it would work if it were frictionless and we didn't try to extract energy from it (here), so in a certain sense, it is very close to possible (they didn't provide a full analysis though.)
Another Image:

 A: If the magnet is powerful enough to pull the ball up from the bottom of the ramp, the force on it will be quite strong at the top of the ramp.
If so, why would the ball drop through the hole?  The pull from the magnet will overwhelm gravity.
Even if you constructed one where the ball could fall through the upper hole, I don't see any reason why it should go through the lower hole.  If the magnet can pull it up the ramp, the pull from the magnet should prevent it from reaching the hole on the lower ramp.
A: There is no problem assuming that the ball will fall trough the hole. Even is the magnetic force is large, it only needs to be larger than the gravity component along the inclined surface. This component is $mg \cos \theta$. Once the ball gets to the hole the gravity felt by the ball increases to mg, so it can happens that the ball that initially went up now goes goes  down   the hole. 
However, notice that as the ball moves downward the gravity force starts to diminish again (is becoming more horizontal also the magnetic force decreases as the ball moves away from the magnet.  If you make a graph about both forces you will find that there is always a point on the ball's path when both forces have the same magnitude but opposite sign. That will be the equilibrium position of the ball, where it will stay at rest if you extract all its initial kinetic energy. Thus it is not a perpertum mobile after all.
A: Newtonian Physics certainly precludes the existence of any 'truly' perpetual motion. Under certain conditions, we can achieve near perpetual motion by judicious application of principles and then form questions like "Although no work can be removed from the system, is the lifetime of the magnetic force in a magnet sufficient that running for several years is an acceptable answer?"  
Regarding the device device shown in the question, which is a modification of John Wilkins' device similar to this one, the problems inherent in Wilkin's machine are compounded with the design shown here.  Gravity is the overwhelming force to overcome in this experiment, friction plays only a small part, but we also can't overlook the current induced in the movement of the ball, which does eventually become the principle force degrading the perpetuation of motion. Here however, if the magnet was strong enough to overcome the inertia and the gravitational force of the weight on an incline, it would have sufficient energy to attach to the magnet at the top.   Even without friction, and let's say that we started the ball at the top to give it an initial kinetic force, when it reached the 2nd hole, the magnet would not only have to overcome gravity, but the overall motion away from the magnet at the top, as it passed through the 2nd hole it would behave more like a skier doing a ski jump and fly away from the machine. So the magnet would also have to over come this motion, and reverse the balls direction as it is now also moving away with a velocity equal to the kinetic energy from dropping the ball minus friction.
Wilkins overcomes this with an additional ramp at the bottom, which by converting the linear motion into angular motion which reverses the direction of the ball and conserves that kinetic motion to move the ball partway up the ramp.  Even so, this is not enough to overcome gravity, and friction, and the counter force of an induced current to enable it to pull the ball all the way up, while being weak enough to allow the ball to drop through the hole.  I believe, but have not tested or proven, that perhaps a reduction of gravity would make this machine feasible, like on the moon.  It appears also, that because the ball is made of metal that is attracted to the magnet, the forces in the ball can set up a induced current counter field, due to the material the ball is made of and its in the field. A reverse lenz effect is present that would also contribute a force the magnet would have to overcome. This would require a superfine balance between a completely attractive force, and the opposing forces. The machine looks very promising; but, on a very subtle level has counter forces from gravity, friction, and a reverse lenz effect which counter the kinetic energy built up in the balls motion which pretty much stop the action from perpetuating fairly quickly.
In the video I link, the original author of the video overcomes this effect through the pulsing of the strength of an electromagnet hidden in the base holding the permanent magnet.  By turning the electromagnet off at the right moment either manually or by inductive sensing, the ball drops.  So, I agree, it is very close to possible; but, it just isn't possible.  I have a sense that being able to tweak gravity might be enough to make the device function under the right gravitational force, but have a real concern that eddy currents in the metal ball would provide the eventual force that would counter any possibility of it working.
As a magnetic field problem, due to Maxwell's laws and Lenz' law, the problem is a lot more complex mathematically than it looks, we tend to overlook the ball's effect on the magnetic field as it would be moving and shifting the flux density which would induce a current which would affect the movement and flux density in opposition to the movement.
A: If I were designing the experiment


*

*I would make the inside surfaces of the ramp out of of mu metal to shield the ball once it falls in the hole, otherwise a strong magnet will be pulling it back up the lower incline.

*I would use an iron ball with a smooth glass coating to reduce friction

*I would use a glass upper ramp , again to reduce friction

*A judicious use of mu metal could shadow the magnetic field as the ball reaches the upper hole so as to be sure to drop.
It might work for some time if correctly designed. Suppose it does, certainly no energy can be extracted from the system .  Even if the friction were zero there would be loss of the magnetization of the original magnet over time: to pull the ball energy must be supplied to the iron ball's   magnetic domains and this over time will demagnetize the red ball. Of course all the other considerations of friction and  impact energy loss as it falls (maybe etc, just these two come to mind) will lead the ball to finally stop in the lower ramp. 
Here is a different kind of perpetual motion, where thermodynamics is more evident. 
A: The very reason for the ball to move up is the presence of the magnet. So, as long as the magnet is present in its place, it will not allow the ball to move in the downward direction. Conversely, if the magnet can allow the ball to go in the downward direction, it would be incapable of pulling it upwards. Therefore the cyclic motion is impossible.
Many have answered the question earlier correctly, but in parts. Perpetual motion of the first kind implies extraction of work from a body which keeps moving on its own, in a cycle . Since the body keeps on moving on its own, the energy we extract comes free with no input (leading to violation of the law of conservation of energy). For the sake of completeness - perpetual motion of the second kind says : when the input energy is heat, we cannot extract an equal quantity of energy in any other form as out put, using even an ideal device (no friction etc) that works in cycles. The design details of the device don't alter the conclusion.
Even in the diagram shown, the essentials are just the vertical component of the force due to the magnet and that due to gravity . Gravitational force can be assumed to be constant irrespective of the position of the ball on its motion in the cycle. 
We may add, in the absence of friction, simple harmonic motion rules, and, a pendulum oscillates for ever, an ideal spring loaded mass oscillates for ever and so on. The moment we try to extract work, the oscillations decrease in amplitude and finally cease, when the initially stored energy got extracted.
Consider the lowest point, A, on the cyclic path. Consider a point B to the left, and a point B' vertically below B on the curved path. Th ball will move from A to B only if the vertical component of the magnetic force is greater than the gravitational force 'mg'. Similarly, it will move from A  to B' if the vertical component of the magnetic force is greater than mg. The vertical component of the magnetic force at B and B' is greater than the vertical component of magnetic force at A. Consequently, if the ball moves from A to B it necessarily moves from A to B'. On the other hand, if the ball moves from B to A then, and only then, it would move from B' to A. 
Alternately, you can consider the highest point, H, on the cyclic path. If the vertical component of the magnetic force is greater than the gravitational force 'mg' at B, the ball will move from B to H, if not, the ball will move from H to B. So is the case with motion between B' and H. Consequently, if the ball moves from H to B it necessarily moves from H to B'. On the other hand, if the ball moves from B to H then it would necessarily move from B' to H. 
