CMSC201
Programming
Project Two
Taylor series
out Wednesday 9/25/96
due Wednesday 10/09/96
at Midnight
|
Brook Taylor
|
Introduction
How can we calculate values for common functions like the
trigonometric functions sine and cosine? In
the early 1700's mathematicians such as Brook
Taylor developed techniques in which the value of a function like
sin(x) could be expressed as the sum of an infinte series of
smaller and smaller terms. This provided a useful way of
approximating the value of such functions by calculating the sum of an
initial number of these terms. The more terms that were used in the
calculation, the better the approximation.
Any function f(x) can be expressed as a polynomial in x (which
is an "infinite series") using a popular method called the Taylor
expansion. This is an important technique and the knowledge of it has
saved the life of at least one famous scientist, as the newspaper item
"Taylor Series - a matter of life or
death" attests.
Taylor's Series for any function f(x) expanded around a point
x=a can be expressed as:
f(x) = f(a) (1)
+ (x-a)*f'(a)/1!
+ (x-a)2*f''(a)/2!
+ (x-a)3*f'''(a)/3!
+ ...
where:
- f(a) is the value of the function at x=a.
- f'(a) is the value of its first derivative of F at point
a.
- f''(a) is the value of its second derivative, and so on.
- n! (read as n factorial) is a symbolic representation for
the product of all natural numbers from 1 to n. In other words,
You should note that the f(x) expansion in (1) is an infinite
series, the nth term of the series is:
(x-a)n*fn(a)/n!, where fn(a) is the nth
derivative of f(x) evaluated at x=a.
Each successive term in the infinite series gets smaller and smaller.
We can use this method to compute an approximate value of a function
at a point x by calculating a certain number of terms, say the
first ten, and summing them. The more terms we add up, the more
accurate our approximation.
The task
For your second project assignment, write a program that uses the
first five terms of a Taylor series expansion to compute values of the
trigonometric functions sine and cosine at a given
point. Since all these functions sin(x), cos(x) and
their derivatives are defined at 0, we can choose a=0 in the Taylor's
series. Using (1), the first five terms of the Taylor series expansion
for these two functions expanded around x = 0 are:
sin(x) = x - x3/3! + x5/5! - x7/7! + x9/9! (2)
cos(x) = 1 - x2/2! + x4/4! - x6/6! + x8/8! (3)
Your program should begin by asking the use to specify a whether he
wants to compute the sine or cosine function and the point as a
floating point number (in radians) at which it should be evaluated.
Here is a sample output session in which the user's input is shown in
italics.
% taylor
Function? sine
** You must enter one of: sin cos
Function? sin
Point (in radians)? 16.0
For sin(16.0) the exact value is S.SSSSS and the five term Taylor
expansion is I.IIIII
terms in Nth Approx-
expansion term imation Error
--------------------------------------
1 t.ttttt a.aaaaa b.bbbbb
2 u.uuuuu c.ccccc d.ddddd
3 v.vvvvv e.eeeee f.fffff
4 w.wwwww g.ggggg h.hhhhh
5 x.xxxxx i.iiiii j.jjjjj
Notes
- The Approximation column gives the successive
approximations using one term, two terms, three terms, etc. The
Error column is just difference between the "exact" value of
the function and the approximated value for the given number of terms.
- The "exact" value of sin of x or cos of x can be evaluated
using the standard 'C' functions sin(x) and cos(x). Here
is an example of a fragment of code to calculate the values of
sin(10) and cos(10):
main()
{
int x = 10;
float y,z;
y = sin(x);
z = cos(x);
printf("sin(%d) = %f, cos(%d) = %f \n",x,y,x,z);
}
To compute xn, you could use the standard C
function, pow(x, n). But for this you must include the following:
#include "math.h"
The function should be entered as a string and the point as a
floating point number. Be sure that you program handles bad input data
gracefully.
You don't have to fully understand the mathematics involved to
successfully complete this assignment. Your job is mainly to express
the formulas (1) and (2) as a C program.
Check this web page a few days before the assignment is due to
make sure there are no minor modifications to what you have to do.
What to hand in
Use the submit program to submit a copy of your source code. We will
not look kindly on people who are unable to successfully use submit for
this second assignment.