You can create a function in MATLAB to find the roots of a quadratic equation for all three cases of discriminants using the following code:
main.m372 chars15 linesIn this function, a, b, and c are the coefficients of the quadratic equation ax^2 + bx + c = 0. The function calculates the discriminant using the formula b^2 - 4ac.
If the discriminant is greater than 0, which means there are two distinct real roots, the function calculates the roots using the quadratic formula and returns them as an array [root1, root2].
If the discriminant is equal to 0, which means there is one real root, the function calculates the root using the quadratic formula and returns it as a single value.
If the discriminant is less than 0, which means there are no real roots, the function returns the string 'No real roots'.
You can use this function by calling it with the coefficients of your quadratic equation, for example:
main.m38 chars2 lines
This will give you the roots of the equation x^2 - 3x + 2 = 0, which are 1 and 2.
gistlibby LogSnag