Select one root of an second degree equation

5 次查看(过去 30 天)
i have solved a second degree equation in matlab using the roots() command and i want only one root to be displayed. How do i do this in MATLAB? Thank you very much

采纳的回答

Roger Stafford
Roger Stafford 2013-6-1
You could also use the formula for second degree equations. If the equation is:
a*x^2 + b*x + c = 0
then get the positive root, (if there is just one,) with:
r = (-b + sign(a)*sqrt(b^2-4*a*c))/(2*a);
  2 个评论
Walter Roberson
Walter Roberson 2013-6-2
There could be multiple positive roots; the above would give the greater of the two.
Roger Stafford
Roger Stafford 2013-6-2
That's why I said "if there is just one [positive root]".

请先登录,再进行评论。

更多回答(3 个)

Andrei Bobrov
Andrei Bobrov 2013-6-1
编辑:Andrei Bobrov 2013-6-1
p = [3 -6 -7];
r = roots(p);
out = r(r>0);
  2 个评论
Walter Roberson
Walter Roberson 2013-6-1
Caution: if there are imaginary roots, then only real() of the roots will be considered by the ">" operator. You might want to use
out = r(imag(r) == 0 & r > 0)
to select only positive real roots. If none exist then "out" will be empty.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2013-6-1
r = roots(...);
r(1)
Did you care which root it displays?

Pavel
Pavel 2013-6-1
thanks all for replaing

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by