MATLAB command What it does
for i=a:b For loop executes the code segment that follows, as variablei code segment increments fromatob.
end
whilelogical expression While loop executes the code segment that follows until code segment emphlogical expression is false.
end
for i=a:b For loop executes the code segment that follows, as variablei code segment increments fromatob.
end
iflogical expression If-then statement executes the code segment that follows, if code segment logical expression is true.
end
mod(q,d) Returns the remainder ifqis divided byd zeros(n,m) Make an×mmatrix of all zeros
10. Use plot to plot the piecewise defined function f(x) =
(2x+ 1, 0≤x <1, 3/x, 1≤x≤4 on the interval 0≤x≤4.
11. Use plot to plot the piecewise defined function f(x) =
(−x/2, −1≤x <0, e3x, 0≤x≤1
on the interval−1≤x≤1. Use open and closed circles on the graph to indicate a function.
12. Write a MATLAB file to approximate the integralR1
0 x2dx using the sumPn
i=1x2∆xwith ∆x= n1. Compare the error in your results forn= 10,n= 100, andn= 1000.
13. Write a MATLAB file used to compute the summationP10 n=1
1
n −n+11 . 14. Write a MATLAB file that computesn! for any integern≥0. Use a while loop.
15. The Euclidean Algorithm is used to find the greatest common divisor ofaandb. The greatest common divisor,d, has the property thatdis a divisor of bothaandb, and ifc is another common divisor ofa andb, it must follow thatc≤d. The psuedocode below is the Euclidean Algorithm, used to find the greatest common divisor of positive integersA andB with A > B >0. Convert the code to working MATLAB code that takes argumentsAandB and outputs their greatest common divisor.
a:=A b:=B r:=B whileb6= 0
r:=a modb a:=b
b:=r end while gcd:=a output gcd
16. Write a MATLAB file that finds the firstnFibonacci numbers, for anyn.
17. Write a program that outputs all divisors of a given positive integer.
18. Use a nested for loop to write a program to find the sum of two matricesAandB. Before adding, use a conditional statement to ensure the dimensions of the matrices are compatible and return an error statement if they are not.
19. Without using the isprime() function, write a MATLAB file used to determine if a numberxis prime or not. To do this, dividexby all the integers below√
x, and examine the remainder. The code should output text to the command window, saying whether or not the number is a prime.
20. Recall from Calculus II that the harmonic series P∞ n=1
1
n diverges to infinity. Write MATLAB code to computePN
n=1 1
n forN = 10,100,1000, and 10000. Determine how large N must be in order to satisfyPN
n=1 1 n ≥15.
21. It turns out that P∞ n=1
1
n2 = π62. Write MATLAB code to compute PN n=1
1
n2 for N = 10,100,1000, and 10000. Compare each of these to π62. Determine how large N must be in order for the error to satisfy
PN n=1
1
n2 −P∞ n=1
1 n2
<10−6.
22. Suppose the sequence{ai}∞i=1 is defined as follows:
a1= 1, a2= 3, ak=ak−2+ 2ak−1 for integersk≥3.
Write a MATLAB file used to generate the sequence, and use it to finda20.
23. See [6]. Consider the velocity of a rocket given by the piecewise function below. Develop an m-file using conditional statements to computev as a function oft. Use this to generate a plot ofv versust on the interval−5≤t≤50.
v(t) =
11t2−5t, 0≤t≤10 1100−5t, 10< t≤20 50t+ 2(t−20)2, 20< t≤30 1520e−0.2(t−30), t >30
0, otherwise
24. Consider the rescursively defined sequencex0= 3, xn+1=√
1 +xn. Write a for loop used to calculate xn forn = 1,2,· · ·30. [Hint: to take the square root of a number x in MATLAB use sqrt(x).] At the beginning of your script, enter the line format long to increase the number of decimal places that are displayed.
(a) Write out the exact number that you are computing on the first, second and third steps, then givex30. Does the sequence seem to be converging to a number and if so, what number?
(b) Compute the solution tox=√
1 +xby hand. Compare to your answer in (a).
(c) Modify your MATLAB code to generate values of the sequence xn until the “error” |xn−φ| is less than 10−4, whereφis the answer you found in (b).
25. Write a MATLAB file used to count the sizes of the gaps between the first 1000 prime numbers. For example, the first few prime numbers are 2, 3, 5, 7, 11, and so the first few gaps would be 1, 2, 2, 4.
Then generate a histogram (using hist()) of these 1000 numbers and comment on the shape of the distribution. [Hint: start by making a vector of size 1000 called gap. Set up a while loop to stop after 1000 prime numbers are found. Inside the while loop use isprime() to test whether the number is prime. If it is, you want to increase the number of prime numbers found by 1. If it is not, you want to increase the value of gap(i) by 1, where i is the number of prime numbers found so far.]
26. In studying rocket propulsion, we have to keep in mind that the overall mass changes as a rocket burns fuel. Due to conservation of momentum, the overall momentum remains constant. This means that the acceleration of the rocket will not be constant.
The initial momentum of the system ispi =mv wheremis the mass of the rocket and fuel combined, andv is the velocity of the rocket. Assuming that the engines burn at a constant rate, then during a small time intervaldtthe mass decreases by dmg with velocity−u(where we use the negative sign since it is moving at the opposite direction of the rocket). The exhaust gas has velocityv−u(with respect to earth). Letdvbe the change in velocity during the time intervaldt. The final momentum is
pf =procket+pgas= (m−dmg)(v+dv) +dmg(v−u).
(a) Setpf =pi to show that
m·dv=dmg·dv+dmg·u. (2.2)
(b) Show thatdv= m−dmdmgu
g.
(c) Suppose that a spacecraft is moving in gravity-free space in a straight path. Thrusters are turned on, fuel is ejected at 2.0×102kg/s at a speed (relative to the rocket) of 2.5×102m/s. The initial mass of the spacecraft and unburned fuel is 2.0×104 kg and the thrusters are on for 30 seconds.
What is the thrust (force applied to rocket by ejected fuel)? What is the accelerationa(t)? Write a MATLAB code to estimate the velocity throughout the 30 second period. Use ∆t= 1.
(d) In (2.2), we can probably ignoredmg·dv since this is a product of two small quantities. Show that this results indv=udmm.
(e) Integrating the equation you just found throughout a period of time, Z tf
t0
dv=−u Z tf
t0
1 mdm.
This results in
dv=ulnmi m
.
This is the rocket equation, first estabilished by the Soviet physicist Konstantin Tsiolkovsky in 1897. Use the rocket equation to solve the problem posed in (c), and compare to your answer found in (d).
Chapter 3
Iterative Methods
The focus of this chapter is on solving equations of the formf(x) = 0. Such a solution is called a zero, or a root of the function f(x). In earlier courses, you may have focused on solving this problem by hand for specific types of functionsf, such as linear, polynomial, or rational functions. Some of these problems were designed so that finding an analytic solution is possible. However, many equations are not so easily solved by hand - for example, x6+x−1 = 0, or √
x+ 1 = 2x. A crude method of approximating solutions is to use technology to graph the equation in the form f(x) = 0, then zoom in to determine where the graph crosses thex-axis. How can we write a computer program to mimic this process in order to approximate the roots? In this section, we look at iterative techniques for approximating these roots. Knowing how well an algorithm will work, or even if it will work at all requires knowing some qualitative properties off(x). This means we need to introduce a little bit of theory in order to understand when the algorithm can be used.
3.1 Fixed Point Iteration
In Activity A.0.10 you found that the sequence
Mt+1= 0.5Mt+ 1, M0= 1 (3.1)
converged to 2, that is lim
t→∞Mt= 2. It turns out that lim
t→∞Mt= 2 for any starting valueM0. You can verify this for yourself by running the code in Activity A.0.10 and changing the initial value of m.
Now imagine that we don’t know the limit, call itM∗ = lim
t→∞Mt. As long as a limit exists, we should have M∗ ≈Mt+1≈Mt for large values of t. Assuming thatM∗ =Mt+1 =Mt and substituting this into (3.1), we get M∗ = 0.5M∗+ 1. Solving this in turn gives M∗= 2. With a fixed point iteration we are reversing this process in a sense. The goal is to solve an equation in the formf(x∗) =x∗. For this particular example it is easy to solveM∗= 0.5M∗+ 1 algebraically so there is no need to use any special algorithm to solve it.
However, the fixed point iteration method can be used to solve an equation such as lnx+ 2 =x, which does not have a closed-form solution.
Definition 3.1.1. A valuex∗ is afixed pointof a function f(x)if f(x∗) =x∗.
The algorithm to find a fixed point of a recurrence relationf(xn) is relatively simple. We compute values of the sequencexn+1 =f(xn) until the values seem to be converging. To be precise, we will use a specific error tolerance to ensure the fixed point is as close as we’d like to be to the true fixed point. This will be programmed in a way similar to the tolerances we used in Chapter 2 to approximate the value of a summation.
To describe a generic algorithm that is used to compute a fixed point, computer programmers or mathemati- cians often usepseudocode. The pseudocode provides an outline of the computer code in a generic form,
and does not correspond to a specific programming language. The pseudocode forfixed point iterationis shown below.
Fixed Point Iteration to solve f(x) =x choose initial guessx0
setn:= 1
loop until some convergence criterion is met, or untilnis too large xn+1:=f(xn)
n:=n+ 1 end loop output xn
It’s worth taking a minute to consider how one might come up with an initial “guess”. Different initial guesses may lead to different results, particularly if there are multiple roots to the equationf(x)−x= 0.
It will sometimes take a bit of trial and error to determine an initial guess that will work. We may know something about the function or be able to use mathematical theory to help narrow down the interval in which a fixed point may occur. The intermediate value theorem, for example, guarantees a root in a closed interval provided the function is continuous and has values with opposite signs at each endpoint. Technology may also give us a rough approximation to the root that can be used as an initial guess. You may use these methods and others in choosing x0.
Example 3.1.2
We wish to determine if the recurrence relationxn+1 = ln(xn) + 2 converges. Use fixed point iteration to solve ln(x) + 2 =x, accurate to within a tolerance of 10−4.
. . . . Using the pseudocode above, a basic MATLAB script to find the fixed point is given here.
1 x=1; %initialize x
2 tol=1e-4; %set tolerance
3 i=1; %set the index
4 x(i+1)=log(x(i))+2; %apply fixed point iteration 5 while abs(x(i+1)-x(i))>tol %until convergence is met
6 i = i + 1; %update index
7 x(i+1)=log(x(i))+2; %apply fixed point iteration 8 end
9 x’ %output iterates
On line 1, we set our initial “guess” for the root tox0= 1. On lines 2 and 3 we set the tolerance to 10−4, and initialize i to keep track of the number of iterations. On line 4 we apply the first step of the fixed point algorithm. Without this, there is no way to evaluate the condition in line 5, since that requires knowing thetwomost recent values of x. In line 6 we update the index i and line 7 performs the fixed point iteration. Lines 6 and 7 are repeated until the difference betweenx(i) andx(i+ 1) is less than the tolerance of 10−4. Keep in mind that in this script, i is initially set to 1, so line 5 will initially compare x(1) and x(2) which is what we want. The first time through the loop, i will increase to 2, then on line 7 we update x(3). Next we end up on line 5 again, but this time compare x(3) with x(2). It is easy to be off by an index, so it is important to mentally work through your code step by step.
One common pitfall that beginning MATLAB programmers make is to initialize a vector x using x(1). The problem is that if x is already in the workspace as a vector (this could be from a program you ran last week), then MATLAB will only update x(1) which is probably not desirable. This is why in line 1, we initialized x without using an index - this tells MATLAB to remove any previous values of x and start over by setting it to a single number.
Avoid using x(1) to initialize a vector x By running this code you should see that the iterates converge to 3.1462.
In the example above, the sequencexn+1= ln(xn) + 2 converges to 3.1462 for most initial guesses. However there are two solutions tox= ln(x) + 2, the other one being close to 0.1586. Also if your initial guess is less than 0.1586, the iterates become complex numbers (the outputs are of the form a±bi), since MATLAB is eventually taking the logarithm of negative numbers. So the fixed point algorithm does not find all of the fixed points, and in other cases it might not find any fixed points.
Theorems 3.1.4 and 3.1.5 give us some insight into when the fixed point iteration works. Specifically, f(x) needs to satisfy certain properties. See Appendix F for the proofs to these (and other) theorems.
Definition 3.1.3. We say that a functionf issmoothon an intervalI iff0exists onIandf0is continuous on I.
The following theorem tells us a condition that guaranteesf has a fixed point.
Theorem 3.1.4. Iff is continuous on [a, b], anda≤f(x)≤bfor allx∈[a, b], thenf has a fixed point in [a, b].
Notice that both xandf(x) must be bounded by the same valuesaand b. This is not always easy to do, in practice.
The next theorem gives conditions for which a fixed point is unique, and for which the fixed point iteration method xn=f(xn−1) is guaranteed to converge to this unique fixed point.
Theorem 3.1.5. If f is smooth on [a, b], a ≤ f(x) ≤ b, and |f0(x)| ≤ k for some k < 1 on (a, b), then f has a unique fixed point x∗ ∈ [a, b]. In addition, for any number x0 in [a, b], the sequence defined by xn=f(xn−1), n≥1, converges to this unique fixed point x∗ in[a, b]. Moreover, |xn−x∗| ≤kn|b−a|.
Example 3.1.6
Can we apply Theorems 3.1.4 and 3.1.5 to ensure thatf(x) =√
1 +xhas a fixed point on some interval [a, b]? How many iterations are required to be within 10−5of the solution?
. . . . Notice that f is continuous for all x ≥ −1. The second condition of Theorem 3.1.4 holds for several values ofaandb. For example, if 0≤x≤10 then certainly 0≤√
1 +x≤10. Therefore, 0≤f(x)≤10 for allx∈[0,10], and Theorem 3.1.4 ensures thatf must have a fixed point in [0,10].
Next,f0(x) = 2√11+x. Notice thatf0is a decreasing function on [0,10] so therefore must have a maximum value atx= 0. Also,f0 is continuous on [0,1] sof is smooth on [0,10]. Moreover,f0 is nonnegative on [0,10] so
|f0(x)|=f0(x)≤f0(0) = 1 2 <1.
By Theorem 3.1.5 (withk= 12),f has a unique fixed point in [0,10] and the sequence xn =√
1 +xn−1 should converge to this fixed point for any initial pointx0∈[0,10].
If we wantxn to be within 10−5 of the root, we need |xn−x∗| ≤kn|b−a|= 0.5n·10<10−5. Solving forn, we see thatn≥20, meaning that we should use at least 20 iterations.
A common question for iterative methods is how to determine when we have reached a root. If we are not sure how many iterations we need, it makes sense to use a while loop with astopping conditionorconvergence
criteria. The stopping condition used in the MATLAB script in Example 3.1.2 is|xn−xn−1|<TOL, where TOL is 10−5. That is, we stop until the iterates are changing by less than TOL. This usually means that xn is within TOL units ofx∗ but this is not guaranteed.
Another type of stopping condition called the absolute relative approximate errortakes into account the relative size ofxn. The stopping condition is
xn+1−xn
xn+1
<TOL.
Work through Activity A.0.18 to see how the tolerance relates to the number of iterations.
Refer to Exercise A.0.19 for an example of root finding with fixed point iteration.