This example is taken from Maple's Getting Started guide. It illustrates how gradients can be used. In this example we have a function describing a mountain and a skier wishes to find the steepest path down the mountain.
Problem Description
A skier has made her way to the top of a mountain. She wants to take the steepest path down, which she can find by performing the calculations outlined in this worksheet.
Suppose that the height at a point (x,y) of the hill is given by
, in thousands of feet.
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
So let's first define the function describing the mountain.
and lets take a look at it...
Now remember that the gradient is going to give us the maximum and minimuim values for our directional derivatives. So for any point we can find the minimum value of the directional derivative at that point. That would be the direction our skier would want to head in.
So to begin we need to determine the partial derivatives.


Now we need to find the top of the mountain. The top would be the (x,y) point at which the partial derivatives were equal to zero. So set the partials to zero and solve for x and y. Note that we use fsolve here because we want real values for the solution. Here we are solving the two equations, fx = 0 and fy = 0, for x and y over the specified range.
Next we assign the above values to x and y
Now we can't start exactly at the top of the mountain since the partials are both zero at that point, there is no (useful) minimum directional derivative. So we will pick a point slightly off the top.
So let's reset x and y
and then evaluate the function at the point (x1, y1)
Next lets look at at contour plot. You should be able to guess at a path from this - it would be where the level curves are closest together.
Now we can begin...
Next, construct and plot the path on the surface of the hill that the skier should take. The negative of the gradient (−∇f(x,y) or −grad(f(x,y)) in the Maple language), gives the x and y components of the direction of steepest descent. At each point (x,y,z) on the surface, the skier must travel in the direction of −∇f(x,y), while staying on the surface. Since −∇f(x,y) changes from point to point, you can break the process into steps, building an approximation of the path of steepest descent. If the step size is too large, the path may leave the surface of the hill. If the step size is too small, you derive no benefit from the increased number of calculations.
Assume that the skier is currently at the starting point (x1,y1,z1). Use a timestep of 0.1 and find 25 points along the path. Use the arrays point3d and route3d to store the values of the computed points and the direction taken, respectively. To simplify the calculation of the points and route, define vector representations of the expressions for the hill and the derivatives with respect to both x and y.
So first lets get vector representations of the expressions for evaluating the function and its partials.
![-6*P[1]/((1+P[1]^2+P[2]^2)^2*(1/4+1/2*(P[1]+1)^2+1/2*(P[2]+2)^2))-3*(P[1]+1)/((1+P[1]^2+P[2]^2)*(1/4+1/2*(P[1]+1)^2+1/2*(P[2]+2)^2)^2)](images/Gradients and Skiers_45.gif)
![-6*P[2]/((1+P[1]^2+P[2]^2)^2*(1/4+1/2*(P[1]+1)^2+1/2*(P[2]+2)^2))-3*(P[2]+2)/((1+P[1]^2+P[2]^2)*(1/4+1/2*(P[1]+1)^2+1/2*(P[2]+2)^2)^2)](images/Gradients and Skiers_48.gif)
Then we get arrays for storing the values at each timestep.
Now define the initialization - set our timestep and our first point as the slightly off top point we found above.
Note: The notation <x1,y1,z1> defines a Vector while P[i] accesses the ith element of the list P.
To start the for loop:
• At the prompt, enter the following and press SHIFT+ENTER.
for i from 1 to 24 do
Note: If you press ENTER, Maple returns the message: “Warning, premature end of input, use <Shift> + <Enter> to avoid this message”. The for statement is not complete. You must use SHIFT+ENTER to go to the next line.
The body of the for loop comprises the next commands. These commands find the skier’s position at the end of each time step.
To construct the 3-D normalized negative of the gradient vectors:
• On the next line, enter the following and press SHIFT+ENTER.
route3d[i] := LinearAlgebra[Normalize](eval(<-gx,-gy,0>, P=point3d[i]));
To find the next point in the skier’s path:
• On the next line, enter the following and press SHIFT+ENTER.
point3d[i+1] := eval(<P[1],P[2],g>,P=point3d[i]+timestep*route3d[i]);
To complete the for loop:
• On the next line, enter the following and press ENTER. Remember to end the line with a colon to suppress the output.
end do:
This command ends the for loop. After you press ENTER, the five commands in the loop body are repeated 24 times. At the end of each iteration, the value of i is increased by 1. That is, for the first iteration, the value of i is its initial value 1, for the second, 2, and so on. For the last iteration the value of i is 24. Maple exits at the end of the 24th iteration once i is set to 25 (since 25 is outside of the bounds of the loop).
![]()
![]()
![]()
![]()
![]()
![]()
To graph the path, you must convert the points representing the path of the skier, which are stored in the point3d array, to a list.
To convert the point3d array to a list:
• At the prompt, enter the following and press ENTER.
listpoints3d := [seq( convert( point3d[i], list ), i=1..25 )];
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
Next we can plot the hill and label the plot 'mountain'.
Then we plot the set of points on the path as a straight line and call it 'path3d'.
And the plot the starting point of the skier and assign it to 'skier'
Finally to view all three elements at once use the display command
Note: We will be using the colon ':' to end each line except the last. To Maple it means execute but don't display the results of this statement. (If you feel like experimenting, change the ':' into ';' to see what results...)
![]()
![]()