Docsity

Evaluating Non-Linear Functions - Algorithm Analysis and Design | CPSC 5115G, Study notes of Computer Science

Toggle sidebar
bg1
Evaluating Non–Linear Functions
Here we address the problem of evaluating a function for a given argument.
Consider the function F(X) to be evaluated at the value X = X0.
1. We assume that F(X) is a mapping from the real numbers into the real
numbers. The set of integers is here seen as a subset of the reals.
2, We assume that F(X) is defined at X = X0.
We do not attempt to evaluate expressions such as
1
.
3. We note that this can be expresses as finding the value Y0 such that
Y0 = F(X0), or F(X0) – Y0 = 0.
There are many similarities to solving non–linear equations. Thus, solving for
Y0 =
52.
is equivalent to solving the equation F(X) = X2 – 2.5 = 0.
We shall briefly discuss two main classes of functions.
1. Polynomial functions
2. Functions that are not polynomials.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Evaluating Non-Linear Functions - Algorithm Analysis and Design | CPSC 5115G and more Study notes Computer Science in PDF only on Docsity!

Evaluating Non–Linear Functions

Here we address the problem of evaluating a function for a given argument. Consider the function F(X) to be evaluated at the value X = X 0.

  1. We assume that F(X) is a mapping from the real numbers into the real numbers. The set of integers is here seen as a subset of the reals. 2, We assume that F(X) is defined at X = X 0. We do not attempt to evaluate expressions such as ^1.
  2. We note that this can be expresses as finding the value Y 0 such that Y 0 = F(X 0 ), or F(X 0 ) – Y 0 = 0. There are many similarities to solving non–linear equations. Thus, solving for Y 0 = 2.^5^ is equivalent to solving the equation F(X) = X 2 - 2.5 = 0. We shall briefly discuss two main classes of functions.
  3. Polynomial functions
  4. Functions that are not polynomials.

Evaluating Polynomials

The first topic to cover is that of polynomial functions. By this, we mean a standard polynomial with a finite number of terms. A polynomial of degree N over the argument X is defined to be of the form F(X) = ANX N

  • AN-1X N-
  • ... + A 2 X 2
  • A 1 X + A 0 , N  0 and AN  0. The only real question in evaluating such a polynomial is the selection of an efficient method. The best method is called “Horner’s Rule”. ALGORITHM HORNER (A[0..N], X0) // THE COEFFICIENTS ARE IN THE ARRAY A[0..N] P = A[N] THE COEFFICIENT OF X N . FOR K = (N - 1) DOWN TO 0 DO P = P * X + A[K] END DO RETURN P // THE VALUE OF F(X) AT X = X0. NOTE: This algorithm uses only the basic operations (addition and multiplication) that are available on any standard computer.

Functions That Are Not Polynomials

Here we consider a subset of the non–linear functions that are not polynomials. The method to be discussed next applies to those real functions of real numbers that are both continuous and analytic in the mathematical sense. In general, none of these functions can be directly evaluated using only the primitive operations available on a stored–program computer. What are the basic mathematical operations available on a modern computer? There are only four: addition, subtraction, multiplication, and division. Some modern computers do not have direct implementations of division, but rather support an inverse function; Y/X = Y * (1/X). The functions that we now discuss can be represented as infinite series. Each term in the infinite series can be evaluated using only multiplication and division. The terms can be combined with addition and subtraction. The primary issue here is termination. How does the algorithm terminate? For polynomials, we just evaluate all of the terms. This is not an option here.

Four Sample Functions

As examples, let us discuss the following functions Trigonometric functions: Sine and cosine Exponential functions: E X and E –X . Logarithmic functions: The natural logarithm. The methods used are based on series expansions obtained from calculus. Sin(X) = X – (X 3 /3!) + (X 5 /5!) – (X 7 /7!) + (X 9 /9!) – … = X – (X 3 /6) + (X 5 /120) – (X 7 /5040) + (X 9 /362,880) – … Cos(X) = 1 – (X 2 /2!) + (X 4 /4!) – (X 6 /6!) + (X 8 /8!) – … = 1 – (X 2 /2) + (X 4 /24) – (X 6 /720) + (X 8 /40320) – … Exp(X) = 1 + X + (X 2 /2!) + (X 3 /3!) + (X 4 /4!) + (X 5 /5!) + … Exp(–X) = 1 – X + (X 2 /2!) – (X 3 /3!) + (X 4 /4!) – (X 5 /5!) + … Ln(1 + X) = X – (X 2 / 2) + (X 3 / 3) – (X 4 / 4) + (X 5 / 5) – , –1 < X  1. But note that each series is infinite. How do we know how many terms to evaluate to get the precision we need?

Preliminary Assessment

Each term in the series expansion of sine, cosine, and exponential is of the form X N / N!. Let us first consider the trigonometric functions.

Given the constraint that 0  X   / 2 , we see that the maximum error in stopping

the series before the term (X N

/ N!) is ( / 2 )

N / N!

We want to have ( / 2 )

N

/ N! <  = (1/

24 ). How many terms do we need?

Here is a rough calculation. Recall that  < 4, so  / 2 < 2.

We solve the inequality ( / 2 )

N / N! < 2 N / N! < (1/ 24 ), which becomes 2 (N+24) < N!. I have to do this by inspection (trial and error) N = 20 2 44  1.756 10 13 20! = 2.433 10 18 N is too big. N = 12 2 36  6.872 10 10 12! = 4.790 10 8 N is too small N = 16 2 40  1.100 10 12 16! = 2.093 10 13 N is too big N = 14 2 38  2.749 10 11 14! = 8.718 10 10 N is too small N = 15 2 39  5.498 10 11 15! = 1.308 10 11 N is too small The choice is the 16 th power, it is the first value of N for which 2 (N+24) < N!.

The Trigonometric Functions

As a result of the above calculations, we have concluded that the following series expansions suffice for the sine and cosine under the assumption of single precision. Sin(X) = X – (X 3 /3!) + (X 5 /5!) – (X 7 /7!)

  • (X 9 /9!) – (X 11 /11!) + (X 13 /13!) – (X 15 /15!) +… Cos(X) = 1 – (X 2 /2!) + (X 4 /4!) – (X 6 /6!)
  • (X 8 /8!) – (X 10 /10!) + (X 12 /12!) – (X 14 /14!) +… Of course, it is very likely that small values of X might lead to the inequality (X N

/ N!) <  = (1/

24 )  5.96 10

  • before all 8 terms are evaluated. It that case, the evaluation can be terminated early. There is an entire mathematical theory devoted to placing an upper bound on the errors generated by approximation by a finite series. This theory is the origin of the “Big O” notation that we used to express algorithmic complexity.
  • The exact value is 1 /