Using Mathematica to solve quadratic equations :

Example 1

Remember that you must use Shift + Enter at the end of each line of mathematics .

In[1]:=

f[x_] = 3x^2 + 5x - 2

Out[1]=

-2 + 5 x + 3 x^2

To find exact solutions we try Solve .

In[2]:=

Solve[f[x] 0, x]

Out[2]=

{{x -2}, {x1/3}}

To find numerical (approximate) solutions we try NSolve .

In[3]:=

NSolve[f[x] 0, x]

Out[3]=

RowBox[{{, RowBox[{RowBox[{{, RowBox[{x, , RowBox[{-, 2.}]}], }}], ,, RowBox[{{, RowBox[{x, , 0.333333}], }}]}], }}]



Example 2

In[4]:=

g[x_] = x^2 - (10^6) x + 1

Out[4]=

1 - 1000000 x + x^2

In[5]:=

Solve[g[x] 0, x]

Out[5]=

{{x1/(500000 + 127 15500031^(1/2))}, {x500000 + 127 15500031^(1/2)}}

The following gives the numerical approximations fo r the preceding output .

In[7]:=

N[%]

Out[7]=

RowBox[{{, RowBox[{RowBox[{{, RowBox[{x, , 1.*10^-6}], }}], ,, RowBox[{{, RowBox[{x, , 1.*10^6}], }}]}], }}]

Of course, we can just use NSolve directly .

In[8]:=

NSolve[g[x] 0, x]

Out[8]=

RowBox[{{, RowBox[{RowBox[{{, RowBox[{x, , 1.*10^-6}], }}], ,, RowBox[{{, RowBox[{x, , 1.*10^6}], }}]}], }}]

Example 3Let ' s try to fool Mathematica with a coefficient of zero .

In[11]:=

h[x_] = 0x^2 + x + 2

Out[11]=

2 + x

Mathematica was not fooled .   It properly sees this as a linear equation, not a quadratic equation .

In[12]:=

Solve[h[x] 0, x]

Out[12]=

{{x -2}}

Example 4

In[14]:=

d[x_] = (10^30) x^2 + 3 * 10^30 * x + 2 * 10^30

Out[14]=

2000000000000000000000000000000 + 3000000000000000000000000000000 x + 1000000000000000000000000000000 x^2

In[15]:=

Solve[d[x] 0, x]

Out[15]=

{{x -2}, {x -1}}

So, Mathematica found the exact solutions, even when given the above huge coefficients .

In[16]:=

NSolve[d[x] 0, x]

Out[16]=

RowBox[{{, RowBox[{RowBox[{{, RowBox[{x, , RowBox[{-, 2.}]}], }}], ,, RowBox[{{, RowBox[{x, , RowBox[{-, 1.}]}], }}]}], }}]


Created by Mathematica  (January 31, 2004)