# Assignment 2

**Opened:**Tuesday, 30 January 2024, 11:00 AM

**Due:**Tuesday, 6 February 2024, 11:59 PM

The purpose of this assignment is to practise developing your own functions in R. Before you begin, we recommend that you create a new directory to hold your assignment, e.g. assignment2, and save your file into that directory.

**0)** Create a file named `myFuncs.R`

and write a comment containing your name, your SciNet user name and a short description of what this script is doing. For example:

```
```# Name: Alexey Fedoseev
# SciNet username: alexey
# Description:
# script calculates various quantities regarding the quadratic equation
# script also displays the ingredients needed for the recipe "Petits Pains au Lait"

The functions we'd like you to develop for the first part of this assignment revolve around our old friend, the quadratic equation,

$$ax^2+bx+c=0$$

You will write a suite of functions which, given the coefficients `a`

, `b`

, and `c`

, will calculate various quantities regarding the equation.

**1a)** Write a function named `quadratic.determinant`

which takes three arguments that represent the three coefficients of a quadratic equation. The function will calculate and return the determinant of the equation, which is defined as

$$\Delta = b^2 - 4ac$$

**1b)** Write a function named `quadratic.roots`

which takes three arguments that represent the three coefficients of a quadratic equation. The function will calculate and return the roots of the equation, which are given by

$$\frac{-b\pm\sqrt\Delta}{2a}$$

The roots should be returned in an un-named list.

**1c)** Write a function named `quadratic.eqn`

which will receive three arguments that represent the three coefficients of a quadratic equation. If the arguments are not specified, the function will assume the following values: \( a = 1 \), \( b = c = 0 \). The function should return a named list which contains the following elements:

- a
*list*containing the coefficients of the equation, - the determinant of the function, \( \Delta \),
- a
*list*containing the two roots of the equation, - a
*list*containing the coefficients of the first derivative of the equation, given by`2a`

and`b`

, the second derivative of the equation, given by`2a`

.

Notice the function will returns a "nested" list, i.e. a list which contains other lists.

- For this particular assignment, please ignore the possibility of cases where
`a = 0`

or the determinant is negative. These cases require special consideration in order to be dealt with properly so that they don't yield mathematical problems. - However, be sure that you obtain the correct values, i.e. check with known cases where, for instance, you can easily compute the roots of the quadratic equation, e.g. \( x^2-x=0 \) or \( x^2-4=0 \).
- Useful functions:
`sqrt()`

and the`^`

or`**`

operators for taking the power of a number, e.g.`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 <- quadratic.eqn()
>
> 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 second function named `petits.pains.au.lait`

which will print and return a data frame containing 3 columns: Ingredients, Amount and Units. These will be in reference to the ingredients needed for the recipe "Petits Pains au Lait", the amounts of which will be based on an argument which will specify the number pieces to obtain. The main ingredients of the recipe are:

- 4 cups of flour
- 1 cup of milk
- 15 grams of yeast
- 2 tbsp of salt
- 2 tbsp of sugar

The above amounts will generate 12 pieces of the delicious French "Petits Pains au Lait"!

Based on the proportions of these ingredients your function should calculate the amounts of the ingredients for a given number of pieces indicated through the argument of the function, which will take the default value of 12 if it is not indicated.

The function should return the data frame containing the ingredients. In order to display a data frame use the `print`

command.

```
```>
> source("myFuncs.R")
>
> twelve.pieces <- petits.pains.au.lait()
Ingredients Amounts Units
1 Flour 4 Cups
2 Milk 1 Cups
3 Yeast 15 Grams
4 Salt 2 Tbsp
5 Sugar 2 Tbsp
>
> print(twelve.pieces)
Ingredients Amounts Units
1 Flour 4 Cups
2 Milk 1 Cups
3 Yeast 15 Grams
4 Salt 2 Tbsp
5 Sugar 2 Tbsp
>
> three.pieces <- petits.pains.au.lait(3)
Ingredients Amounts Units
1 Flour 1 Cups
2 Milk 0.25 Cups
3 Yeast 3.75 Grams
4 Salt 0.5 Tbsp
5 Sugar 0.5 Tbsp

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 6, 2024 (midnight), with 0.5 point penalty per day for late submission until the cut-off date of February 13, 2024, at 10:00am.**