Go to content ALT+c

Intro to Programming with R  (Winter 2021) (Old site; new site is at https://scinet.courses)

Friday May 24, 2024 - 07:39

5.1 Assignment 1: Functions

Due date: Monday, March 15th at 11:55 pm

Please note that all of the commands and techniques you need to solve this assignment were given in class.  No internet searches should be necessary to complete this assignment.  If you aren't sure where to start, review the class slides.


The purpose of this assignment is to practise developing your own functions in R. Before you begin, I'd recommend that you create a new directory to hold your assignment, eg. assignment1 and save your files into that directory:


0) Start by creating a plain text file (ie no word processing!) named "myFuncs_assign1.R", which will contain the definitions for the following two functions.

2) 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 of the first derivative, given by 2a and b
  • second derivative (aka concavity) given by 2a

Notice also that in this case the function will return a "nested" list, ie a list containing other elements 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 will require special consideration in order to deal with them properly so that they don't yield mathematical problems.
  • However, be sure that you obtain the correct values, ie. 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. Eg.
    quadEqn <- quadraticEqn()

    > print(
    quadEqn)
    $eqn
    $eqn$a
    [11

    $eqn$b
    [10

    $eqn$c
    [10


    $determinant
    [10

    $roots
    $roots
    [[1]]
    [
    10

    $roots
    [[2]]
    [
    10


    $first
    .deriv
    $first
    .deriv[[1]]
    [
    12

    $first
    .deriv[[2]]
    [
    10


    $second
    .deriv
    [12

    2) Write a second function named "blueberryMuffins" which will return the list components needed for the recipe of "Blueberry Muffins" based on an argument which will specify the number pieces to obtain.
    The ingredients for the blueberry muffins recipe are:

    • 1 and 1/2 cups of flour
    • 3/4 cups of sugar
    • 1/3 cup of milk
    • 1 cup of blueberries
    • 2 tbsp of baking powder
    • 1/4 tea-spoon of salt
    • 1 egg
    • 2 tea-spoons of vanilla
    this will generate 12 pieces of delicious "blueberry muffins"!
    Based on the proportions of this ingredients your function should calculate 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 print in screen and return the list of ingredients. For instance,
    twelve.pieces <- blueberryMuffins()

    Recipe for 'Blueberry Muffins', for:  12 pieces
         Flour
    :  1.5  cups
         Milk
    :  0.3333333 cups
         Baking powder
    :  2 grams
         Salt
    :  0.25  tbsp
         Sugar
    :  0.75 tbsp
         Blueberries
    :  1 cups
         Eggs
    :  1 eggs
         Vanilla
    :  2 tbsp
    Ingredients computed based on  12  pieces

    str(twelve.pieces)
    List 
    of 8
     
    flour.cups     num 1.5
     
    milk.cups      num 0.333
     
    bakingPwdr.tbspnum 2
     
    salt.tbsp      num 0.25
     
    sugar.cups     num 0.75
     
    eggs           num 1
     
    blueberries    num 1
     
    vanilla.tbsp   num 2

    3) Testing your functions: create another plain-text file named "tests_assign1.R" in the same directory as your myFuncs_assign1.R file, which will:

    1. source your "myFuncs_assign1.R" file
    2. execute each of the following examples

    # default case
    quadraticEqn()

    # x^2 + 4x + 2 
    quadraticEqn(a=1,b=4,c=2)

    # 3x+1
    quadraticEqn(a=0,b=3,c=1)

    # default case
    blueberryMuffins()

    # 6 pieces
    blueberryMuffins(6)

    # 20 pieces
    blueberryMuffins(20)

    Be sure to comment your code, document your functions, indent your code blocks, and use meaningful variable names.

    Submit your files "myFuncs_assign1.R" and "tests_assign1.R" to the 'Assignment Dropbox'.

    Assignments will be graded on a 10 point basis. Due date is March 15th 2021 (midnight).
    Late submissions will not be accepted.

Last Modified: Friday Feb 26, 2021 - 13:37. Revision: 3. Release Date: Monday Mar 1, 2021 - 12:00.


Content Navigation


Course Calendar


Forum Posts


Related



Questions? Contact Support.
Web site engine's code is copyright © ATutor®.
Modifications and code of added modules are copyright of SciNet.