Link Search Menu Expand Document (external link)

How to graph mathematical functions (in R)

See all solutions.

Task

Assume we have a mathematical formula and we would like to plot a graph of it using the standard Cartesian coordinate system.

Related tasks:

Solution

Let’s assume we want to graph the function $x^2-5x+9$ from $x=-10$ to $x=10$.

We compute a series of $(x,y)$ pairs to generate the plot. Notice how R automatically computes a $y$ value for each $x$ value if we just include all the $x$s in the formula we wish to graph.

1
2
3
xs <- seq(-10,10,length.out=100)   # 100 values from x=-10 to x=10
ys <- xs^2 - 5*xs + 9              # compute all corresponding ys
plot( xs, ys, type='l' )

You can also plot more than one function on the same graph.

1
2
3
ys2 <- 10*sin(xs) + 20             # ys for the formula y=10sin(x)+20
plot( xs, ys, type='l' )           # make the original plot
lines( xs, ys2 )                   # add the second plot to it

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Contributed by Nathan Carter (ncarter@bentley.edu)