0.0 Remarks
To understand the material, a good working knowledge of calculus, in particular Taylor series, is a great advantage. The same is true, although mostly for the later part of the semester, for matrix algebra.
The text is color coded. The normal text color is black, while Python programs are in red, and proofs of some facts that are mostly for the curious and will definitely not appear in the exams are in blue. Chapter 0 is largely a programming exercise, while Chapter 1 provides a short excursion into some techniques of Numerical Analysis and their pitfalls together with a review of some results of Calculus.
Numerical Analysis deals with finding approximate values for numbers that cannot, or cannot reasonably be computed accurately. For example, while √4 2 accurately, there is no number we can write down more explicitly for √2 except for that symbol itself. We know that approximately √2 1.414. This is of course not a correct equality as 1.414 1.999396. For many practical purposes this is good enough, but not for all. We can derive a true statement about this approximation by giving an estimate for what we will call the absolute error
√2 1.414.
Using the third binomial formula we can derive
and
∗ √2 1.414 2 1.999396 0.000604
|| 0.000604 . √2 1.414
Now √2 1.414 and therefore √2 1.414 2 ∗ 1.414 2.828, thus || 0.000604 0.00022.
We will later come back to this and introduce on additional kind of error, studying how errors are generated and how they propagate through mathematical operations.
2.828
1
As most such calculations are now done by computers, we begin with a little programming exercise.
0.1 A Programming Exercise
We write a program that plots a given relatively simple function.
import numpy as np; import pylab as pl
def f(x): y=x**5-3*x**4+x**3+10*x-7 return y
N=200 a=0 b=1
x=np.linspace(a,b,N+1) y=np.linspace(a,b,N+1)
for k in range (0,N+1): y[k]=f(x[k])
pl.grid(b=True) pl.plot(x,y)
The program plots the function f(x) from a to b by computing it at 200 equidistant points and then connecting the dots by straight lines. Below is the output of the program.
2
This gives us the possibility of using the computer as a graphing calculator. Now we review a little bit of Calculus and use it to compute an approximate value for a function.
Homework Problem Set 1
Play around with the plotting program and submit plots of
1. 2. 3. 4.
cos(x) for 0
7
k=k+1 b=-b*x**2/(2*k) e=np.abs(b)/(2*k+1)
y=y/c y=y+.5
print(y)
Now we want to define a function for this calculation
import numpy as np x=5.0
def F(x): d=.0000000000001
c=np.sqrt(8.0* np.arctan(1.0));d=d*c;e=2*d b=x;y=0;k=0
while e>d: a=b/(2*k+1)
y=y+a
k=k+1 b=-b*x**2/(2*k) e=np.abs(b)/(2*k+1)
8
y=y/c y=y+.5 return y
print(F(x))
As the next step we want to plot this function, just inserting F into our previous plotting program. Note that a variable name used in a function can be reused elsewhere.
import numpy as np;import pylab as pl def F(x):
d=.0000000000001
c=np.sqrt(8.0* np.arctan(1.0));d=d*c;e=2*d; b=x;y=0;k=0 while e>d:
a=b/(2*k+1) y=y+a;k=k+1 b=-b*x**2/(2*k) e=np.abs(b)/(2*k+1)
y=y/c;y=y+.5 return y
N=200 a=-6 b=6
9
x=np.linspace(a,b,N+1);y=np.linspace(a,b,N+1) for k in range (0,N+1):
y[k]=F(x[k])
pl.grid(b=True) pl.plot(x,y)
With a and b as above the plot looks like this
If instead we change a to -8.9 and b to 8.9, our plot becomes
10
This can of course not be correct. What is worse, for a=-10 and b=10 we get
Upon inspection we see that if we add the positive and negative expressions in the sum separately, then for large x both are very large. We can modify our first program and make it show us these two numbers. The output for both is
11
1.0448680833928798e+20, -1.0448680833928797e+20
for the difference 16384.5, which is better than what we would have any right to expect on the basis of the first result, but still quite far from what we can immediately say based on the fact that F is a distribution function, therefore always in 0,1.
This phenomenon is called loss of significant digits.
Homework Problem Set 3
1. Does √ have a linear Taylor polynomial approximation at 0? How about 1?
2. Use the error terms for the Taylor polynomial of degree 1 and 2 to estimate the error of these polynomials for the function for 0. Any observations?
3. Defineafunctionbyfor0and00.FindtheTaylor
polynomial of degree three for this function and give a good estimate for the error for 0 1.
12