How to resolve the algorithm Roots of a quadratic function step by step in the Mathematica/Wolfram Language programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Roots of a quadratic function step by step in the Mathematica/Wolfram Language programming language

Table of Contents

Problem Statement

Write a program to find the roots of a quadratic equation, i.e., solve the equation

a

x

2

b x + c

0

{\displaystyle ax^{2}+bx+c=0}

. Your program must correctly handle non-real roots, but it need not check that

a ≠ 0

{\displaystyle a\neq 0}

. The problem of solving a quadratic equation is a good example of how dangerous it can be to ignore the peculiarities of floating-point arithmetic. The obvious way to implement the quadratic formula suffers catastrophic loss of accuracy when one of the roots to be found is much closer to 0 than the other. In their classic textbook on numeric methods Computer Methods for Mathematical Computations, George Forsythe, Michael Malcolm, and Cleve Moler suggest trying the naive algorithm with

a

1

{\displaystyle a=1}

,

b

10

5

{\displaystyle b=-10^{5}}

, and

c

1

{\displaystyle c=1}

. (For double-precision floats, set

b

10

9

{\displaystyle b=-10^{9}}

.) Consider the following implementation in Ada: As we can see, the second root has lost all significant figures. The right answer is that X2 is about

10

− 6

{\displaystyle 10^{-6}}

. The naive method is numerically unstable. Suggested by Middlebrook (D-OA), a better numerical method: to define two parameters

q

a c

/

b

{\displaystyle q={\sqrt {ac}}/b}

and

f

1

/

2 +

1 − 4

q

2

/

2

{\displaystyle f=1/2+{\sqrt {1-4q^{2}}}/2}

and the two roots of the quardratic are:

− b

a

f

{\displaystyle {\frac {-b}{a}}f}

and

− c

b f

{\displaystyle {\frac {-c}{bf}}}

Task: do it better. This means that given

a

1

{\displaystyle a=1}

,

b

10

9

{\displaystyle b=-10^{9}}

, and

c

1

{\displaystyle c=1}

, both of the roots your program returns should be greater than

10

− 11

{\displaystyle 10^{-11}}

. Or, if your language can't do floating-point arithmetic any more precisely than single precision, your program should be able to handle

b

10

6

{\displaystyle b=-10^{6}}

. Either way, show what your program gives as the roots of the quadratic in question. See page 9 of "What Every Scientist Should Know About Floating-Point Arithmetic" for a possible algorithm.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Roots of a quadratic function step by step in the Mathematica/Wolfram Language programming language

The provided Wolfram programming language code demonstrates various ways to solve polynomial equations and find roots. Here's a detailed explanation:

1. Solve[a x^2 + b x + c == 0, x]

This line solves a general quadratic equation a x^2 + b x + c == 0 for x. It finds the two possible values of x that satisfy the equation.

2. Solve[x^2 - 10^5 x + 1 == 0, x]

This line specifically solves the quadratic equation x^2 - 10^5 x + 1 == 0 for x. It again returns the two possible solutions.

3. Root[#1^2 - 10^5 #1 + 1 &, 1]

This line uses the Root function to find the first root of the polynomial #1^2 - 10^5 #1 + 1 (which is the same polynomial as above). The & symbol in Wolfram is used to create an anonymous function that takes one argument and returns #1^2 - 10^5 #1 + 1. The 1 argument tells Root to find the first root.

4. Root[#1^2 - 10^5 #1 + 1 &, 2]

This line follows the same principle as the previous one, but it tries to find the second root of the same polynomial.

5. Reduce[a x^2 + b x + c == 0, x]

This line simplifies a general quadratic equation a x^2 + b x + c == 0 by factoring out common terms and reducing it to its irreducible form. It does not explicitly solve for x.

6. Reduce[x^2 - 10^5 x + 1 == 0, x]

This line simplifies the specific quadratic equation x^2 - 10^5 x + 1 == 0 using the same rules as Reduce.

7. FindInstance[x^2 - 10^5 x + 1 == 0, x, Reals, 2]

This line tries to find two numerical approximations for the roots of x^2 - 10^5 x + 1 == 0 within the set of real numbers. The FindInstance function returns a list of approximate solutions.

8. FindRoot[x^2 - 10^5 x + 1 == 0, {x, 0}]

This line attempts to find a root of x^2 - 10^5 x + 1 == 0 starting with an initial guess of x = 0. FindRoot uses numerical methods to find a solution.

9. FindRoot[x^2 - 10^5 x + 1 == 0, {x, 10^6}]

Similar to the previous line, this one tries to find a root of the same equation but starts with a different initial guess of x = 10^6.

Source code in the wolfram programming language

Solve[a x^2 + b x + c == 0, x]
Solve[x^2 - 10^5 x + 1 == 0, x]
Root[#1^2 - 10^5 #1 + 1 &, 1]
Root[#1^2 - 10^5 #1 + 1 &, 2]
Reduce[a x^2 + b x + c == 0, x]
Reduce[x^2 - 10^5 x + 1 == 0, x]
FindInstance[x^2 - 10^5 x + 1 == 0, x, Reals, 2]
FindRoot[x^2 - 10^5 x + 1 == 0, {x, 0}]
FindRoot[x^2 - 10^5 x + 1 == 0, {x, 10^6}]


  

You may also check:How to resolve the algorithm Monte Carlo methods step by step in the Ursala programming language
You may also check:How to resolve the algorithm Faulhaber's triangle step by step in the jq programming language
You may also check:How to resolve the algorithm Random numbers step by step in the D programming language
You may also check:How to resolve the algorithm Memory allocation step by step in the E programming language
You may also check:How to resolve the algorithm CSV to HTML translation step by step in the Fortran programming language