Functions

math
Published

January 23, 2023

We know what a quadratic equation is:

$ax^2 + bx + c = 0$

But what if we could actually see the answers, plotted out in a graph?

## Some equations

Before we can plot anything, we need an equation.

We will be plotting 4 equations. Here they are:

• $$x^2 - 11x + 28$$
• $$x^2 - x + 46$$
• $$x^2 + 49$$
• $$x^2 -4x$$

## Making Functions

We need to make a function first. We will use variables f, g, h, and k for our functions. Here’s how you make a function:

Code
f <- function(x) {
x^2 - 11*x + 28
}

### What’s going on here?

Let’s break down each part of the function. The function(x) part is the number we will plug in, it is the same as writing $$f(x)$$. Now, what does $$f(x)$$ equal? That is the part that goes in the curly brackets. We also want to save it to an object f.

This:

Code
f <- function(x) {
x^2 - 11*x + 28
}

Is the same thing as this:

$f(x) = x^2 - 11x + 28$

## All of the other functions

Here’s what the rest of the functions are written in code:

Code
g <- function(x) {
x^2 - x + 46
}

h <- function(x) {
x^2 + 49
}

k <- function(x) {
x^2 -4*x
}

And here they are not in code; $$g(x) = x^2 - x + 46$$, $$h(x) = x^2 + 49$$, $$k(x) = x^2 - 4x$$

## Playing around with numbers

Now that we have all of our functions written out, we can plug any number in for $$x$$

Code
f(1)
 18
Code
k(3)
 -3
Code
g(-7)
 102
Code
h(13)
 218

## Plotting

Now that we have a couple values for x, we can plot.

First, we need to load in the tidyverse package.

Code
library(tidyverse)

This will be made with ggplot, just with nothing in the parentheses. We want to use geom_function so we can see our function mapped out, and we want function f.

Code
ggplot() +
geom_function(fun = f)

The next layer we want to add will be the limits of the Cartesian plane.

Code
ggplot() +
geom_function(fun = f) +
xlim(-10, 10) +
ylim(-10, 10) The plane will span 10 units of measurement on either side of 0.

Now, we can make the graph look fancier. We can add vertical and horizontal lines using geom_hline() and geom_vline(). For both we need to provide an x and a y intercept which will both be 0.

Code
ggplot() +
geom_function(fun = f) +
xlim(-10, 10) +
geom_hline(yintercept = 0) +
geom_vline(xintercept = 0) We can also label the x and y axis using labs():

Code
ggplot() +
geom_function(fun = f) +
xlim(-10, 10) +
ylim(-10, 10) +
geom_hline(yintercept = 0) +
geom_vline(xintercept = 0) +
labs(x = "x",
y = "y") We can also change the color of the line, that way it is easier to see:

Code
ggplot() +
geom_function(fun = f, color = "red") +
xlim(-10, 10) +
ylim(-10, 10) +
geom_hline(yintercept = 0) +
geom_vline(xintercept = 0) +
labs(x = "x",
y = "y") The rest of the graphs are made the same way, just in geom_function(), you change what fun equals.

Code
ggplot() +
geom_function(fun = g, color = "red") +
xlim(-100, 100) +
ylim(-100, 100) +
geom_hline(yintercept = 0) +
geom_vline(xintercept = 0) +
labs(x = "x",
y = "y")
Code
ggplot() +
geom_function(fun = h, color = "red") +
xlim(-150, 150) +
ylim(-150, 150) +
geom_hline(yintercept = 0) +
geom_vline(xintercept = 0) +
labs(x = "x",
y = "y")
ggplot() +
y = "y")