http://www.mathworks.com/matlabcentral/newsreader/view_thread/297460
MATLAB Central Newsreader  Quadratic formula
Feed for thread: Quadratic formula
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Sat, 27 Nov 2010 01:29:05 +0000
Quadratic formula
http://www.mathworks.com/matlabcentral/newsreader/view_thread/297460#799683
Don
Ok so this programming assignment calls for:<br>
<br>
Write two programs which find the roots of a quadratic. For the first, use the standard quadratic<br>
formula. For the second, modify the formula so that the term<br>
is replaced with a more stable formula. Test both formulas on f (x) = x2 􀀀 50000x + 1. How close are your roots to the true solution?<br>
<br>
<br>
Questions: How do I make it into a more stable formula and how do i test the roots?<br>
<br>
Here is my code:<br>
<br>
%Write two programs which find the roots of a quadratic. For the first, use <br>
%the standard quadratic<br>
%formula. For the second, modify the formula so that the term<br>
% <br>
%is replaced with a more stable formula. Test both formulas on f (x) = x2 ? 50000x + 1. How<br>
%close are your roots to the true solution?<br>
<br>
<br>
% This mfile will solve the quadratic equation :ax^2 + bx + c = 0<br>
<br>
function Quad= Quadratic(A,B,C)<br>
<br>
% A=1;<br>
% B=2;<br>
% C=3;<br>
<br>
<br>
%IfElse statement for if A=0<br>
<br>
if A==0,<br>
X= C/B;<br>
else<br>
X(1) = (B+sqrt(B^24*A*C))/(2*A);<br>
X(2) = (Bsqrt(B^24*A*C))/(2*A);<br>
<br>
disp(X(1))<br>
disp(X(2))<br>
fprintf('X(1) = %f \n',X(1));<br>
fprintf('X(2) = %f \n',X(2));<br>
<br>
<br>
end

Sat, 27 Nov 2010 02:24:03 +0000
Re: Quadratic formula
http://www.mathworks.com/matlabcentral/newsreader/view_thread/297460#799688
Roger Stafford
"Don " <don.jackson@coker.edu> wrote in message <icpmt1$seh$1@fred.mathworks.com>...<br>
> ......<br>
> Questions: How do I make it into a more stable formula and how do i test the roots?<br>
> ......<br>
        <br>
I won't solve your problem for you since it is obviously homework. But here is a hint. Ask yourself what the situation would be to make the standard formula be unstable. Clearly by 'unstable' your instructor means a situation where accuracy would be seriously compromised. It isn't only when A is zero. The scope of possible instability is wider than that. What are these circumstances and what can you do about them? It looks as though your instructor has given you a concrete example of instability. In algebra have you ever heard the term 'rationalize' as applied to expressions such as this standard formula. Hint, hint!<br>
<br>
Roger Stafford

Tue, 20 Mar 2012 09:53:11 +0000
Re: Quadratic formula
http://www.mathworks.com/matlabcentral/newsreader/view_thread/297460#870567
Hamad
"Don " <don.jackson@coker.edu> wrote in message <icpmt1$seh$1@fred.mathworks.com>...<br>
> Ok so this programming assignment calls for:<br>
> <br>
> Write two programs which find the roots of a quadratic. For the first, use the standard quadratic<br>
> formula. For the second, modify the formula so that the term<br>
> is replaced with a more stable formula. Test both formulas on f (x) = x2 ???? 50000x + 1. How close are your roots to the true solution?<br>
> <br>
> <br>
> Questions: How do I make it into a more stable formula and how do i test the roots?<br>
> <br>
> Here is my code:<br>
> <br>
> %Write two programs which find the roots of a quadratic. For the first, use <br>
> %the standard quadratic<br>
> %formula. For the second, modify the formula so that the term<br>
> % <br>
> %is replaced with a more stable formula. Test both formulas on f (x) = x2 ? 50000x + 1. How<br>
> %close are your roots to the true solution?<br>
> <br>
> <br>
> % This mfile will solve the quadratic equation :ax^2 + bx + c = 0<br>
> <br>
> function Quad= Quadratic(A,B,C)<br>
> <br>
> % A=1;<br>
> % B=2;<br>
> % C=3;<br>
> <br>
> <br>
> %IfElse statement for if A=0<br>
> <br>
> if A==0,<br>
> X= C/B;<br>
> else<br>
> X(1) = (B+sqrt(B^24*A*C))/(2*A);<br>
> X(2) = (Bsqrt(B^24*A*C))/(2*A);<br>
> <br>
> disp(X(1))<br>
> disp(X(2))<br>
> fprintf('X(1) = %f \n',X(1));<br>
> fprintf('X(2) = %f \n',X(2));<br>
> <br>
> <br>
> end<br>
<br>
<br>
<br>
can you please tell<br>
<br>
if A===<br>
<br>
how is X=C/B and not B/A ?<br>
<br>
regards

Tue, 20 Mar 2012 10:54:19 +0000
Re: Quadratic formula
http://www.mathworks.com/matlabcentral/newsreader/view_thread/297460#870570
Torsten
Am Dienstag, 20. März 2012 10:53:11 UTC+1 schrieb Hamad :<br>
> "Don " <don.jackson@coker.edu> wrote in message <icpmt1$seh$1@fred.mathworks.com>...<br>
> > Ok so this programming assignment calls for:<br>
> > <br>
> > Write two programs which find the roots of a quadratic. For the first, use the standard quadratic<br>
> > formula. For the second, modify the formula so that the term<br>
> > is replaced with a more stable formula. Test both formulas on f (x) = x2 ???? 50000x + 1. How close are your roots to the true solution?<br>
> > <br>
> > <br>
> > Questions: How do I make it into a more stable formula and how do i test the roots?<br>
> > <br>
> > Here is my code:<br>
> > <br>
> > %Write two programs which find the roots of a quadratic. For the first, use <br>
> > %the standard quadratic<br>
> > %formula. For the second, modify the formula so that the term<br>
> > % <br>
> > %is replaced with a more stable formula. Test both formulas on f (x) = x2 ? 50000x + 1. How<br>
> > %close are your roots to the true solution?<br>
> > <br>
> > <br>
> > % This mfile will solve the quadratic equation :ax^2 + bx + c = 0<br>
> > <br>
> > function Quad= Quadratic(A,B,C)<br>
> > <br>
> > % A=1;<br>
> > % B=2;<br>
> > % C=3;<br>
> > <br>
> > <br>
> > %IfElse statement for if A=0<br>
> > <br>
> > if A==0,<br>
> > X= C/B;<br>
> > else<br>
> > X(1) = (B+sqrt(B^24*A*C))/(2*A);<br>
> > X(2) = (Bsqrt(B^24*A*C))/(2*A);<br>
> > <br>
> > disp(X(1))<br>
> > disp(X(2))<br>
> > fprintf('X(1) = %f \n',X(1));<br>
> > fprintf('X(2) = %f \n',X(2));<br>
> > <br>
> > <br>
> > end<br>
> <br>
> <br>
> <br>
> can you please tell<br>
> <br>
> if A===<br>
> <br>
> how is X=C/B and not B/A ?<br>
> <br>
> regards<br>
<br>
The original equation is <br>
Ax^2+Bx+C=0.<br>
Now if A=0, the equation left is<br>
Bx+C=0, thus x=C/B.<br>
<br>
Best wishes<br>
Torsten.

Tue, 20 Mar 2012 16:25:15 +0000
Re: Quadratic formula
http://www.mathworks.com/matlabcentral/newsreader/view_thread/297460#870621
Roger Stafford
"Roger Stafford" wrote in message <icpq43$if7$1@fred.mathworks.com>...<br>
> "Don " <don.jackson@coker.edu> wrote in message <icpmt1$seh$1@fred.mathworks.com>...<br>
> > ......<br>
> > Questions: How do I make it into a more stable formula and how do i test the roots?<br>
> > ......<br>
>         <br>
> I won't solve your problem for you since it is obviously homework. But here is a hint. ...... In algebra have you ever heard the term 'rationalize' as applied to expressions such as this standard formula. Hint, hint!<br>
> Roger Stafford<br>
      <br>
Over a year has gone by since this question was asked so it seems appropriate to divulge what I was hinting at without compromising homework assignment ethics.<br>
<br>
In the quadratic equation<br>
<br>
x^2  50000*x + 1 = 0<br>
<br>
the standard quadratic formula for the roots of a*x^2+b*x+c=0 gives one of its two roots as:<br>
<br>
x = (bsqrt(b^24*a*c))/(2*a) = (50000sqrt(50000^24))/2<br>
<br>
Though in theory this expression is correct, its numerator is a very small difference between two very large numbers and the round off errors are consequently proportionally very large. It is far better in this case to use the alternate formula:<br>
<br>
x = 2*c/(b+sqrt(b^24*a*c)) = 2/(50000+sqrt(50000^24))<br>
<br>
Here the denominator is the sum of two very large numbers wherein the relative round off error is much, much smaller.<br>
<br>
One can prove this second formula for the quadratic root by multiplying both numerator and denominator by b+sqrt(b^24*a*c) and simplifying. In algebra this is a standard method known as rationalizing the numerator.<br>
<br>
Note that the corresponding formula for the other root would be very inaccurate compared to its standard formula in this case. The decision as to which formula to use depends very much on the particular numbers involved. A good function written to solve general quadratic equations should be able to to select either formula depending on which would be the most accurate.<br>
<br>
Roger Stafford

Wed, 21 Mar 2012 09:45:45 +0000
Re: Quadratic formula
http://www.mathworks.com/matlabcentral/newsreader/view_thread/297460#870709
Greg Heath
On Mar 20, 12:25 pm, "Roger Stafford"<br>
<ellieandrogerxy...@mindspring.com.invalid> wrote:<br>
> "Roger Stafford" wrote in message <icpq43$if...@fred.mathworks.com>...<br>
> > "Don " <don.jack...@coker.edu> wrote in message <icpmt1$se...@fred.mathworks.com>...<br>
> > > ......<br>
> > > Questions: How do I make it into a more stable formula and how do i test the roots?<br>
> > > ......<br>
> >         <br>
> > I won't solve your problem for you since it is obviously homework. But here is a hint. ...... In algebra have you ever heard the term 'rationalize' as applied to expressions such as this standard formula. Hint, hint!<br>
> > Roger Stafford<br>
><br>
>       <br>
> Over a year has gone by since this question was asked so it seems appropriate to divulge what I was hinting at without compromising homework assignment ethics.<br>
><br>
> In the quadratic equation<br>
><br>
> x^2  50000*x + 1 = 0<br>
><br>
> the standard quadratic formula for the roots of a*x^2+b*x+c=0 gives one of its two roots as:<br>
><br>
> x = (bsqrt(b^24*a*c))/(2*a) = (50000sqrt(50000^24))/2<br>
><br>
> Though in theory this expression is correct, its numerator is a very small difference between two very large numbers and the round off errors are consequently proportionally very large. It is far better in this case to use the alternate formula:<br>
><br>
> x = 2*c/(b+sqrt(b^24*a*c)) = 2/(50000+sqrt(50000^24))<br>
><br>
> Here the denominator is the sum of two very large numbers wherein the relative round off error is much, much smaller.<br>
><br>
> One can prove this second formula for the quadratic root by multiplying both numerator and denominator by b+sqrt(b^24*a*c) and simplifying. In algebra this is a standard method known as rationalizing the numerator.<br>
><br>
> Note that the corresponding formula for the other root would be very inaccurate compared to its standard formula in this case. The decision as to which formula to use depends very much on the particular numbers involved. A good function written to solve general quadratic equations should be able to to select either formula depending on which would be the most accurate.<br>
><br>
> Roger Stafford<br>
<br>
What else would be needed in addition to sign(b) and<br>
sign(b^24*a*c) ?<br>
<br>
Hope this helps.<br>
<br>
Greg