Professional Documents
Culture Documents
General philosophy
In this class we will derive algorithms (this is the easy part), we will prove rigorously that these algo-
rithms converge to the right answer (this is harder) and we will study rigorously how fast they converge to
the right answer (this is also hard). A good background in calculus is needed: we will use again and
again tools such as Taylor’s theorem in order to derive error estimates.
There will be lots of coding too. The homework will be computationally intensive. You will be asked to
write codes during the midterms and the final exam (with pen and paper, not with a computer).
You will be taught how to use MATLAB and SCILAB (they are basically the same language). Previous
familiarity with these packages is NOT REQUIRED for enrollment. The first dicussion will be a
crash course in MATLAB/SCILAB. Don’t miss it!
Your homework must be written in MATLAB or SCILAB. During exams you will be asked to write
some MATLAB/SCILAB codes.
1
Grading Scheme:
Homework: 8%
2 Midterms: 46% (23% each)
Final: 46%
Homework:
There will be about 8 homework (≈ 1% each).
All numerical computation should be done in MATLAB/SCILAB.
No late homework will be accepted.
Office hours:
To be determined
2
Homework # 1
NOTE: Each time you are ask to write a code which gives the solution with accuracy , it means that you
have to use the stopping criterium
|pn − pn−1 | ≤
Note that this does not guaranty that |pn − p| ≤ !!! So you are actually not sure to have found the solution
with accuracy !!!
(f) What can you conclude about using a fixed point iteration.
(g) Find the fixed point with accuracy 10−3 . How many iterations were needed? (provide the MAT-
LAB code)
(h) Use the theoretical result given in class (|p − pn | ≤ k n (b − a)) to estimate the number of iterations
required to achieve 10−3 accuracy. Why is this number different than the one obtained in g). ?
5. Let f (x) = e−x
1
(a) Sketch the graph of f (x).
(b) Find f ([ 13 , 1])
(c) Is it true that f ([ 13 , 1]) ⊂ [ 13 , 1]? What can you conclude about the existence of a fixed point?
(d) Find maxx∈[ 13 ,1] |f 0 (x)|.
(e) Find a number k ∈ (0, 1) such that
1
|f 0 (x)| ≤ k for all x ∈ ,1
3
(f) What can you conclude about using a fixed point iteration.
(g) Find the fixed point with accuracy 10−3 . How many iterations were needed? (provide the MAT-
LAB code)
(h) Use the theoretical result given in class (|p − pn | ≤ k n (b − a)) to estimate the number of iterations
required to achieve 10−3 accuracy.
2
Homework # 2
(Hint: Before to do any coding, plot the function with matlab so that you can see roughly where
is the zero.)
2. Repeat problem 1., but this time with the equation
1
ex =
0.1 + x2
3. Newton’s method is the commonly used method for calculating square roots on a computer.
√
(a) What equation would you solve in order to find a?
(b) Show that in this case, Newton’s method reduces to the following fixed point iteration:
1 a
pn+1 = pn + (1)
2 pn
(c) Explain with a picture why, for any initial iterate p0 > 0, Newton’s method will converge (I am
not looking for a precise answer: just draw a picture and write something short).
√
(d) Use iteration (1) to find 57 with accuracy 10−10 . Start with the initial iterate p0 = 57. How
many iterates were needed?
4. In class we have proven that Newton method converges by viewing it as a fixed point
iteration. In this problem, you will prove it in a different way, without using a fixed point
iteration.
1
(a) Use Taylor’s theorem to show that there exist a numbers ξn between p and pn such that
f 00 (ξn )
p − pn+1 = −(p − pn )2 (2)
2f 0 (pn )
(b) Explain why it is possible to pick a (small) interval I = [p − δ, p + δ] on which f 0 (x) 6= 0.
(c) Since f 0 (x) 6= 0 on I, then minx∈I |f 0 (x)| =
6 0 and therefore the number
maxx∈I |f 00 (x)|
M= .
2 minx∈I |f 0 (x)|
is well defined. Show that if |pn − p| ≤ δ, then
2
|p − pn+1 | ≤ M |p − pn | (3)
and therefore, multiplying both side by M :
2
M |p − pn+1 | ≤ (M |p − pn |) (4)
Hint for (d)ii): Note that, because of (6), M |p − p0 | < 1 and therefore, using (3), we get
|p − p1 | ≤ (M |p − p0 |) |p − p0 | ≤ |p − p0 |
Then you are almost done...
2
Homework #3
1. We have seen in class that, if f (x) has a root of multiplicity m ≥ 2, then Newton’s method converges
only with order 1. Here is a modified version of Newton’s method:
f (pn )
pn+1 = pn − m
f 0 (pn )
2. Let f (x) = ex − x − 1
(a) Plot f (x) with matlab. Where is the zero (just guess with the picture)?
(b) Show that the zero is actually where you thought it was, and show that it has multiplicity 2.
(c) How many iterations are needed with Newton’s method to find this zero with accuracy 10−10 ?
(start with p0 = 1)
(d) How many iterations are needed with the modified version of Newton’s method that we have seen
in problem 1.?
3. Prove by induction that (11111 . . . 1)2 = 2n − 1 (the binary number contain n digits, all are equal to
one).
1
Homework # 4
1. (a) Use Lagrange formula to find the unique polynomial of degree ≤ 2 which passes through the three
points (0,1), (-1,2) and (1,3).
(b) Do some algebra to write the polynomial as follow:
p(x) = a2 x2 + a1 x + a0
2. Consider the function f (x) = e−x . We are going to approximate it with a polynomial of degree 2 and
we are going to study how good is this approximation.
(a) Find a polynomial p2 (x) = a2 x2 +a1 x+a0 which approximate f (x) on the interval [−1, 1]. Choose
your polynomial so that p2 (−1) = f (−1), p2 (0) = f (0) and p2 (1) = f (1). Use Lagrange formula
and then do some algebra to find a1 , a2 and a3 .
(b) Plot both f and p2 on matlab on the interval let say [−2, 2].
(c) Write a matlab code to find the maximum distance between the line representing the polynomial
and the line representing the function f on the interval [−1, 1]. In other words, define the error
by E(x) = |f (x) − p2 (x)| and find the maximum value of E on the interval [−1, 1] using your
matlab code.
max E(x) =
x∈[0,1]
(d) Use the error formula that we derived in class to get an upper bound on the error on the interval
[−1, 1] of the type
E(x) ≤ C|(x − a)(x − b)(x − c)| for all x ∈ [−1, 1]
I want numerical values for C, a,b and c. I want the best C possible.
(e) Make a sketch of the function (x − a)(x − b)(x − c) from the previous question. Find analytically
where is its max on [−1, 1] and what is this max. Then write
E(x) ≤ M for all x ∈ [−1, 1]
I want a numerical value for M .
(f) Does c) and e) agree with one another?
(g) Use Taylor’s theorem to find the best polynomial of degree 2 which approximate f (x) around
x = 0. This means that the polynomial and f must have the same value at x = 0 and their
derivatives must match up to order 2 at x = 0. Estimate the error on the interval [−1, 1] using
the error formula coming from Taylor’s theorem:
E(x) ≤ C(x − a)3 for all x ∈ [−1, 1]
I want numerical values for C and a. I want the best possible C. Then Use this to find a bound
for the error on the interval [−1, 1]
E(x) ≤ M for all x ∈ [−1, 1]
I want a numerical value for M .
(h) Which of the two polynomials do the the best approximation? The one with one node or the one
with three nodes?
1
Homework # 5
Your proof has to be more detailed than the one I did in class. Be more precise on how to use
the intermediate value theorem in order to conclude the proof.
2. In order to derive Simpson’s rule, we proved that, if p(x) is the unique polynomial of degree ≤ 2 which
interpolates the function f (x) at x0 , x1 and x2 , then
Z x2
h
p(x)dx = (f (x0 ) + 4f (x1 ) + f (x2 )) where h = x1 − x0
x0 3
In this problem we will do the same for the trapezoidal rule. Let p(x) be the unique polynomial of
degree ≤ 1 which interpolate the function f (x) at x0 and x1 . Prove that:
Z x1
h
p(x)dx = (f (x0 ) + f (x1 )) where h = x1 − x0
x0 2
Remark: In class we derived this result by drawing a picture and calculating the area of a trapezoid.
Here I am asking you to derive this result in a more rigourous way: First use Lagrange’s formula and
then integrate the polynomial that you obtained.
3. While proving that the error in the trapezoidal rule is O(h2 ), we used the fact that:
x4 3
(x4 − x3 )
Z
1 3
x4 − 3x24 x3 + 3x4 x23 − x33 = −
(x − x3 )(x − x4 )dx = −
x3 6 6
1
R1
(d) Use the trapezoidal rule to compute 0 ex dx with the number of node points from the previous
question. What is the actual error? Smaller or bigger than 10−8 ? How can you explain this?
Provide your matlab code.
(e) Repeat c) and d) but with Simpson’s rule. Compare the number of node points needed for this
two method in order to reach a 10−8 precision
2
Homework #6
R1
1. Let InT (ex ) be the approximation of 0 ex dx that you get by using the trapezoidal rule with n + 1 node
R1
points (x0 , x1 , . . . , xn ). Similarly, let InS (ex ) be the approximation of 0 ex dx that you get by using
Simpsons rule with n + 1 node points. Define the errors:
Z 1 Z 1
T x T x S x S x
En = e dx − In (e ) En =
e dx − In (e )
0 0
(a) Write a program which compute EnT and EnS for n = 10, 110, 210, 310, 410, . . . , 2010 and then plot
log EnT versus log n and log EnS versus log n. Use the command plot(, ,x) so that you just see the
data points. You should obtain lines.
(b) What are the equations of these lines (you can use the matlab command basic fitting and do a
linear fitting. You will find this command in “Tools”, just above your plot.)
(c) Why are the slopes −2 and −4? How can you explain this with the error formula we have derive
in class.
R1
2. In the previous problem we computed 0 ex dx with the trapezoid rule and Simpsons rule. It was not
very usefull because this integral could be computed by hand. But it R 1was 2good because it allowed
us to study numerically the error. In this problem you will compute 0 e−x dx with Simpson’s rule.
Your answer should be accurate up to 7 digits after the point (i.e: the 7 first digits after the point
should be exact). Note that this integral can not be computed analitically. In this case we really need
a computer.
(a) How many nodes points are necessary in order to be sure that
the first
seven digits after the 2point
are correct? (hint: using MAPLE, I found that maxx∈[0,1] f (4) (x) = 12, where f (x) = e−x ).
(b) Do the computation with this number of node points.
R1
3. Since Simpsons rule has degree of precision 3, if you compute 0 x3 dx with Simpsons rule, then the
R1 3
error should be exactly zero. Use Simpson rule to compute 0 x dx with n = 100. What is
Z 1
x3 dx − In[0,1] (x3 )?
0
How can you explain this difference between the theory and the actual computation?
4. (a) What system of nonlinear equations would you solve in order to find w1 , w2 , w3 , x1 , x2 and x3
such that the integration formula
1
(a) Sketch or plot with matlab f (x) on [−1, 1].
R1
(b) Compute −1 f (x)dx by hand.
R1
(c) Write a code to compute −1 f (x)dx using Gaussian Quadrature with 7 nodes points x1 , x2 , . . . , x7
(hint: store the nodes and weights of the table in two vectors “node(i)” and “w(i)”). What is the
error?
R1
(d) Write a code to compute −1 f (x)dx using Simpson rule with nodes points x0 , x1 , . . . , x6 (so that
you have a total of 7 nodes points, as in question (b)). What is the error?
(e) Which method work the best?
2
Homework #7
(a) Write a program which compute EF (h) for h = 10−1 , 10−2 , . . . , 10−5 and then plot log10 (EF (h))
versus log10 (h). Use the command plot(, ,’x’) so that you just see the data points. Do the same
thing with ES (h).
Remark: This time we use log10 (log base 10) instead of log. The reason for doing this is
simply because the plot you obtain is more readable. For example, with the symmetric difference,
you should obtain a point whose coordinates is something like (-3,-6). This means that, when
h = 10−3 , the error is roughly 10−6 (which of course is what you expect since the symmetric
difference is O(h2 ))
(b) What are the equations of these lines ( you can use the matlab command “basic fitting” and do
a linear fitting. You will find this command in “Tools”, just above your plot.)
(c) Why are the slopes 1 and 2? (We know from the theory that EF (h) ≤ M21 h and ES (h) ≤ M62 h2 .
In this question, you can assume that there exists constant C1 and C2 such that EF (h) = C1 h
and ES (h) = C2 h2 .)
(d) Repeat question (a), but this time with h = 10−1 , 10−2 , . . . , 10−10 . In class we did study the
effect of the finite precision of the computer when doing a symmetric difference. Does the log-log
plot that you obtain for the symmetric difference roughly match this very rough analysis?
(e) Study the effect of the finite precision of the computer when doing a forward difference (just mimic
what we did in class with the symmetric difference). From this analysis, what is the optimal h
that one should use? What is the best accuracy that one can get? Does this match what you
observe on your log-log plot?
1
Homework #8
For example,
−2 1 0 0 0 0 3
1 −2 1 0 0 0
3
0 1 −2 1 0 0 3
A6 = A6 =
0 0 1 −2 1 0
3
0 0 0 1 −2 1 3
0 0 0 0 1 −2 3
The goal of this homework will be to solve An x = bn for large n and study how
long it takes for the computer to do so.
Remark : The matrix An occurs, for example, when one want to solve the heat equation on a rode. The
solution of Ax = b will be the heat distribution on the rode: x(i) will be the temperature at the node
point xi = ih. Typically when dealing with this type of problem, n is very large, that is why we need good
algorithms. And, as I said in class, Gaussian elimination is a very bad one. For this specific problem, in
practice, people use algorithm that use the fact that “most” of the matrix An is filled with zeros.
1
If you did everything correctly, you just have to type in the matlab window:
init
fact
rhs
backsub
Check your answer using the “backslash” matlab command: if you want matlab to solve Ax = b,
you need to type:
(Here we count only the multiplications and divisions; the number of additions and subtractions are
the same.) So of course we expect fact.m to take much more time than rhs.m and backsub.m. Use
the command “tic” and “toc” of matlab to find the computing time for each of the three parts of your
code (write tic at the beginning of each code, and toc at the end).
6. We will now solve A1000 x = b1000 . Note that n is twice larger than in the previous question. The
number of operations performed by fact.m should be how many times larger? Use the tic and toc
command of matlab to see if you get a computing time which match the theory.
7. Solve A1000 x = b1000 . Use the command tic and toc to find the computing time for each of the three
parts of your code. Use the backslash command of matlab to check that x(1) and x(100) are correct.
8. Solve A2000 x = b2000 . How long does it take? Then solve A2000 x = c2000 where
−3
3
−3
c= 3
..
.
−3
3
Since you have already solve A2000 x = b2000 , it should be very quick to solve A2000 x = c2000 : you just
need to use rhs.m and backsub.m. How long does it take?
2
Math 135A
October 4, 2010
Homework 1
1. Plot
(a) (Plot in Wolframalpha.com)
Zoomed in
Estimate for zero: x=0.6412
(b) function [ a, b, k ]=hw1b(f, a, b, er)
f=inline(f); %f is a string of characters from the input on the command line
if (f(a)*f(b) >0)||(a >= b) error('Does not satisfy conditions for bisection method'); end
%k is iterations
%a is left point
%b is right point
%x(k) is midpoint
k=1; x(k)=(b+a)/2;
while(abs(b-a)/2.>er && f(a)*f(b)<0) if(f(x(k))*f(a)<0) b=x(k);
else a=x(k);
end k=k+1;
x(k)=(b+a)/2;
end
After running the code: [a, b, k]=hw1b('x-2.^-x', -5, 5, 10.^-5), we get 0.6412 after 20 iterations
(c) Solving analytically we have,
b−a
10−5 =
2n+1
10
10−5 = n+1
2
solving for n yields 18.9316, which is close to 20.
2. Plot
(a)
1
(b) 4.4934
Matlab code
function [ a, b, k ]=hw1b(f, a, b, er) f=inline(f); if (f(a)*f(b) >0)||(a >= b) error('Does not satisfy
conditions for bisection method'); end k=1; x(k)=(b+a)/2;
(c) a − f (a) < 0 and b − f (b) > 0 we can call this new function g(x) = x − f (x), by applying the Intermediate
Value theorem, there exists a number p between a and b such that g(P ) = 0, thus f (P ) = p which is the
xed point. Q.E.D.
3. plot
(a)
(b) f (0) = ln(2) > 0, f (2) = ln(4) < 2,
(c) Yes, it is true, which implies that there exists a xed point on [0, 2]
(d) Maximum is 12
(e) 1
2
(f) We can use xed point iteration to nd the convergence
(g) 1.1445 to 1.1465, 11 iterations (used same code as above)
(h) 10.9658,
4. plot
(a)
(b) f ([ 13 , 1]) ≈ [0.71653, 0.36787]
(c) It is true, there exists a xed point on [ 13 , 1].
2
(d) The max is 1/3
(e) k=1/3
(f) Fixed point iteration allows us to see whether e−x diverges or converges
(g) between 0.5664 and 0.5684, 11 iterations
[a, b, k]=hw1b('exp(-x)-x',0,2,10^-3)
(h) number of iterations 5.918
3
Math 135A
October 11, 2010
Homework 2
1. Plot
(a) x3 = x2 + x + 1
Bisection Method
1.8393, 21 iterations for 10−5 accuracy, interval was chosen via chart between 1 and 2
4
(b) Newtons: 0.6498 5 iterations
(c) Secant: 0.6498, 7 iterations
5
(b) Since the function is continuous, we can pick a δ neighborhood in which the function isn't changing
direction, thus f 0 (x) 6= 0 holds true for that neighborhood.
(c) We know that |pn −p| > |pn+1 −p|, since after every iteration, the sequence is converging, and the distance
00
from p gets smaller and smaller. Since |pn − p| < δ this implies (using p − pn+1 = −(p − pn ) 2ff 0 (p(ξ)n )
therefore
M |p − pn+1 | ≤ (M |p − pn |)2
6
Math 135A
October 18, 2010
Homework 3
1. Let
f (x)
g(x) = x − m
f 0 (x)
By proving that g 0 (p) = 0,we prove that pn+1 converges with order 2. So we plug in
f (x) = (x − p)m h(x) where h(p) 6= 0
which simplies to
m h(x)(x − p)
g(x) = x −
m h(x) + h0 (x)(x − p)
then the derivative is
m h(x) d m h(x)
g 0 (x) = 1 − 0
+ (x − p)
m h(x) + h (x)(x − p) dx m h(x) + h0 (x)(x − p)
which simplies to
m h(p)
g 0 (p) = 1 − =0
m h(p)
Thus it converges with order 2. Q.E.D.
2. Plot
(a) Here's the graph
7
The zero seems to be at x = 0
(b) f (x = 0) = e0 − 0 − 1 = 1 − 0 − 1 = 0 thus f (0) = 0
f ex − x − 1 x
0
= x
=1− x
f e −1 e −1
f 0 (0) = f (0) = 0
f 00 (0) = 1 6= 0, thus it has convergence of order 2
(c) 28 iterations
(d) 5 iterations
3. So
(1)2 = 1 × 20 = 21 − 1
(11)2 = 1 × 21 + 1 × 20 = 22 − 1
(111)2 = 1 × 22 + 1 × 21 + 1 × 20 = 23 − 1
Therefore
(111...1)2 = 2n − 1
From a more generalized way of induction, assume the above is true
1(111...1)2 = 2n−1 + 2n = 2n+1 − 1
8
thus m = 41
Spacing
20 − 21
252
thus m = 52
5. -
(a)
11
−1−1023 1
(−1)0 22 (1 − 252 ) = 3.59 × 10308
(b) −3.59 × 10308
(c) −21075
(d) 10000011100
9
Math 135A
10/29/2010
Homework 4
1. Lagrange formula
(a) for (0,1) (-1, 2) and (1,3) P (x) = y0 l0 (x) + y1 l1 (x) + y2 l2 (x) + y3 l3 (x), plugging in
(x − −1)(x − 1) (x + 1)(x − 1)
l0 (x) = =−
(0 − −1)(−1 − 1) 2
(x − 0)(x − 1) x(x − 1)
l1 (x) = =
(−1 − 0)(−1 − 1) 2
(x − 0)(x − −1) x(x + 1)
l2 (x) = =
(1 − 0)(1 − −1) 2
thus
(x + 1)(x − 1) 3
P (x) = − + x(x − 1) + x(x + 1)
2 2
(b) Rewriting
1
4x2 + x + 1
P (x) =
2
2. f (x) = e−x
(a) (−1, e), (0, 1), (1, e−1 )
Using Lagrange's formula
(x − 0)(x − 1) x(x − 1)
l0 (x) = =
(−1 − 0)(−1 − 1) 2
(x − −1)(x − 1)
l1 (x) = = −(x − 1)(x + 1)
(0 − −1)(0 − 1)
(x − −1)(x − 0) x(x + 1)
l2 (x) = =
(1 − −1)(1 − 0) 2
So
e x(x + 1)
P (x) = x(x − 1) − (x − 1)(x + 1) +
2 2e
which simplies to
(e − 1)2 2 (e2 − 1)
x − x+1
2e 2e
(b) Plotting it we have,
10
(c) 0.55169254
Matlab code used:
f = @(x)-1*(exp(-x)-((exp(1)-1)^2/(2*exp(1))*x.^2-(exp(2)-1)/(2*exp(1))*x+1))
x=fminbnd(f,-1,1)
The output was 0.5517, thus
max E(x) = 0.5517
x∈[0, 1]
(d) error
f (n+1) (ξ(x))
Error = (x − x0 )...(x − xn )
(n + 1)!
f 000 (ξ(x))
= (x − x0 )(x − x1 )(x − x2 )
3!
f 000 (ξ(x))
= (x − 0)(x − −1)(x − 1)
3!
f 000 (ξ(x))
= x(x + 1)(x − 1)
3!
we know that
f 000 = −e−x
max occurs at -1 e+1 =2.71828
2.71828
error = x(x + 1)(x − 1)
6
(e) Sketch
(f) Error is close, but not quite the same, they agree with one another
(g) Taylor series approximation around f (x)
x2 ξ3
1−x+ −
2! 6
error formula from taylors theorem, we use the Lagrange form of the remainder term
f n+1 (ξ)
(x − a)n+1
(n + 1)!
since a = 0 and n = 2
f 3 (ξ)
(x − a)3
3!
11
Thus
1 3
C= f (ξ)
6
using ξ = −1, we have
C = e/6 = 0.45304
The error is 0.45304(x) since x=0
(h) The best approximation is the one with three nodes
12
Math 135A
11/5/2010
Homework 5
1. -
(a) Intermediate Value Theorem: If f is a continuous real-valued function on the interval [a, b] and h is a
number between f (a) and f (b) then there exists a c ∈ [a, b] such that f (c) = h.
(b) w is a continuos real-valued function on the interval of integration [a, b], thus there exists a θ ∈ [a, b]
such that f (θ) is between f (a) and f (b). THus
m ≤ g(x) ≤ M
multiplying by w(x)
mw(x) ≥ g(x)w(x) ≥ M w(x)
g(x) attains every value in m, M , and g(x)w(x) is continuous
´b
a
g(x)w(x)dx
n≤ ´b ≤M
a
w(x)dx
2. So we have
P (x) = ax + b
ˆ x1
(ax + b) dx
x0
1 2 (x1 − x0 )
ax + bx|xx10 = (a(x1 + x0 ) + 2b)
2 2
f (x0 ) = ax0 + b
and
f (x1 ) = ax1 + b
f (x1 ) + f (x0 ) = a(x0 + x1 ) + 2b
ˆ x1
x1 − x0 h
P (x)dx = (f (x0 ) + f (x1 )) = (f (x0 ) + f (x1 ))
x0 2 2
x4 x2 x3 x2 x3
= x3 x4 x − − +
2 2 3
1
= x(6x4 x3 − 3x4 x − 3x3 x + 2x2 )
6
1
= − (x4 − x3 )6
6
QED
4. -
13
(a) Computing ˆ 1
ex dx = ex |10 = e1 − e0 = e − 1
0
(b) M ax
max |f 00 (x)| = max |f 4 (x)| = e1 = e
x∈[0, 1] x∈[0, 1]
(c) Using
(b − a)M 2 (1 − 0)3 e
h = = 10−8
12 12n2
solving for n we have
n ≈ 4759
14
Math 135A
11/15/2010
Homework 6
1. Program Code
(a) (see previous homework for simpson and trapezoid function)
//Simpson Rule
k=10:100:2010;
y=[];
for i=1:21
y=[y abs(exp(1)-1-simpson('exp(x)',0,1,k(i)))]; end
ny=log10(y); nx=log10(k); plot(nx, ny, 'o')
// Trapezoid Rule
ny=[];
for i=1:1:21
y=[y abs(exp(1)-1-trapezoid('exp(x)',0,1,k(i)))];
end ny=log10(y);
plot(nx, ny, 'o')
(b) Here is the chart
15
(c) The error formula for Trapezoidal rule is O(h2 ) where as for simpsonsrule it is O(h4 ). 22 = 4, simpsons
rule is better
2. -
(a) Error has to be on the order of 10−7 , Using the error formula derive din class
(b − a)12h4
10−7 =
180
where h = (b − a)/n, we substitute this in and solve, we have n ≈ 29
(b) Matlab code is below
simpson('exp(-1*x*x)',0,1,29)
thus we have the answer
7.333997511728708 × 10−1
(a) so we have ˆ 1
I3 (x5 ) = x5 dx = 0 = w1 x51 + w2 x52 + w3 x53
−1
ˆ 1
2
I3 (x4 ) = x4 dx = = w1 x41 + w2 x42 + w3 x43
−1 5
16
ˆ 1
3
I3 (x ) = x3 dx = 0 = w1 x31 + w2 x32 + w3 x33
−1
ˆ 1
2 2
I3 (x ) = x2 dx = = w1 x21 + w2 x22 + w3 x23
−1 3
ˆ 1
I3 (x) = x dx = 0 = w1 x1 + w2 x2 + w3 x3
−1
ˆ 1
I3 (1) = 1 dx = 2 = w1 + w2 + w3
−1
which agree.
5. Gaussian v. Simpson
(a) Graph
17
Math 135A
11/29/2010
Homework 7
1. Matlab code is below
(a) h=[]; for i=1:5
h=[h 10^(-i)]; end
ef=[]; es=[]; for i=1:5
ef=[ef abs(exp(2)-(exp(2+h(i))-exp(2))/h(i))];
es=[es abs(exp(2)-(exp(2+h(i))-exp(2-h(i)))/(2*h(i)))];
end
nh=log10(h);
nef=log10(ef);
nes=log10(es);
plot(nh, nef, 'x');
plot(nh,nes,'x');
Plot
18
(b) yEF = 1.0031x + 0.57999, yEs = 1.9644x + 0.019253
(c) The slopes are 1 and 2 because log(h) = 2 ∗ log(h)
(d) So we have
h=[]; for i=1:10
h=[h 10^(-i)]; end
ef=[]; es=[]; for i=1:10
ef=[ef abs(exp(2)-(exp(2+h(i))-exp(2))/h(i))];
es=[es abs(exp(2)-(exp(2+h(i))-exp(2-h(i)))/(2*h(i)))];
end
nh=log10(h);
nef=log10(ef);
nes=log10(es);
plot(nh, nef, 'x');
plot(nh,nes,'x');
19
(a) Theorem
We do a Taylor series expansion around xn − h
h2 f 000 (xn )h3
f (xn − h) = f (xn ) − f 0 (xn )h + f 00 (xn ) −
2 3!
and around xn − 2h
2h2 f 000 (xn )8h3
f (xn − 2h) = f (xn ) − f 0 (xn )2h + f 00 (xn ) −
1 3!
let's set n = 0then we have
−3f (x0 ) + 4f (x1 ) − f (x2 ) = (−3 + 4 − 1)f (x0 ) + (−4hf 0 (x0 ) + 2hf 0 (x0 ))
4
−3f (x0 ) + 4f (x1 ) − f (x2 ) = 2hf 0 (x0 ) + Oh2 f 0 (x0 ) + [2f 000 (ξ2 ) − f 000 (ξ1 )]
3!
Thus 2 3
3f (x0 ) − 4f (x1 ) − f (x1 ) h
f 0 (x0 ) = + [2f 000 (ξ2 ) − f 000 (ξ1 )] 3
2h 2h
Thus we have a remainder term of
h3
(2f 000 (ξ2 ) − f 000 (ξ1 ))
3
(b) Corollary
Using the triangle inequality it is true
h2 h2 h2
error ≤ max |(2f 000 (ξ2 ) − f 000 (ξ1 )| ≤ max [12f 000 (ξ2 ) + |f 000 (ξ1 )|] ≤ 3M = M h2
x∈[a, b] 3 3 x∈[a,b] 3
20
Math 135A
December 3, 2010
Homework 8
1. init.m code
n=6;
A=toeplitz([-2,1,zeros(1,n-2)]);
b=3*ones(n, 1);
2. fact.m code
n=max(size(A)); %n is the size of the matrix given
Ao=A; % Ao is the matrix which will undergo operation
m=A-A;
%m is an empty matrix with same dimensions as A
for i=2:n
for j=i:n
m(i,j-1)=Ao(j,i-1)/Ao(i-1,i-1);
Ao(j,:)=Ao(j,:)-Ao(i-1,:)*m(i,j-1); end
end
U=Ao+m;
% we can add these matrices because one is a lower triangular % and the other is an upper triangular matrix
3. rhs.m code
bo=b; for i=2:n for j=i:n bo(j)=bo(j)-m(i,j-1)*bo(i-1); end
end
g=bo;
backsub.m code
x=zeros(n,1);
x(n)=g(n)/Ao(n,n);
for i=n-1:-1:1
x(i)=(g(i)-Ao(i,i+1:n)*x(i+1:n))/Ao(i,i);
end
4. Table from x1 to x100 (truncated at decimal)
-750 -8085 -15120 -21855 -28290 -34425 -40260 -45795 -51030 -55965
-1497 -8802 -15807 -22512 -28917 -35022 -40827 -46332 -51537 -56442
-2241 -9516 -16491 -23166 -29541 -35616 -41391 -46866 -52041 -56916
-2982 -10227 -17172 -23817 -30162 -36207 -41952 -47397 -52542 -57387
-3720 -10935 -17850 -24465 -30780 -36795 -42510 -47925 -53040 -57855
-4455 -11640 -18525 -25110 -31395 -37380 -43065 -48450 -53535 -58320
-5187 -12342 -19197 -25752 -32007 -37962 -43617 -48972 -54027 -58782
-5916 -13041 -19866 -26391 -32616 -38541 -44166 -49491 -54516 -59241
-6642 -13737 -20532 -27027 -33222 -39117 -44712 -50007 -55002 -59697
-7365 -14430 -21195 -27660 -33825 -39690 -45255 -50520 -55485 -60150
5. For A500 :
fact.m: Elapsed time is 1.369596 seconds.
rhs.m: Elapsed time is 0.008380 seconds.
backsub.m: Elapsed time is 0.011052 seconds.
21
6. The number of operations performed by fact.m should be 23 = 8 times larger. The Elapsed time is 11.404127
seconds, which is 8.3266 times larger than the time it took for A500 . The computing time matches the theory.
7. For A1000 :
fact.m: Elapsed time is 11.404127 seconds (from above)
rhs.m: Elapsed time is 0.031975 seconds.
backsub: Elapsed time is 0.033450 seconds.
x(1) = −1.5×10−3 and x(100)=−1.3515 × 105
8. For A2000
fact.m: Elapsed time is 97.856150 seconds.
rhs.m: Elapsed time is 0.124420 seconds.
backsub:Elapsed time is 0.089146 seconds.
22
Midterm 1
Math 135A: Numerical Analysis
Professor: Thomas Laurent
University of California, Riverside
1. [2 Points] In this problem, you don't need to justify your answer. -0.5 point per mistake)
2. What is the order of convergence of the sequence you obtain if you apply Newton's method to the function
f (x) graphed below?
3. What is the order of convergence of the sequence you obtain if you apply the bisection method to the function
f (x) graphed below?
4. What is the order of convergence of the sequence you obtain if you apply Newton's method to the function
f (x) graphed below?
5. What is the order of convergence of the sequence you obtain if you apply a xed point iteration to the function
f (x) graphed below?
6. What is the order of convergence of the sequence you obtain if you apply a xed point iteration to the function
f (x) graphed below?
7. [4 Points] Suppose p is a xed point of g and suppose that the sequence dened by pn+1 = g(pn ) converges
to p. Suppose also that g is three times continuously dierentiable and g 0 (p) = g 00 (p) = 0. Show that the
sequence {pn }n≥0 converges to p with order 3 (at least).
1
8. Let f (x) be a continuous function which maps [a, b] into itself (i.e.: f ([a, b]) ⊂ [a, b]). Use the intermediate
value theorem to prove that f has a xed point p ∈ [a, b].
9. In this problem, we will study a variant of Newton's method. Let f (x) be a smooth function (all the derivatives
exist and are continuous, so you don't need to worry about that). Let p be the zero of f (x). The sequence
{pn }n≥0 is dened as shown on the picture below.
On the above picture the line L is tangent to the graph of f (x) at the point (p, 0) and the lines L0 , L1 , L2
are parallel to the line L
10. [2 points] Express pn+1 as a function of pn . (If this were Newton's method, the formula would be pn+1 =
pn − f (pn )/f 0 (pn ). What is the formula for this variant of Newton's method?)
11. [3 points] Assuming that pn converges to p, what is the order of convergence? Circle the right answer:
pn converges linearly to p
pn converges at least quadratically to p.
Justify your answer (you have to use some theorem we proved in class.)
12. [1 point] Explain in no more than three sentences why this variant of Newton's method is of no practical
interest to nd the zero of a function. (there is a very simple reason for that! It is just common sense, no
math needed here!)
13. [2 points]
14. What is the distance (or spacing as I called it in class) between two adjacent machine numbers around 10,000?
Give an exact answer in the format 1/2n .
15. [1 point] Suppose you want to use Newton's method to nd the zero of a function. You know that this zero
is somewhere around 10,000. Does it make sense to use the stopping criteria |pn+1 − pn | ≤ 10−10 ?
16. Write down the simplest possible MATLAB code to solve x = 1 + 0.3 cos(x) with Newton's method. Use the
stopping criteria
|pn − pn−1 |
≤ 10−8
|pn |
choose your initial iterate to be 2. If your code compiles and gives the correct answer, then you get full credit.
Midterm 1 Solutions
1. Solutions
2. 2
3. 1
2
4. 1
5. 2
6. 1
7. Let g(p) = p Use Taylor series approximation, since g 0 (p) = g 00 (p) = 0, then the only terms that are left is
g 000 (ξ)(pn − p)3
g(pn ) = g(p) +
3!
where ξ ∈ [pn , p] thus 000
|pn+1 | g (ξn )
=
|pn − p|3 3!
because ξn ∈ [pn , p] where ξn → p
8. Let g(p) = f (p) − p use the intermediate value theorem that at one place g(p) = 0.
9. -
10. pn+1 = pn − f (pn )/f 0 (p)
11. Let g(x) = x − f (x)/f 0 (p) then g 0 (x) = 1 − f 0 (x)/f 0 (p) and we have g 0 (p) = 1 − 1 = 0 which implies pn
converges quadratically.
12. to nd p we have to know f 0 (p)
13. -
14. 213 /252 = 1/239
15. Yes, it makes sense to use the stopping criteria |pn+1 − pn | ≤ 10−10
16. Matlab code
p=@(x) 1+0.3*cos(x)-x;
dp=@(x) -0.3*sin(x)-1;
n=2;
x=2;
while(abs(p(x)-p(n))/abs(p(x)) <=10^(-8) || x=2)
n=x;
x=n-p(n)/dp(n);
end
3
Midterm 2
1. [2 points] This problem is here to help you do the next one
Let f (x) be a smooth function (i.e. f has as may continuous derivatives as you want.)
2. On the picture below, draw the unique polynomial of degree zero which interpolates f (x) at x0 .
3. Let p0 (x) be this unique polynomial of degree zero which interpolate f (x) at x0 . What is the error formula?
Complete the following:
For each x there exists a ξ in the interval ________ such that
f (x) − p0 (x) =
Since ξ depends on x we often write ξ(x)
4. In this problem, we will study the numerical integration method described in the picture below:
´
The integral ab f (x) dx is approximated by the sum of the area of the rectangles.
We will assume during all the problem that f (x) has one continuous derivative. Also we will use the notation
n .
h = b−a
4
5. [3 points] As you know the formula for Simpson's rule is
h
In (f ) = (f (x0 ) + 4f (x1 ) + 2f (x2 ) + 4f (x3 ) + . . . + 4f (xn−1 ) + f (xn ))
3
What is the formula for the integration method we are studying here?
In (f ) =
where ξ is some number between a and b. What is the error formula for our numerical integration method?
Comments: There are two steps in this proof. Provide a picture to make your explanations clearer (okay one
picture: I am really looking for one specic picture). State which theorem you are using and say why it is
OK to use it. Make sure to say in which interval lie the ξi . Your proof do not need to be more detailed than
the one I gave in class.
8. [5 points] Find w1 , w2 , and x2 so that the numerical integration rule
I2 (f ) = w1 f (0) + w2 f (x2 )
approximates ˆ 1
f (x) dx
0
with degree of precision 2. ´
In other words, you have to choose w1 , w2 , and x2 so that I2 (p) = 01 p(x) dx for every polynomial p(x) of
degree ≤ 2.
Comment: Be careful, here we integrate from 0 to 1, and not from -1 to 1 as we have done in class with
Gaussian quadrature. Also the node x1 is xed to be equal to 0.
Midterm 2 Solutions
5
1. [x0 , x], Applying the mean value theorem f 0 (ξ(x))(x − x0 )
2. -
3. h(f (x0 ) + f (x2 ) + ...f (xn−1 ))
´ x1 ´ x1
4. f 0 (ξ(x))(x − x0 ) dx = f 0 (ξ(θ)) h2 Using the weighted mean value theorem
2
x0
f (x) − p0 (x) dx = x0
ˆ b
f 0 (θ) = w(x)f (x)dx
a
thus
f 0 (ξ(θ))h2
E0 =
2
5. Error for general case
n−1
X h2
f 0 (ξi )
i=0
2
where ξ ∈ [xi , xi+1 ] then
n−1
X
= f 0 (ξi )
i=0
m ≤ f 0 (ξi ) ≤ M
which implies
n−1
X
nm ≤ f (ξi ) ≤ nM
i=0
divide the above equation by n invoke mean value theorem we have
n−1
0 1X 0
f (ξ) = f (ξ)
n i=0
thus
(a − b)2 0
f (ξ)
2n
where ξ ∈ [x0 , xn ]
6. Let P (x) = αx2 + βx + γ be a polynomial of degree 2, where α, β, γ = 1then
ˆ 1
1
x2 dx = w1 0 + w2 x22 =
0 3
ˆ 1
1
x dx = w1 0 + w2 x2 =
0 2
ˆ 1
1 dx = w1 + w2 = 1
0
Solving we have x2 = 32 , w1 = 1
4 and w2 = 3
4