QuadraticRoots(1, 0, 5) # "You have chosen the quadratic equation 1x^2 + 0x + 5." # "This quadratic equation has no real numbered roots." The function call in R would be quadraticRoots(1, 0, 5). In this example, the quadratic formula is used for the equation \(y = x^2 + 5\). It does not really matter whether the quadratic form can be factored or not. The quadratic formula can be applied to any quadratic equation in the form \(y = ax^2 + bx + c\) ( \(a \neq 0\)). Using The Quadratic Formula Through Examples The format() function with round() is used to round the answers (x-intercepts) to five decimal places. Return(paste0("The quadratic equation has only one root. Return(paste0("The two x-intercepts for the quadratic equation are ",įormat(round(x_int_plus, 5), nsmall = 5), " and ",įormat(round(x_int_neg, 5), nsmall = 5), ".")) X_int_plus <- (-b + sqrt(discriminant)) / (2*a) # Quadratic equation form of ax^2 + bx + c The usage of print and paste0() allows for printing strings in R. Since the quadratic formula has three cases with the discriminant we need if, else if and else statements. In R, a function has the following format. Creating The Quadratic Formula Function In R