638 views (last 30 days)

Hai,

How could I solve a quadratic equation in matlab? Looking for your reply.

BSD

Walter Roberson
on 8 Nov 2011

roots(). Or if you have the symbolic toolbox, solve()

Walter Roberson
on 8 Nov 2011

Walter Roberson
on 9 Nov 2011

You can tell whether a number has a complex part or not by testing to see if the imaginary part is 0. imag(x) gives you the imaginary part of x, so imag(x)==0 tests whether the imaginary part is 0. TheRoots(imag(TheRoots)==0) thus selects only the roots which are real-valued with no imaginary component.

Of course for a quadratic function over real coefficients, either _neither_ root is complex or _both_ roots are complex...

Sign in to comment.

Rick Rosson
on 8 Nov 2011

Please try:

x = zeros(2,1);

d = sqrt(b^2 - 4*a*c);

x(1) = ( -b + d ) / (2*a);

x(2) = ( -b - d ) / (2*a);

HTH.

Rick

Rick Rosson
on 9 Nov 2011

Walter Roberson
on 9 Nov 2011

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.