Select one root of an second degree equation

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일

0 개 추천

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

There could be multiple positive roots; the above would give the greater of the two.
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일

1 개 추천

p = [3 -6 -7];
r = roots(p);
out = r(r>0);

댓글 수: 2

about positive root
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일

0 개 추천

r = roots(...);
r(1)
Did you care which root it displays?
Pavel
Pavel 2013년 6월 1일

0 개 추천

thanks all for replaing

카테고리

도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by