## Assignment 3: Steepest Descent Approximation

Aufgabe 1 10 Punkte

Click on the GitHub Classroom invitation URL on moodle and accept the assignment. A repository under your GitHub name will automatically be created. For example, if your GitHub name is janedoe, your repository will be named assignment3-janedoe. Clone the repository to your local machine. Create a rmarkdown document in your repository folder with the title AS3 and output format pdf. Save your file with the name AS3.Rmd in your local GitHub repository. Sign this pdf on page 1 and put it in your repository in pdf, png or jpg format. When you are done with this assignment, push your solution file AS3.Rmd and the signed pdf to the remote GitHub repository within the assignment deadline. Check the remote repository to see if it worked. If the solution file and the pdf show up in your remote GitHub repository, you are done with this assignment. It is also advisable to push your solution file after each subtask or after each day you work on it. That way, you get used to the GitHub workflow and you can make sure everything works. (1 Bonuspoint)

(a) You are on a hill with your friends hiking. You want to reach the closest valley with a pub as soon as possible as it is getting dark. The fastest way to get down is to examine the ground and walk in the direction of the steepest descent. You brought a fancy new robot on your hike that is able to examine the slope to find the steepest descent and you want to try it out. The robot is able to evaluate the derivative of f(x), here denoted by df(x) when prompted, but is not able to do anything else. The hill you are on is almost exactly approximated by this function f(x)=0.5x3 + x2 3x + 10 (1) Add a code chunk to your rmarkdown file and define the function f(x) in R. Calculate the analytical derivative df(x) using pen and paper and define it in R as well.

(b) Now that you have defined the above functions f(x) and df(x) using R Code, include them as an equation in your rendered Rmd file as well. They should look like equation 1 for f(x) in subtask (a) (Hint: See for example here how to include math in Rmarkdown files).

(c) Replicate the plot shown below of the function from subtask (a). Note: Here the plot is created using base R plot functions, but you can use ggplot2 if you want. The red dot is the location of your friends, the robot and you in (2.5, f(2.5)), the red cross marks the closest pub at the lowest value of f(x). Replicate the plot as exactly as possible. It does not matter if you do not have the exact color of green or if the font size of the axis labels is not the same, however it should replicate the main features of the plot.

(d) You use your robot to find the direction and magnitude of the steepest descent and go down 10 steps in that direction starting from the location (2.5, f(2.5)). The step size is 1 meter and it is multiplied by the value of df(x) in each step. Keep in mind that the scale of the plot is in kilometers. Using your functions from subtask (a), write a for loop that calculates your 10 steps and report your location after those steps. Recreate your plot from subtask (c) and add the current location to the plot. If your implementation is correct, you should be in location (2.4662, f(2.4662) = 16.183) after 3 steps. Use this information to sanity-check your code.

(e) You decide to increase the step size to 250 meters. That means you use your robot again to find the direction of the steepest descent and walk in that direction for 250 meters multiplied by the value of df(x) without checking the surroundings and without consulting the robot. What is your location after 1 step (250 meters) starting from your original starting position in (2.5, f(2.5))? What happened and why is it a problem for you and your friends?

(f) Using all the information above, write a function downhill that repeatedly takes steps in the direction of the closest valley starting in x start using a step size of 100 meters. Your function stops when the slope at that point is close to 0 with a tolerance of 0.01. Use the function signature below and - as always - write a documentation of your function. Your function should return a list with the following information: the final location x, the value of f in the final location f(x), the value of df in the final location df(x) and the number of steps i that were needed to arrive there. Test your function with x start = 2.5 and x start = 1. How many steps does it need to arrive at the pub?

```
downhill <- function(f, df, x_start, stepsize, tol = 0.01){
# to do
}
```

(g) BONUSPOINT QUESTION (1 Point): What happens when you insert x start = 3 into your function from subtask (f) and why? What would be your suggestion for fixing the problem in your function? It is important to not include the code for this in your rmarkdown document. If you know the answer, you know why. Answer this Bonuspoint question with text only.