Select one root of an second degree equation
조회 수: 16 (최근 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
댓글 수: 0
채택된 답변
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
2013년 6월 2일
There could be multiple positive roots; the above would give the greater of the two.
추가 답변 (3개)
Andrei Bobrov
2013년 6월 1일
편집: Andrei Bobrov
2013년 6월 1일
p = [3 -6 -7];
r = roots(p);
out = r(r>0);
댓글 수: 2
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Octave에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!