How to stop quadratic formula calculator from giving inverse outputs?

조회 수: 2 (최근 30일)
Alice
Alice 2024년 5월 30일
댓글: John D'Errico 2024년 6월 10일
Hi, I'm doing a worksheet based around matlab which involved solving quadratic formulas. I probably overcomplicated it and now made a calculator that keeps giving the inverse of my expected answer, could anyone help please?
for k = 1 : 7
disp ( ' For the equation ax^2 + bx + c ' )
a = input ( ' Enter a; ' );
b = input ( ' Enter b; ' );
c = input ( ' Enter c; ' );
D = ( b^2 ) - ( 4 * a * c );
if D < 0
fprintf ( ' \n The equation has no real roots . \n\n ')
elseif D == 0
root = -b / ( 2 * a );
fprintf ( ' \nThe equation has one root, \n ' )
fprintf ( ' %.7f\n\n ' , root )
else
r1 = ( - b + sqrt ( D ) ) / ( 2 * a ) ;
r2 = ( - b - sqrt ( D ) ) / ( 2 * a ) ;
fprintf ( '\n The equation has two roots, \n ' )
fprintf ( ' %.7f and %.7f\n\n ' , r1 ,r2 )
end
end
For the equation ax^2 + bx + c
Enter a;
1
Enter b;
9
Enter c;
14
The equation has two roots,
-2.0000000 and -7.0000000
For the equation ax^2 + bx + c
Enter a;
1
Enter b;
-3
Enter c;
-18
The equation has two roots,
6.0000000 and -3.0000000
  댓글 수: 3
Alice
Alice 2024년 6월 6일
on the first one it was this that was inversed, as i expected two positive roots. im an absolute beginner with this sorry
Enter a;
1
Enter b;
9
Enter c;
14
The equation has two roots,
-2.0000000 and -7.0000000
Alice
Alice 2024년 6월 6일
nevermind, just realised my sleep deprived misconception, sorry !

댓글을 달려면 로그인하십시오.

답변 (1개)

John D'Errico
John D'Errico 2024년 5월 30일
편집: John D'Errico 2024년 5월 30일
The roots of the quadratic you supply are indeed 6 and -3.
roots([1 -3 -18])
ans = 2x1
6.0000 -3.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
So your code worked correctly.
If you expected something else, then it is your expectations that are wrong, not your code.
Anyway, perhaps I should point out that a cute feature of polynomial equations in that if you reverse the sequence of the cofficients, thus as here...
roots([-18 -3 1])
ans = 2x1
-0.3333 0.1667
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
then the computed roots will be the inverses of the true roots. And that is what I think has happened to you in some way. This would be true for any order polynomial, in fact.
Perhaps that is what you did at some point in your code, or maybe in your spreadsheet, you provided the coefficients in the reverse order. Can we know what you actually did wrong, since your code works correctly? Only da shadow know, but I'm going to bet, that IF you have gotten the inverse of the expected roots, then somewhere, somehow, you flipped the sequence of the coefficients. I'm not da shadow though.
  댓글 수: 3
Alice
Alice 2024년 6월 6일
nevermind, just realised my sleep deprived misconception, sorry !
John D'Errico
John D'Errico 2024년 6월 10일
Lol. An intersting fact about an equation like
x^2 + 14*x + 9
is if ALL of the coefficients are positive, then there can NEVER be any positive real roots. Any real roots must either be zero or negative, and zero only happens if there is a zero constant term.
(You can prove that easily enough if you think about it.) For a positive root to exist, at least SOME of the coefficients must be negative.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Quadratic Programming and Cone Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by