Chap 3 Exercises

\[ \newcommand{\dnorm}{\text{dnorm}} \newcommand{\pnorm}{\text{pnorm}} \newcommand{\recip}{\text{recip}} \]

Exercise 1  

  1. Input name in \(g(t) \equiv 2 t^2 + 8\)?
t       u       v       w       x       y       z      

question id: clc2-1

  1. Input name in \(\text{straight\_line}(z) \equiv a z + b\)?
t       u       v       w       x       y       z      

question id: clc2-2

  1. Input name in \(h(t) \equiv 2 t^2 + 8 w\)?
t       u       v       w       x       y       z      

question id: clc2-3

  1. Input name in \(f(u) \equiv a u + b + u^2\)?
t       u       v       w       x       y       z      

question id: clc2-4

5Input name in \(g(w) \equiv x + 4\)?

t       u       v       w       x       y       z      

question id: clc2-5

Exercise 2 In this activity, you will start getting familiar with the structure of R computer commands. Particularly when mathematical formulas are involved, commands are similar across many computer languages, so what you learn will be applicable to any other languages such as Python or MATLAB.

The first high-level computer language was FORTRAN, released in 1957. The name stands for “formula translation.” FORTRAN enabled arithmetic operations to be written in a way very nearly that of traditional notation. Until then, computer programs were written using alpha-numeric codes that were meaningless to a casual reader. We unfortunately still use the term “computer code” to refer to programs, despite 70-years progress in improving legibility.

The best way to learn to implement mathematical formulas in a computer language is to read examples and practice writing them.

Here are some examples:

Traditional notation R notation
\(3 + 2\) 3 + 2
\(3 \div 2\) 3 / 2
\(6 \times 4\) 6 * 4
\(\sqrt{\strut4}\) sqrt(4)
\(\ln 5\) log(5)
\(2 \pi\) 2 * pi
\(\frac{1}{2} 17\) (1 / 2) * 17
\(17 - 5 \div 2\) 17 - 5 / 2
\(\frac{17 - 5}{\strut 2}\) (17 - 5) / 2
\(3^2\) 3^2
\(e^{-2}\) exp(-2)

Each of these examples has been written using numbers as inputs to the mathematical operations. The syntax will be the same when using an input name such as x or y or altitude, for instance (x - y) / 2. In order for that command using x and y to work, some meaning must have been previously attached to the symbols. We will come back to this important topic on another day.

Read through the above table of examples several times. Note down any that you find at all confusing. Then cover up the right side of the table and write down the R expression for each item on the left side. Similarly, cover up the left side of the table and translate the R expression into traditional notation.

Use Active R chunk lst-chicken-chew-screen-1 to write and run the R expression for each of these traditional-notation expressions. We give the numerical result for each of the traditional expressions to let you confirm that your R version is correct.

  1. \((16 - 3)/2\) gives result 6.5
  2. \(\sqrt{\frac{19}{3}}\) gives 2.5166115
  3. \(\cos(\frac{2 \pi}{3})\) gives -0.5
  4. \(\pi^3 + 2\) gives 33.0062767
  5. \(\pi^{3+2}\) gives 306.0196848
Active R chunk 1

Once you have a grasp of how to render traditional-notation formulas into R, read these few principles to help consolidate your understanding.

  1. Computer languages generally, and R in particular, involve expressions that are written in a typewriter format: one conventional character after another in a straight line. There are no superscripts or subscripts used. So, \(\frac{1}{3}\) will be written 1/3. The equivalent of \(\sqrt{7}\) is sqrt(7).
  2. Parentheses:
    1. When an opening parenthesis directly follows a name, as in sqrt(, it means that the named operation is to be applied to the quantity inside the parentheses. So sqrt(7) means “take the square root of 7” and log(10) means “take the (natural) logarithm of 10.” The common mathematical functions typically take only one input. We will stick with those for now.
    2. When an opening parenthesis does not directly follow a name, as in 6 - (2+3), the parentheses stand for grouping in the same way as in traditional notation. For instance, \((6 + 15) / 3\) is translated as (6 + 15) / 3.
  3. Multiplication and exponentiation are often written traditionally in terms of the relative position of quantities. So \((3 + 2)\pi\) means to multiply 5 times \(\pi\), even though there is no multiplication sign being written. Similar, \(\pi^{3 + 2}\) means to raise \(\pi\) to the fifth power. There is no “exponentiation sign” used, just the positioning of \(3+2\) as a superscript: \(^{3+2}\). In R (and almost every computer language) multiplication and exponentiation must be written explicitly. The quantities \((3 + 2)\pi\) and \(\pi^{3 + 2}\) would be translated as (3 + 2) * pi and pi^(3+2).

One additional note: All these examples can be written with a single line of code, as is true for many short formulas. Later on, we will encounter longer formulas that could be written on a single line but are more readable when spread over two or more consecutive lines.

:::

Exercise 3 Use the MOSAIC Calculus naming conventions to answer these questions.

  1. What is \(h()\)?

The name of a function

The name of an input.

A specific numerical value

question id: cdrill-1

  1. How come we write \(f()\) for the name of a function rather than just \(f\) or \(f(x)\)?

No good reason

It is a reminder that we are talking about a function with the name “\(f\)”.

The parentheses are part of the name.

question id: cdrill-2

  1. What sort of thing is denoted by \(x_0\) or \(y_\star\) or \(y_{max}\)?

A particular numerical value

The name of an input

question id: cdrill-3

  1. Which of these symbols might stand for the entire domain of a function?

\(y\)

\(f()\)

\(y_0\)

question id: cdrill-4

  1. Suppose you come across \(v(w) \equiv w + 3\) in this book. What do \(v\) and \(w\) stand for?

\(v()\) is the name of a function and \(w\) is the name of the input to that function.

It is meaningless.

It is the same thing as \(v = w + 3\).

question id: cdrill-5

  1. Are \(g(x) \equiv x^2\) and \(h(w) \equiv w^2\) the same function?

Yes, although that function is being given two different names.

Of course not!

question id: cdrill-6

Exercise 4 When your R command is not a complete sentence, the R output will contain an error like this:

\[\color{red}{\mathtt{Error\ in\ parse(text = x, keep.source = TRUE) : <text>:5:0:\\ unexpected\ end\ of\ input}}\]

The “unexpected end of input” is the computer’s way of saying, “You haven’t finished your sentence so I don’t know what to do.”

Each of the following R expressions is incomplete. Your job, which you should do in one line after another in @dolphin-toss-candy, is to turn each into a complete expression that produces a numerical result. Sometimes you will have to be creative, since when a sentence is incomplete you, like the computer, don’t really know what it means to say! But each of these erroneous expressions can be fixed by adding or changing text. Note: In the end, your R chunk will have seven commands, each on its own line.

  1. sin 3
  2. ((16 - 4) + (14 + 2) / sqrt(7)
  3. pnorm(3; mean=2, sd=4)
  4. log[7]
  5. 14(3 + 7)
  6. e^2
  7. 3 + 4 x + 2 x^2
Active R chunk 2

Exercise 5  

library(devoirs)

Here is an R chunk that evaluates a function at an input \(x\):

Active R chunk 3

In the following questions, numbers have been rounded to two or three significant digits. Select the answer closest to the computer output.

  1. Change \(x\) to 1 in Active R chunk lst-fish-sees-tree. What’s the output of \(\sin(x) \ \sqrt{\strut x }\)

-1.51

0.244

0.84

0.99

2.14

NaN 

question id: fst9-1

  1. Change \(x\) to 3. What’s the output of \(\sin(x) \ \sqrt{\strut x }\)
-1.51       0.244       0.84       0.99       2.14       NaN       

question id: fst9-2

  1. Change \(x\) to \(-5\). What’s the output of \(\sin(x) \ \sqrt{\strut x }\)
-1.51       0.244       0.84       0.99       2.14       NaN      

question id: fst9-3

  1. Change the function formula from \(\sin(x) \sqrt{x}\) to be \(\sqrt{\strut\pnorm(x) }\). For \(x=2\), what’s the output of from the new formula?
-1.51       0.244       0.84       0.99       2.14       NaN      

question id: fst9-4

Exercise 6 Using the interactive R chunk, translate all of the following mathematical expressions into R to calculate the numerical value of the expression.

  1. \((16 - 3)/2\)
  2. \(\sqrt{\frac{19}{3}}\)
  3. \(\cos(\frac{2 \pi}{3})\)
  4. \(\pi^3 + 2\)
  5. \(\pi^{3+2}\)
Active R chunk 4

Exercise 7 Consider this expression in math notation:

\[\frac{e^{k t}}{k}\]

Which of the following R expressions implements the math-notation expression?

k exp(kt)

e^k*t / k

exp(k t) / k

exp(k*t) / k

1/k e^kt

question id: horse-sing-drawer-01

Exercise 8 Suppose you want to define a straight-line function named \(f()\) such that \(f(x)\equiv m x + b\). Each of the following R statements is incorrect for this purpose. Say why.

  1. What’s wrong with f <- m*x + b

Need to use makeFun() to define a function.

m is not defined.

b is not defined.

Should be y <- m*x + b.

question id: plmy7-1

  1. What’s wrong with f <- makeFun(m*x + b)

The first argument to makeFun() should be a tilde expression.

m is not defined.

b is not defined.

makeFun() requires two inputs.

question id: plmy7-2

  1. What’s wrong with f <- makeFun(x ~ m*x + b)

The tilde expression should have the input name on the right-hand side of the ~.

m is not defined.

b is not defined.

The first argument is not a tilde expression.

question id: plmy7-3

  1. What’s wrong with f <- makeFun(mx + b ~ x)

The tilde expression is missing the multiplication operator * between m and x

m is not defined.

b is not defined.

The name f is mis-spelled.

question id: plmy7-4

  1. What’s wrong with f <- makeFun(b*x + m ~ x)

The roles of m and b have been reversed.

m is not defined.

b is not defined.

x is not defined.

question id: plmy7-5

Exercise 9 What is wrong with this function definition?

dorothy(kansas) \(\equiv\) niece_of_(AuntieEm) \(\times\) kansas

You cannot name a function dorothy.

“kansas” is a US State, not the name of an argument.

“dorothy” and “kansas” should be capitalized.

Auntie Em is a movie character, not a number.

Nothing.

question id: dorothy-kansas

Exercise 10 Something is wrong with this function definition. What is it?

electric(z) \(\equiv\) ln()

No input has been given to ln().

electric() should have been written as E().

Nothing

question id: bad-fun-def

No answers yet collected