Bar Plot

Exercises

Use curve() to plot the linear function \(y = 2x + 1\) over the range \(x = -10\) to \(x = 10\).

Solution:

curve(2 * x + 1, from = -10, to = 10, 
       col = "blue", lwd = 2,
      main = "Plot of y = 2x + 1", 
      xlab = "x", ylab = "y")

Plot the function \(y = x^2 - 4x + 3\) over the range \(x = -5\) to \(x = 5\).

Solution:

curve(x^2 - 4 * x + 3, from = -5, to = 5, 
       col = "red", lwd = 2,
      main = expression(paste("Plot of ",y ==  x^2 - 4*x + 3)), 
      xlab = "x", ylab = "y")

Plot the function \(y = x^2\) on the interval \(x = -10\) to \(x = 10\), then add \(y = -x^2\) on the same plot, using different colors for each curve.

Hint: The first curve has the argument ylim=c(-100,100).

Solution:

curve(x^2, from = -10, to = 10, 
      col = "green", 
      lwd = 2,
      main = expression(paste("Plot of ",y == x^2," and ",y == -x^2)), 
      xlab = "x", ylab = "y", ylim=c(-100,100))
curve(-x^2, from = -10, to = 10, 
      col = "purple", 
      lty = 2, lwd = 2, add = TRUE)
      
legend("topright", 
       legend = c(expression(y == x^2), expression(y == -x^2)), 
          col = c("green", "purple"), 
          lty = c(1, 2), lwd = 2)

Use curve() to plot the function \(y = e^x\) over the range \(x = -2\) to \(x = 2\).

Solution:

curve(exp(x), from = -2, to = 2, 
        col = "orange", lwd = 2,
       main = expression(paste("Plot of ",y == e^x)), 
       xlab = "x", ylab = "y")

Plot \(y = \sin(x)\) and \(y = \cos(x)\) on the same plot over the interval \(x = 0\) to \(x = 2\pi\), with different line types and colors for each curve.

Solution:

curve(sin(x), from = 0, to = 2 * pi, 
      col = "blue", lwd = 2,
      main = "Sine and Cosine Curves", 
      xlab = "x", ylab = "y")
curve(cos(x), from = 0, to = 2 * pi, 
      col = "red", lty = 2, lwd = 2, add = TRUE)
      
legend("topright", 
       legend = c("sin(x)", "cos(x)"), 
          col = c("blue", "red"), 
          lty = c(1, 2), lwd = 2)

Use curve() to plot the natural logarithm function \(y = \ln(x)\) over the range \(x = 0.1\) to \(x = 10\). Add labels to the axes to indicate the logarithmic nature of the plot.

Solution:

curve(log(x), from = 0.1, to = 10, 
      col = "brown", lwd = 2,
      main = "Plot of y = ln(x)", 
      xlab = "x", ylab = "y")

Plot the probability density function of a standard normal distribution, \(y = \frac{1}{\sqrt{2\pi}} e^{-\frac{x^2}{2}}\), over the range \(x = -3\) to \(x = 3\).

Solution:

curve((1 / sqrt(2 * pi)) * exp(-x^2 / 2), 
      from = -3, to = 3, 
       col = "darkgreen", lwd = 2, 
       main = "Standard Normal Distribution",
       xlab = "x", ylab = "Density")

Use curve() to plot the logistic function \(y = \frac{1}{1 + e^{-x}}\) over the range \(x = -6\) to \(x = 6\).

Solution:

curve(1 / (1 + exp(-x)), from = -6, to = 6, 
      col = "purple", lwd = 2,
      main = "Plot of Logistic Function", 
      xlab = "x", ylab = "y")

Plot \(y = \sin(x)\) over the interval \(x = 0\) to \(x = 2\pi\) with a higher resolution using the n argument in curve(), and set n = 1000.

Solution:

curve(sin(x), from = 0, to = 2 * pi, 
      col = "blue", lwd = 2,
      n = 1000, 
      main = "High-Resolution Plot of y = sin(x)", 
      xlab = "x", ylab = "y")

Define a custom function, \(f(x) = 3x^3 - 5x^2 + 2x - 4\), and use curve() to plot it over the range \(x = -3\) to \(x = 3\).

Solution:

custom_function <- function(x) { 3 * x^3 - 5 * x^2 + 2 * x - 4 }
curve(custom_function, from = -3, to = 3, 
      col = "pink", lwd = 2,
      main = "Plot of Custom Polynomial Function", 
      xlab = "x", ylab = "y")
  • Plot \(y = 2x + 3\) and \(y = -x + 5\) on the same plot over the range \(x = -10\) to \(x = 10\).

  • Use different line types (e.g., solid and dashed) and colors for each line.

  • Add a legend indicating which line corresponds to each function.

Solution:

curve(2 * x + 3, from = -10, to = 10, 
      col = "blue", lty = 1, lwd = 2,
      ylab = "y", 
      main = "Plot of Linear Functions with Legend")
      
curve(-x + 5, from = -10, to = 10, 
       col = "red", lty = 2, lwd = 2, add = TRUE)
       
legend("topright", 
        legend = c("y = 2x + 3", "y = -x + 5"), 
       col = c("blue", "red"), 
       lty = c(1, 2), lwd = 2)
  • Plot \(y = \sin(x)\) and \(y = \cos(x)\) on the same plot over the interval \(x = 0\) to \(x = 2\pi\).

  • Use distinct colors and line types for each curve.

  • Add a legend to show which curve is sine and which is cosine.

Solution:

curve(sin(x), from = 0, to = 2 * pi, 
      col = "purple", lty = 1, lwd = 2,
      ylab = "y", 
      main = "Sine and Cosine Curves with Legend")
curve(cos(x), from = 0, to = 2 * pi, 
      col = "orange", lty = 3, lwd = 2, add = TRUE)
      
legend("topright", 
       legend = c("sin(x)", "cos(x)"), 
       col = c("purple", "orange"), 
       lty = c(1, 3), lwd = 2)
  • Plot \(y = x^2\) and \(y = e^x\) on the same graph over the range \(x = -2\) to \(x = 2\).

  • Use different colors and line types for each function.

  • Add a legend that labels each function appropriately.

Solution:

curve(x^2, from = -2, to = 2,
      col = "green", lty = 4, lwd = 2,
      ylab = "y", 
      main = "Quadratic and Exponential Functions with Legend")
curve(exp(x), from = -2, to = 2, 
col = "brown", lty = 5, lwd = 2, add = TRUE)

legend("topleft", 
       legend = c(expression(y == x^2), expression(y == e^x)), 
       col = c("green", "brown"), 
       lty = c(4, 5), lwd = 2)