---
# System prepended metadata

title: Methods 2 Portfolio 2

---

# Methods 2 Portfolio 2

## 1. Square root function

Along the lines of section 6.4.2 (p. 247ff) in Gill’s book, write an _R_ function that calculates the square root of a given positive number. Your solution should contain:

```{r}
square_root <- function(x){
delta <- 0.0000000001
sqrtx <- x
sqrtx_prev <- x
while( abs(sqrtx^2-x) > delta){
  
  sqrtx <- 0.5 * ( sqrtx_prev + (x / sqrtx_prev) )
  
  sqrtx_prev <- sqrtx
}
return(sqrtx)
}

square_root(25) 
```


a) A quick introduction into what the function does and why it works
b) A discussion of the choices you made (e.g., starting point of the algorithm)
c) A range of examples
d) A discussion of what happens when the program is applied to negative numbers


## 2. Power series derivatives

The power series definitions ($:=$ means "is defined as") of the exponential, sine, and cosine functions are

\begin{align*}
    \exp(x) &:= \sum_{n=0}^\infty \frac{x^n}{n!},\\[8pt]  
    \sin(x) &:= \sum_{n=0}^\infty \frac{(-1)^nx^{2n+1}}{(2n+1)!}, \\[8pt]
    \cos(x) &:= \sum_{n=0}^\infty \frac{(-1)^nx^{2n}}{(2n)!}.
\end{align*}

Using these definitions, show that

\newcommand{\dx}{\:\mathrm{d}x}
\newcommand{\md}{\mathrm{d}}
\newcommand{\dfdx}{\frac{\md}{\dx}}
\begin{align*}
    \dfdx \exp(x) &= \exp(x), \\[8pt]
    \dfdx \sin(x) &= \cos(x), \\[8pt]
    \dfdx \cos(x) &= -\sin(x).
\end{align*}

You can either [use LaTeX](https://www.overleaf.com/learn/latex/Learn_LaTeX_in_30_minutes) or include photos of your (nicely) handwritten equations in the notebook. In any case, write down all intermediate steps.