Вы находитесь на странице: 1из 2

MATH2070 Scientific Computing

Prac 1: Root Finder


Background
A root of a function is a value of the argument for which the function is zero. Thus roots are
solutions to the equation f (x) = 0.
An analytic procedure for finding the roots of the arbitrary quadratic ax2 + bx + c has been known
for over 1000 years. The roots are given by the well-known quadratic formula:

If the discriminant b2 - 4ac is positive, then the roots are real; otherwise they are complex
conjugates.
A procedure for arbitrary cubics ax3 + bx2 + cx + d was developed in the 1500s. The following
formulation is based on a technique published by Cardano in 1545:

where ω = (-1 + i√3)/2 is one of the cube roots of 1.


If the discriminant q2 + p3 is positive, there is a single real solution (given by x1) and 2 complex
conjugate solutions; otherwise there are 3 real solutions.

Preparation
In preparation for this prac, make sure you can log into your IST computer account and are
comfortable with basic Unix commands for manipulating files and directories (the Unix file
commands page in the HowTo module on FLO contains a quick introduction to the following
commands: ls, cd, mkdir, cp, rm)
You will probably find it convenient to organise all your topic files into a directory, with
subdirectories for each separate activity. For example, you could have a math2070 directory for
all of your work, and a roots subdirectory for this prac.
If you haven’t used MATLAB, Fortran, or C++ before have a look at the appropriate HowTo pages
on FLO. Suitable source files for the circle example are in the topic teaching space:
/opt/teaching/math2070/examples/circle

Flinders University / School of Informatics & Engineering 1


MATH2070 Scientific Computing

Task A
Write MATLAB code to find real roots.
1. Write MATLAB code to find real roots of quadratics (you can assume that the discriminate is
positive). Use the built-in sqrt function to find the square root. Test your code with the
following quadratics:
x2 - 3x + 2 roots at 1, 2
2x2 +2x - 10 roots at 1.79129, -2.79129
2. Write MATLAB code to find the real root of a cubic using Cardano’s method (assuming that
the discriminant is positive). To find a cube root, use an exponent of 1/3. Test your code with
the following cubics:
x3 - 3x2 + 7x - 5 root at 1
2x3 + 4x2 +10x - 5 root at 0.41626

Task B
Write Fortran and C++ code to find real roots.
3. Rewrite your quadratic code in Fortran and in C++. In C++ you’ll need to #include
<math.h> and also add the extra argument -lm to the compile line. In each case, test your
solution to make sure it works correctly.
4. Also rewrite your cubic code in Fortran and C++. Use the power function to compute the
cube root in C++.

Task C
Investigate what happens (and how to deal with it) if the discriminant is not positive.
5. What happens in MATLAB if the discriminant of a quadratic is not positive? Use the
following data to find out:
x2 - 2x + 5 roots at 1 + i2, 1 - i2
2x2 - 3x + 4 roots at 0.75 + i1.19896, 0.75 - i1.19896
6. What happens if you try the same thing with your Fortran and C++ quadratic code? Modify
your Fortran and C++ code to handle this case properly. Both languages know about complex
numbers, but you’ll need to decide which variables should be declared complex (in C++ you
need to #include <complex> and use the type complex<float>).
7. Extend your code (in all 3 languages) to compute all of the roots of cubics. Note that the
simplified Cardano’s method given above does not correctly handle the case of repeated roots
(which occur when u is zero).

Flinders University / School of Informatics & Engineering 2

Вам также может понравиться