Function quadratic.solve returns the two real or complex x-values solving a quadratic equation in the standard form of aX^2+bx+c=0

Call function in the form: quadratic.solve(a,b,c) where a,b,c are real numeric values as input in the standard quadratic form. The output will be the two solutions for x in complex form. (to cover real or imaginary number solutions) Example calls are after the function code.

HW Mark Schmalfeld


knitr::opts_chunk$set(echo = TRUE)


quadratic.solve<-function(a,b,c)
{   
  p<-b/a
  q<-c/a
  resultx1<-as.complex(-p/2)+as.complex(((p/2)^2-q))^0.5
    
  resultx2<-as.complex(-p/2)-as.complex(((p/2)^2-q))^0.5
  
  resultList<-list(resultx1,resultx2)

  return(resultList)
    
}
#A couple of example calls to use the function
quadratic.solve(4,8,4)
## [[1]]
## [1] -1+0i
## 
## [[2]]
## [1] -1+0i
quadratic.solve(3,7,4)
## [[1]]
## [1] -1+0i
## 
## [[2]]
## [1] -1.333333+0i
quadratic.solve(4,9,25)
## [[1]]
## [1] -1.125+2.232571i
## 
## [[2]]
## [1] -1.125-2.232571i
quadratic.solve(-1,0,25)
## [[1]]
## [1] 5+0i
## 
## [[2]]
## [1] -5+0i
quadratic.solve(-5,5,-50)
## [[1]]
## [1] 0.5+3.122499i
## 
## [[2]]
## [1] 0.5-3.122499i

```

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.