# Quadratic Equation Solver in MATLAB

This algorithm finds the roots of a quadratic equation in the form ax^2 + bx + c = 0.It tests for several conditions:
-If a = 0 and b = 0, then the equation reduces to c = 0. If this condition occurs we call the equation degenarete.
-If a = 0 and b ~= 0, the equation reduces to bx + c = 0 with single root at -c/b.
-If a ~= 0 and c = 0, the equation reduces to ax^2 + bx = 0. There are two roots, 0 and -b/a.
-If none apply we use the famous equation to find roots. Roots are either complex or real depending on the discriminant.

## 8 thoughts on “Quadratic Equation Solver in MATLAB”

1. Hi,

A pair of brackets is missing in the following two equations:
root1=(-b+sqrt(b*b-4*a*c)/(2*a));
root2=(-b-sqrt(b*b-4*a*c)/(2*a));

For instance, try giving the input as [a b c]=[1 4 4]. The program will return -4 and -4 as the roots.
Whereas the actual roots are: -2 and -2. So, the equations should have been as below-
root1=((-b+sqrt(b*b-4*a*c))/(2*a));
root2=((-b-sqrt(b*b-4*a*c))/(2*a));

2. Instead of all this code, Use :

exp = ‘x^2 + 3*x + 2’ ; % put any expression u need to evaluate
solve ( exp )

This is the advantage of Matlab ðŸ™‚
In no other language can you do it…

3. exp = â€˜x^2 + 3*x + 2â€² ; % put any expression u need to evaluate
solve ( exp )

if I use the above command Matlab asks me that what unefined variable x. So in that case what should I do??

4. what if i want to plot a graph for the quadratic equation and trace its roots onn the graph? how do i go about that?

5. To plot a polynomial: (as an example)

To designate the roots (if there are any) on the plot, you can use:

where fzero returns the roots as (x,y) pairs.