|
|
CS 2073, Spring 2006
Program 4
Root of an Equation
Week 4: Feb 6-10
Due (on time):
2006-02-17 23:59:59
Due (late):
2006-02-20 23:59:59
|
Program 4 must be emailed to:
nrwagner@cs.utsa.edu
following directions for: running
and submitting a C program, with deadlines:
- 2006-02-17 23:59:59 (that's Friday, 17 February 2006, 11:59:59 pm)
for full credit.
- 2006-02-20 23:59:59 (that's Monday, 20 February 2006, 11:59:59 pm)
for 75% credit.
|
Introduction:
The objective of this programming assignment is to find
a real number x satisfying cos(x) = x, where
0 <= x <= pi/2, given in radians.
(All C trig functions use radians, rather than degrees.)
Here is a graph of the function y = cos(x):

Now draw the graph of the line y = x,
which is just a line through the origin making an angle of pi/4 (45 degrees).
These two will cross at a point with x-coordinate
between 0 and pi/2,
and this is the root we want.
It's interesting that this simple equation has no algebraic or analytic solution.
This means that there does not exist an expression for the root in terms of elementary
functions. The best we can do is to calculate an approximation to the solution.
This is perhaps surprising behavior,
since the equation sin(x) = x
has solution x = 0,
and the equation cos(x) = 0.5
has solution x = acos(0.5) = 1.047197551,
which is just 60 in degrees.
Two Different Programs:
You are to write two different root-finding
programs for finding this x value,
applying them to the equation
f(x) = cos(x) - x.
(A root is a place where f(x) = 0,
or in this case cos(x) - x = 0,
or cos(x) = x.)
First Program: Newton's method:
The first program will use Newton's
method, which is taught in calculus courses.
Don't worry if you forgotten your calculus, because you just
need to use the formulas given below.
A write-up about Newtons's Method: here.
For this method,
one also needs the derivative of f,
f'(x) = -sin(x) - 1. Newton's method
starts with a guess at the answer, call it x0,
and produces a new, hopefully better guess x1 using
the ``magic'' formula
x1 = x0 - f(x0)/f'(x0)
In this particular case the formula looks like:
x1 = x0 - (cos(x0) - x0)/(-sin(x0) - 1)
Replace the old guess with the new one and repeat
in a loop until the old guess and the new guess are within
some small value epsilon of one another.
(You should ask if the absolute value of their difference is
less than epsilon, using the function fabs.)
In this assignment we will take epsilon to be 0.000000000001 .
You should use the last new
guess as your final answer.
Second Program: the Bisection Method:
The second program will use the
bisection method. Here one starts with two values
x1 and x2
(with x1 < x2) for which f(x1) and f(x2)
have opposite signs (i.e., one is positive and
the other is negative, or f(x1) * f(x2) < 0).
If f is a continuous function
(no jumps or breaks), then there must be an x
between x1 and x2
for which f(x) = 0.
To get closer to that value, let
xmid = (x1 + x2)/2, the midpoint.
Consider f(xmid) and replace either x1 or x2
by xmid, so that after the replacement we
still have f(x1) and f(x2) with opposite signs,
but now x1 and x2 are half as far apart as they
were before.
Repeat this process until the distance between x1 and
x2 is less than epsilon, again taken to be 0.000000000001
in this assignment.
Your final answer should be xmid.
For the particular example here, you should take x1 = 0.0 ,
x2 = PI/2.0 , and
f(x) = cos(x) - x.
Directions for both programs:
Your programs set epsilon = 0.000000000001 for the
margin of error.
Then print "Tolerance value:" and the
value for epsilon.
Each program must count the iterations
and must terminate the loop
in case the iteration count gets larger than 50,
since it is easy to produce an infinite printing loop if you make a mistake.
(See the last example in the
Week 4 lectures for a way
to do this counter.)
The first program should print "Newton's method:"
and a short list of approximate values obtained at each iteration,
ending with a final approximation.
The second program should print "Bisection method"
and a somewhat longer list of pairs of values obtained
at each stage of
the second method, again ending with the final
approximation.
Notice that you should get (almost) the same answer by
the two methods.
All real numbers must be printed out with 15 digits to the
right of the decimal place.
What you should email:
Refer to the submissions directions and to
deadlines at the top of this page. The text file that you submit
should first have Your Name, the Course Number,
and the Program Number. The rest of the file
should have the following in it, in the order below, and clearly labeled,
including at the beginning the appropriate item letters:
a, b, c, etc.
|
Contents of email submission
for Program 4:
Last Name, First Name; Course Number; Program Number.
- C source for the first program that uses Newton's Method,
along with a run of this program.
- C source for the second program that uses the Bisection Method,
along with a run of this program.
|
Revision date: 2006-02-13.
(Please use ISO
8601, the International Standard.)