# Assignment 1

**Opened:**Wednesday, 15 February 2023, 9:30 AM

**Due:**Wednesday, 22 February 2023, 11:59 PM

**1)** Create an R file named `myFuncs.R`, which contains definitions for the following two functions.

Write a function named `quadraticEqn`

which will receive three arguments that represent the three coefficients for a quadratic equation of the form, \(a x^2 + b x + c = 0\). If the arguments are not specified, the function will assume the following values: \(a=1\), \(b=c=0\). The function should compute and return in a list the following elements:

- a list containing the coefficients of the equation,
- the determinant of the function, defined as \(\Delta = b^2 - 4ac\).
- a list containing the two roots, given by \(\frac{-b \pm \sqrt{\Delta}}{2a}\).
- a list containing the coefficients first derivative, given by \(2a\) and \(b\), and
- the second derivative (aka concavity) given by \(2a\).

Notice the function will return a "nested" list, i.e. a list containing other elements of which a few of them happen also to be lists. For this particular assignment, disregard the cases where \(a=0\) or the determinant is negative. This case requires special consideration in order to be dealt with properly so that it doesn't yield mathematical problems. However, be sure that you obtain the correct values check with known cases where, for instance, you can easily compute the roots of the quadratic equation, eg. \(x^2-x=0\) or \(x^2-4=0\). Useful functions: `sqrt()`

and the `^`

or `**`

operators for taking the power of a number, eg. \(2^3 = 2**3 =8\). This function shouldn't explicitly print anything to the screen but instead return a list with the elements specified above.

```
>
> source("myFuncs.R")
> quadEqn <- quadraticEqn()
> print(quadEqn)
$eqn
$eqn$a
[1] 1
$eqn$b
[1] 0
$eqn$c
[1] 0
$determinant
[1] 0
$roots
$roots[[1]]
[1] 0
$roots[[2]]
[1] 0
$first.deriv
$first.deriv[[1]]
[1] 2
$first.deriv[[2]]
[1] 0
$second.deriv
[1] 2
>
```

**2)** Write a function, called `harmonicMean`

, which takes 1 argument, `x`

, a vector of numeric values. The function should calculate and return the "harmonic mean" of `x`

.

The harmonic mean determines the typical value of a set of numbers by calculating the reciprocal of the average reciprocal value of the vector of values. Thus for `n`

numbers, i.e. for a set of numbers `x1, x2, ..., xn`

, the harmonic mean is defined as

$$\left(\frac1n \sum_{i=1}^n \frac1{x_i}\right)^{-1} = \frac1{\frac1n(\frac1{x_1} + \frac1{x_2} + \ldots + \frac1{x_n})}$$

There are some native R functions you may find useful: `sum()`

, and `length()`

; use `help()`

to understand how to use them. You should not need to use loops in your function.

Your function should behave as follows:

```
```>
> source("myFuncs.R")
>
> harmonicMean(1:10)
[1] 3.414172
>
> harmonicMean(c(-2, 0.5, 7, 4, 4, -2))
[1] 3.652174
>

Be sure to comment your code, indent your code blocks, and use meaningful variable names. You should not need to use loops in this assignment.

Submit your script `myFuncs.R`

.

**Assignments will be graded on 10 points basis.**

**Due date is February 22, 2023 (midnight), with 0.5 point penalty per day for late submission until the cut-off date of March 1, 2023, at 9:00am.**