Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Quadratic formula

Subject: Quadratic formula

From: Don

Date: 27 Nov, 2010 01:29:05

Message: 1 of 6

Ok so this programming assignment calls for:

Write two programs which find the roots of a quadratic. For the first, use the standard quadratic
formula. For the second, modify the formula so that the term
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?


Questions: How do I make it into a more stable formula and how do i test the roots?

Here is my code:

%Write two programs which find the roots of a quadratic. For the first, use
%the standard quadratic
%formula. For the second, modify the formula so that the term
%
%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?


% This m-file will solve the quadratic equation :ax^2 + bx + c = 0

function Quad= Quadratic(A,B,C)

% A=1;
% B=2;
% C=3;


%If-Else statement for if A=0

if A==0,
    X= -C/B;
else
    X(1) = (-B+sqrt(B^2-4*A*C))/(2*A);
    X(2) = (-B-sqrt(B^2-4*A*C))/(2*A);

    disp(X(1))
     disp(X(2))
     fprintf('X(1) = %f \n',X(1));
    fprintf('X(2) = %f \n',X(2));
    

end

Subject: Quadratic formula

From: Roger Stafford

Date: 27 Nov, 2010 02:24:03

Message: 2 of 6

"Don " <don.jackson@coker.edu> wrote in message <icpmt1$seh$1@fred.mathworks.com>...
> ......
> Questions: How do I make it into a more stable formula and how do i test the roots?
> ......
- - - - - - - - -
  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!

Roger Stafford

Subject: Quadratic formula

From: Hamad

Date: 20 Mar, 2012 09:53:11

Message: 3 of 6

"Don " <don.jackson@coker.edu> wrote in message <icpmt1$seh$1@fred.mathworks.com>...
> Ok so this programming assignment calls for:
>
> Write two programs which find the roots of a quadratic. For the first, use the standard quadratic
> formula. For the second, modify the formula so that the term
> 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?
>
>
> Questions: How do I make it into a more stable formula and how do i test the roots?
>
> Here is my code:
>
> %Write two programs which find the roots of a quadratic. For the first, use
> %the standard quadratic
> %formula. For the second, modify the formula so that the term
> %
> %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?
>
>
> % This m-file will solve the quadratic equation :ax^2 + bx + c = 0
>
> function Quad= Quadratic(A,B,C)
>
> % A=1;
> % B=2;
> % C=3;
>
>
> %If-Else statement for if A=0
>
> if A==0,
> X= -C/B;
> else
> X(1) = (-B+sqrt(B^2-4*A*C))/(2*A);
> X(2) = (-B-sqrt(B^2-4*A*C))/(2*A);
>
> disp(X(1))
> disp(X(2))
> fprintf('X(1) = %f \n',X(1));
> fprintf('X(2) = %f \n',X(2));
>
>
> end



can you please tell

if A===

how is X=-C/B and not -B/A ?

regards

Subject: Quadratic formula

From: Torsten

Date: 20 Mar, 2012 10:54:19

Message: 4 of 6

Am Dienstag, 20. März 2012 10:53:11 UTC+1 schrieb Hamad :
> "Don " <don.jackson@coker.edu> wrote in message <icpmt1$seh$1@fred.mathworks.com>...
> > Ok so this programming assignment calls for:
> >
> > Write two programs which find the roots of a quadratic. For the first, use the standard quadratic
> > formula. For the second, modify the formula so that the term
> > 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?
> >
> >
> > Questions: How do I make it into a more stable formula and how do i test the roots?
> >
> > Here is my code:
> >
> > %Write two programs which find the roots of a quadratic. For the first, use
> > %the standard quadratic
> > %formula. For the second, modify the formula so that the term
> > %
> > %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?
> >
> >
> > % This m-file will solve the quadratic equation :ax^2 + bx + c = 0
> >
> > function Quad= Quadratic(A,B,C)
> >
> > % A=1;
> > % B=2;
> > % C=3;
> >
> >
> > %If-Else statement for if A=0
> >
> > if A==0,
> > X= -C/B;
> > else
> > X(1) = (-B+sqrt(B^2-4*A*C))/(2*A);
> > X(2) = (-B-sqrt(B^2-4*A*C))/(2*A);
> >
> > disp(X(1))
> > disp(X(2))
> > fprintf('X(1) = %f \n',X(1));
> > fprintf('X(2) = %f \n',X(2));
> >
> >
> > end
>
>
>
> can you please tell
>
> if A===
>
> how is X=-C/B and not -B/A ?
>
> regards

The original equation is
Ax^2+Bx+C=0.
Now if A=0, the equation left is
Bx+C=0, thus x=-C/B.

Best wishes
Torsten.

Subject: Quadratic formula

From: Roger Stafford

Date: 20 Mar, 2012 16:25:15

Message: 5 of 6

"Roger Stafford" wrote in message <icpq43$if7$1@fred.mathworks.com>...
> "Don " <don.jackson@coker.edu> wrote in message <icpmt1$seh$1@fred.mathworks.com>...
> > ......
> > Questions: How do I make it into a more stable formula and how do i test the roots?
> > ......
> - - - - - - - - -
> 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!
> Roger Stafford
- - - - - - -
  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.

  In the quadratic equation

 x^2 - 50000*x + 1 = 0

the standard quadratic formula for the roots of a*x^2+b*x+c=0 gives one of its two roots as:

 x = (-b-sqrt(b^2-4*a*c))/(2*a) = (50000-sqrt(50000^2-4))/2

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:

 x = 2*c/(-b+sqrt(b^2-4*a*c)) = 2/(50000+sqrt(50000^2-4))

Here the denominator is the sum of two very large numbers wherein the relative round off error is much, much smaller.

  One can prove this second formula for the quadratic root by multiplying both numerator and denominator by -b+sqrt(b^2-4*a*c) and simplifying. In algebra this is a standard method known as rationalizing the numerator.

  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.

Roger Stafford

Subject: Quadratic formula

From: Greg Heath

Date: 21 Mar, 2012 09:45:45

Message: 6 of 6

On Mar 20, 12:25 pm, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
> "Roger Stafford" wrote in message <icpq43$if...@fred.mathworks.com>...
> > "Don " <don.jack...@coker.edu> wrote in message <icpmt1$se...@fred.mathworks.com>...
> > > ......
> > > Questions: How do I make it into a more stable formula and how do i test the roots?
> > > ......
> > - - - - - - - - -
> > 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!
> > Roger Stafford
>
> - - - - - - -
> 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.
>
> In the quadratic equation
>
> x^2 - 50000*x + 1 = 0
>
> the standard quadratic formula for the roots of a*x^2+b*x+c=0 gives one of its two roots as:
>
> x = (-b-sqrt(b^2-4*a*c))/(2*a) = (50000-sqrt(50000^2-4))/2
>
> 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:
>
> x = 2*c/(-b+sqrt(b^2-4*a*c)) = 2/(50000+sqrt(50000^2-4))
>
> Here the denominator is the sum of two very large numbers wherein the relative round off error is much, much smaller.
>
> One can prove this second formula for the quadratic root by multiplying both numerator and denominator by -b+sqrt(b^2-4*a*c) and simplifying. In algebra this is a standard method known as rationalizing the numerator.
>
> 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.
>
> Roger Stafford

What else would be needed in addition to sign(b) and
sign(b^2-4*a*c) ?

Hope this helps.

Greg

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us