The friction of the block with the ramp acts to oppose the force of gravity,
and it may fight gravity to a tie, but it never overpowers it! There are
actually two separate cases to consider.
(1) If the acceleration due to friction ( friction[ theta ]) is greater
than or equal to the acceleration due to gravity ( gravity[ theta ]), then
the block will not move at all. (It certainly won't move up the ramp!)
(2) If the acceleration due to friction is less than the acceleration due
to gravity, then the acceleration of the block will be
gravity[ theta ] - friction[ theta ]
Mathematica will let us treat these two cases separately, but not within the
framework of a simple function. Instead, we will have to use a more general way of
describing a function that takes the two cases into account. Here is a
Mathematica function that will calculate the distance moved by the block as a
function of time and angle.
distance[t_, theta_]:=0/; gravity[theta]<=friction[theta];
distance[t_, theta_]:=
N[1/2 * (gravity[theta]-friction[theta]) * t^2]
Now let's see what this procedure is all about, and how it differs from (and is
similar to) a regular function.
We have named the procedure ``distance'' just as we name functions and other
things in Mathematica---via assignment. We assigned two different outcomes to any invocation of the function, as specified by the /; operator. In the example above we told the function that the value is zero for
gravity[theta]<=friction[theta]
and otherwise to use the function as we first defined it. How far does the block move in ten seconds if the angle of the ramp is 0 degrees?
distance[10, 0]
How about how far it moves in ten seconds if the angle of the ramp is 90
degrees?
distance[10, Pi/2]
We can also plot the distance covered by the block in five seconds as theta
varies from 0 to 90 degrees (0 to Pi/2 radians).
Plot[distance[5, theta], {theta, 0, Pi/2}];
All of this experimentation should confirm that the block no longer moves
backwards for small angles. It also reveals an important property of programs.
It is perfectly feasible to use a procedure (or function) without knowing
anything about how the procedure (or function) works. You only need to
now what it does. This is called the principle of abstraction, and
is what makes it possible to write large programs.
Now let's look at the internal workings of the function. In a procedure, we
give step-by-step instructions, called statements, to Mathematica. In this one the
instructions are rather simple. We tell Mathematica to do a comparison, and then to
return one value or another based upon the outcome of the comparison.
When you write C and Fortran programs, you'll be writing procedures much like
``distance.'' The procedures that you write will consist of statements that
give step-by-step instructions. Among other kinds of statements, C and Fortran
have conditional statements much like the one you've just seen.
Although the details of Fortran and C are different, the surface appearance of programs will be similar.