---
# System prepended metadata

title: Methods 2 -- Portfolio Assignment 2

---

---
title: "Methods 2 -- Portfolio Assignment 2"
output:
  html_document:
    df_print: paged
  Date: 03/04/23
  pdf_document: default
  Author: Shaked Mofaz
---

- *Type:* Individual assignment
- *Due:* 10 April 2022, 23:59



# 1. Square root function

### A quick Introduction:

The function computes the square root of a positive number by using the Newton-Raphson method. For a number $n$, the method finds the root of $f(x) = x^2 - n$. Starting with an initial guess, the algorithm refines the guess by applying the formula $x_{1} = x_{0} - \frac{f(x_{0})}{f'(x_{0})}$, and repeats the process until the approximation converges to the square root.

### Choices I made:

At the starting point I will choose the initial guess (x0) to be half of the input number (n/2), because it is a reasonable first guess for most positive numbers.
The function stops repeating when the difference between the current and previous guesses, divided by the new guess, is less than a very small value (e.g., 1e-8).
Convergence criterion: I used a while loop in order to continuously update the guess until the relative error between the current and previous guesses is lower than the tolerance. The relative error is calculated as the absolute difference between the new and old guesses, divided by the new guess.






### Function


```{r, cache=FALSE}
sqrt_newton_raphson <- function(n, tol = 1e-8) {
  # Checking if input is a negative numbe
  if (n < 0) {
    stop("Input must be a positive number.")
  }
  
  # initial guess for the square root (half of the input)
  x <- n / 2
  
  # Keep updating the guess until the difference between guesses is small enough
  while (TRUE) {
    # Calculate a new guess using the Newton-Raphson formula
    x_new <- x - (x^2 - n) / (2 * x)
    
    # Checks if the difference between the new and old guess is smaller than tol
    if (abs(x_new - x) / x_new < tol) {
      # If the difference is small enough, stop updating and return the result
      break
    }
    
    # Update the guess to the new value for the next iteration
    x <- x_new
  }
  
  # Return the final guess for the square root
  x
}


```




### Examples
```{r}
sqrt_newton_raphson(4) # Should return approximately 2
sqrt_newton_raphson(9) # Should return approximately 3
sqrt_newton_raphson(16) # Should return approximately 4
sqrt_newton_raphson(2) # Should return approximately 1.414
#sqrt_newton_raphson(-5) - error

```

### Discussion - Negative numbers



If I would use the current function to find the square root of a negative number, it will give me an error. This is because the Newton-Raphson method I used doesn't work for negative numbers, which have complex square roots. To deal with negative numbers, I could use a different, more complex method or try R's built-in sqrt function, which can handle complex numbers.








## 2. Power series derivatives

*To demonstrate that the derivatives of the exponential, sine, and cosine functions are as stated, I'll differentiate each power series term by term*.

### Exponential function:

$$
\exp(x) := \sum_{n=0}^{\infty} \frac{x^n}{n!}
$$

Differentiating term by term:

$$\frac{d(\exp(x))}{dx} = \frac{d}{dx} \sum_{n=0}^{\infty} \frac{x^n}{n!} = \sum_{n=0}^{\infty} \frac{d(x^n / n!)}{dx}$$



 Applying the product rule to differentiate each term with respect to x, while treating $\frac{1}{n!}$ as a constant:

$$\sum_{n=0}^{\infty} \frac{d(x^n / n!)}{dx} = \sum_{n=0}^{\infty} \frac{d(x^n)}{dx} \cdot \frac{1}{n!}$$

 Differentiating $x^n$ with respect to x, which gives $nx^{n-1}$:

$$\sum_{n=0}^{\infty} \frac{d(x^n / n!)}{dx} = \sum_{n=0}^{\infty} \frac{nx^{n-1}}{n!}$$

Now, When n=0, the term is 0 because the derivative of a constant is 0. Therefor, I change the lower limit of the sum to start at n=1:

$$\sum_{n=0}^{\infty} \frac{nx^{n-1}}{n!} = \sum_{n=1}^{\infty} \frac{nx^{n-1}}{n!}$$

Canceling the n in the numerator and denominator:

$$\sum_{n=1}^{\infty} \frac{nx^{n-1}}{n!} = \sum_{n=1}^{\infty} \frac{x^{n-1}}{(n-1)!}$$

Now the result is equivalent to the original series for exp(x) by replacing n-1 with a new index, using k:

$$\sum_{n=1}^{\infty} \frac{x^{n-1}}{(n-1)!} = \sum_{k=0}^{\infty} \frac{x^k}{k!}$$

Thus,

$$\frac{d(\exp(x))}{dx} = \exp(x)$$

### Sine function:



Power series for the sine function is:
$$\sin(x) := \sum_{n=0}^{\infty} \frac{(-1)^n x^{2n+1}}{(2n+1)!}$$

Differentiating term by term:

$$\frac{d(\sin(x))}{dx} = \frac{d}{dx} \sum_{n=0}^{\infty} \frac{(-1)^n x^{2n+1}}{(2n+1)!} = \sum_{n=0}^{\infty} \frac{d((-1)^n x^{2n+1} / (2n+1)!)}{dx}$$


Applying the product rule to differentiate each term with respect to x, while treating $\frac{(-1)^n}{(2n+1)!}$ as a constant:

$$\sum_{n=0}^{\infty} \frac{d((-1)^n x^{2n+1} / (2n+1)!)}{dx} = \sum_{n=0}^{\infty} \frac{d(x^{2n+1})}{dx} \cdot \frac{(-1)^n}{(2n+1)!}$$

Using the power rule of differentiation to find the derivative of $x^{2n+1}$, which gives $(2n+1) x^{2n}$:

$$\sum_{n=0}^{\infty} \frac{d((-1)^n x^{2n+1} / (2n+1)!)}{dx} = \sum_{n=0}^{\infty} \frac{(-1)^n (2n+1) x^{2n}}{(2n+1)!}$$

Cancelling the (2n+1) in the numerator and denominator:

$$\sum_{n=0}^{\infty} \frac{(-1)^n (2n+1) x^{2n}}{(2n+1)!} = \sum_{n=0}^{\infty} \frac{(-1)^n x^{2n}}{(2n)!}$$

Now, the resulting series is the power series for the cosine function:

$$\frac{d(\sin(x))}{dx} = \sum_{n=0}^{\infty} \frac{(-1)^n x^{2n}}{(2n)!} = \cos(x)$$

Thus,

$$\frac{d(\sin(x))}{dx} = \cos(x)$$




### Cosine function:

$$\cos(x) := \sum_{n=0}^{\infty} \frac{(-1)^n x^{2n}}{(2n)!}$$

Differentiating term by term with respect to x:

$$\frac{d(\cos(x))}{dx} = \frac{d}{dx} \sum_{n=0}^{\infty} \frac{(-1)^n x^{2n}}{(2n)!} = \sum_{n=0}^{\infty} \frac{d((-1)^n x^{2n} / (2n)!)}{dx}$$

Applying the product rule to differentiate each term with respect to x, while treating $\frac{(-1)^n}{(2n)!}$ as a constant:

$$\sum_{n=0}^{\infty} \frac{d((-1)^n x^{2n} / (2n)!)}{dx} = \sum_{n=0}^{\infty} \frac{d(x^{2n})}{dx} \cdot \frac{(-1)^n}{(2n)!}$$

Using the power rule of differentiation to find the derivative of $x^{2n}$, which gives $2n x^{2n-1}$:

$$\sum_{n=0}^{\infty} \frac{d((-1)^n x^{2n} / (2n)!)}{dx} = \sum_{n=0}^{\infty} \frac{(-1)^n 2n x^{2n-1}}{(2n)!}$$

Since:

$$(2n)! = (2n) \cdot (2n-1) \cdot (2n-2) \cdot \ldots \cdot 3 \cdot 2 \cdot 1 = 2n \cdot (2n-1)!$$

I can simplify by canceling $2n$ from the numerator and denominator. 

$$\sum_{n=0}^{\infty} \frac{(-1)^n 2n x^{2n-1}}{(2n)!} = \sum_{n=0}^{\infty} \frac{(-1)^n x^{2n-1}}{(2n-1)!}$$

Excluding the term with n=0, because the derivative of a constant is zero, and start the sum from n=1:

$$\frac{d(\cos(x))}{dx} = \sum_{n=1}^{\infty} \frac{(-1)^n x^{2n-1}}{(2n-1)!}$$


Thus,

$$\frac{d(\cos(x))}{dx} = -\sin(x)$$





**In conclusion, the derivatives of the all the functions, using their power series definitions, are:**

$$\begin{align*}
\frac{d(\exp(x))}{dx} &= \exp(x),\
\frac{d(\sin(x))}{dx} &= \cos(x), \
\frac{d(\cos(x))}{dx} &= -\sin(x).
\end{align*}$$

























