6 Functions [10 marks]
Create a function that computes the two roots of a quadratic equation using the quadratic formula. Recall that the quadratic formula gives the two roots of the quadratic equation \(ax^2 + bx + c = 0\) as:
\[\begin{eqnarray*} x_1 & = & \frac{-b + \sqrt{b^2 - 4ac}}{2a} \\ x_2 & = & \frac{-b - \sqrt{b^2 - 4ac}}{2a} \end{eqnarray*}\]Your function should satisfy the following constraints:
- it should be named
roots2
- it should return two values
x1
andx2
(the two roots given by the quadratic formula) - it should have one input vector
coeff
The input vector coeff
should contain the three coefficients of the quadratic equation. The first element of coeff
is equal to a
, the second element of coeff
is equal to b
, and the third element of coeff
is equal to c
.
6.1 Example usage
The following example finds the roots of the quadratic equation \(-x^2 + 3x + 10\):
co = [-1 3 10];
[x1, x2] = roots2(co)