Class 4

Between Class Sessions - Prep for Day 4

Please spend around 2 hours working between class sessions, focusing on the tasks below. Use any extra time to complete KnewtonAlta assignments and/or work on Project tasks.

Pick something from your prep today that you can share with your group in class. It might be something new that you learned. It might be questions you have that are still unanswered, or a question along with what helped you eventually answer it. It might be something tricky that you solved. It might be a review topic that helped you remember something. It might be a conversation you had with AI that was helpful. You will have a chance to share this with your peers during class. Come ready to articulate your thinking and questions.

Preparation

(1) Interactive Programming Activity

seq() function in R
Run the following code in your Console.

seq(-10,10,0.2)

What does this code do?

What if we change the last number?

seq(-10,10,1)
seq(-10,10,5)
seq(-10,10)

What if we change the first number?

seq(-10,10,2)
seq(-1,10,2)
seq(3,10,2)
seq(-14,10,2)

What if we change the second number?

seq(-10,10,2)
seq(-10,-2,2)
seq(-10,5,2)

Run the following in your Console?

?seq

Plotting in R
Run the following code in your Console.

x <- seq(-2,2,length=5)
y <- c(4,1,0,1,4)
plot(x,y)

What does this code do?

What does the argument, type=‘l’, do in the plot function?

plot(x,y,type='l')

What does the argument, type=‘p’, do in the plot function?

plot(x,y,type='p')

Run the following code in your Console.

plot(iris$Sepal.Length,iris$Petal.Length,pch=16)

What does this code do?

What does the argument, “pch=16”, do?

Run the following code in your Console.

head(iris)
head(iris$Petal.Width)
head(iris$Sepal.Length)

What does the $ do?

(2) Transformation of Functions (Videos)

If you prefer reading you can carefully read and review Section 1.5 Precalculus Text. You can skip the “Determining Even and Odd Functions” part of this section.

(3) Project 1 Task 1

Regular Reminders

Skill Practice (KA Homework)

  • Begin 1 – Transformations of Functions assignment
  • Begin 1 – Solving Linear Systems assignment

Applied Practice (Project Work)

  • Begin Project 1 Task 1

During Class

Brain Gains

  • In R, plot the function \(f(x) = 3x\) for \(-4\leq x\leq 7\).
Solution

There are many ways to do this. Here is one.

f <- function(x){3*x}
x <- seq(-4,7,0.1)
plot(x,f(x), type = "l")
  • In R, plot the function \(g(x) = -2\) for \(-5\leq x\leq 5\).
Solution

Again there are many ways to do this. The following option, which tries to mimic the previous, will fail.

f <- function(x){-2}
x <- seq(-5,5,0.1)
plot(x,f(x), type = "l")

# Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ

Why did it fail? Because the output is \(-2\) regardless of how many elements are passed into the function with the vector \(x\). The output is a single number, not \(-2\) for each input. This is what the error message “‘x’ and ‘y’ lengths differ” means.

One way to fix the problem above is to multiply \(x\) by 0, preserving the number of elements in \(x\), and then subtracting 2.

f <- function(x){0*x -2}
x <- seq(-5,5,0.1)
plot(x,f(x), type = "l")

We can actually complete both of the above exercises with a single function, where we use \(m\) and \(b\) as parameters for the slope and intercept of a line, and then pass these parameters into the function.

f <- function(x, m, b){m*x + b}
x <- seq(-4,7,0.1)
plot(x,f(x,3,0), type = "l")
x <- seq(-5,5,0.1)
plot(x,f(x,0,-2), type = "l")

Discussion - Create and render a Quarto document

  • Click the New File icon OR Click File -> New File -> Quarto Document
  • Render the document, so verify you can get HTML output.
    • Typing Math
      • Not required in Math 119 (You can insert an image with handwritten math.)
      • But you can copy Latex from our Wiki or Project Instructions.

Practice creating HTML with Quarto - Given \(h(x) = \sqrt{3-x}\), let’s compute \(h(-4)\) and \(h(5)\) inside an R coding chunk. - Now let’s write a cohesive analysis to explain what we’re doing. - We’ll then render our Quarto document.

Group Meeting

Start by giving each person a moment to share what they chose to prepare for class. Help each other address any questions. When each person has had a chance to share, move on the other activities.

Interactive Programming Activity

The prep for today included completing an interactive programming activity (see above). As a group, discuss each question below.

  • What does the seq() command do?
  • How does the plot() command work?
  • What does c() command do?
  • What does the <- symbol do?
  • How was the $ symbol used?
  • What does the head() command do?

Activity - R Practice

Help each other use R (in Positron) to calculate each value, by first defining a function, and then evaluating the function at various inputs. Practice putting your work in a Quarto document and writing a cohesive analysis to introduce each computation in a code chunk.

  • Let \(w(v) = v^2 - 5v -6\) Compute the following values.
    • \(w(-1)\)
    • \(w(-3)\)
    • \(w(6)\)
    • \(w(4)\)
Answers
w <- function(x){
x^2 - 5*x - 6
}

w(-1)
w(-3)
w(6)
w(4)

# Or all at once.
x <- c(-1,-3,6,4)
w(x)
  • Let \(f(x) = \frac{2x+4}{x^2}\). Find each of the following values.
    • \(f(-4)\)
    • \(f(1)\)
    • \(f(0)\)
Answers
f <- function(x){
(2*x + 4)/(x^2)
}

f(-4)
f(1)
f(0)

# Or all at once.
x <- c(-4,1,0)
f(x)

Note: Make sure you know how to interpret the output from R. The value \(f(0)\) is undefined. Infinity, \(\infty\), is not a number but a concept.

  • Let \(f(x) = 8x^2 - 15\). Find each of the following values.
    • \(f(-2)\)
    • \(f(1)\)
Answers

Compute \(f(-2)\)

8*(-2)^2 - 15

Compute \(f(1)\)

8*(1)^2 - 15
  • Consider the piecewise function \[g(x) = \begin{cases} x^2 - 6 & \quad x < 0 \\ \\ 10 - x & \quad x \geq 0. \end{cases}\]

Find the following values. - \(g(7)\) - \(g(-7)\) - \(g(-33)\) - \(g(0)\)

Answers
g <- function(x){ ifelse(x < 0, x^2-6, 10-x) }

g(7)
g(-7)
g(-33)
g(0)

x <- c(7,-7,-33,0)
g(x)
  • Consider the piecewise function \[f(x) = \begin{cases} x^3 & \quad x < -1 \\ \\ -2 & \quad -1 < x < 4 \\ \\ \sqrt{x} & \quad x \geq 4. \end{cases}\] Find the following values.
    • \(f(-2)\)
    • \(f(-0.5)\)
    • \(f(3)\)
    • \(f(0)\)
    • \(f(5.2)\)
    • \(f(-1)\)
    • \(f(4)\)
Answers
f <- function(x){
  ifelse(x < -1, x^3, ifelse(
         x > -1 & x < 4, -2, ifelse(
         x >= 4, sqrt(x),
         NA)))
}

f(-2)
f(-1)
f(0)

x <- c(-2,-0.5,3,0,5.2,-1,4)
f(x)

Discussion

Prepare for our Project work

Project 1

We’ll have a brief discussion about the project work, and start looking at the example project.


Source: Class.4 on byuimath.com