Prev: W7 Next: W9

📗 Tuesday lectures: 4:00 to 4:50, Zoom, TopHat: Link (or Google Form: Form if TopHat not working). MATLAB.
📗 Programming Homework: P4

Slide:


# Vectorization, Again

📗 Loops are used when the same task is repeated for a large number of times.
➩ If these tasks can be done simultaneously in parallel, vectorization is preferred in MATLAB: define the repeating task as a function, and apply the function to a vector or matrix.
➩ If these tasks must be done sequentially, then a for loop could be used.
➩ If these tasks are done for an unknown number of times until some condition is met, then a while loop could be used.

# For Loops

📗 for t = 1:n \(...f\left(t\right)...\) end repeats the function \(f\) for \(n\) times.
t is the counter or index variable.
➩ In MATLAB, since i is the complex number \(\sqrt{-1}\), using i as the index variable is not recommended.
➩ In MATLAB, for loop is count controlled, meaning changing the counter variable inside the loop has no impact on the number of times the loop is repeated.
📗 for t = v \(...f\left(t\right)...\) end repeats the function \(f\) for length(v) times, one for each value in v.
v = 1:n is the special case in which the set is the index set.
TopHat Quiz
📗 Approximate \(\displaystyle\sum_{i=1}^{\infty} \dfrac{1}{i^{2}} = \dfrac{\pi^{2}}{6}\). Note: sum(1 ./ (1:1000).^2) should be used instead (vectorization is faster).
s = 0;
for z = 1:1000

➩ A: s = 1 / (i ^ 2);
➩ B: s = 1 / (z ^ 2);
➩ C: s = s + 1 / (i ^ 2);
➩ D: s = s + 1 / (z ^ 2);

end; s

TopHat Quiz
📗 Approximate \(1 + \dfrac{1}{1 + \dfrac{1}{1 + \dfrac{1}{1 + ...}}} = \dfrac{1 + \sqrt{5}}{2}\).
s = 1; is a random guess.
for p = 1:1000

➩ A: s = 1 + 1 / s;
➩ B: s = 1 + 1 / p;
➩ C: s = 1 + 1 / (1 + s);
➩ D: s = 1 + 1 / (1 + p);

end; s

TopHat Quiz
📗 Approximate \(2 + \dfrac{2}{1 + \dfrac{1}{\dfrac{1}{2} + \dfrac{1}{\dfrac{1}{3}  + \dfrac{1}{...}}}} = \pi\).
s = 1; is a random guess.
for t = 1:1000

➩ A: s = 1 / t + 1 / s;
➩ B: s = 1 / (1001 - t) + 1 / s;
➩ C: s = 1 / t + 1 / (1001 - s);
➩ D: s = 1 / (1001 - t) + 1 / (1001 - s);

end; 2 + 2 / s


# Continue and Break

📗 It is possible to stop a for loop without finishing all iterations.
continue skips the remaining code of the current iteration.
break skips the remaining code for the current iteration and all remaining iterations.
➩ In general, avoid using continue and break and use while loops instead.

# Fixed Point

📗 A function \(f\) is a contraction map if \(\left| f\left(x\right) - f\left(y\right) \right| < k \left| x - y \right|\) for some \(k \in \left[0, 1\right)\), for all \(x\) and \(y\).
📗 Every contraction mapping has a unique fixed point \(x^\star\) such that \(f\left(x^\star\right) = x^\star\).
📗 The fixed point \(x^\star\) could be found by fixed point iterations.
➩ Start with any \(x_{0}\).
➩ Compute \(x_{n+1} = f\left(x_{n}\right)\), for \(n = 0, 1, 2, ...\).
➩ The sequence \(x_{0}, x_{1}, x_{2}, ...\) converges to \(x^\star\).
📗 Newton's method to solve non-linear system of equations is an example of a fixed point algorithm.
TopHat Quiz
📗 Approximate the solution for \(\cos\left(x\right) = x\).
s = 0; is a random guess.
for x = 1:1000

➩ A: s = cos(s);
➩ B: s = cos(x);
➩ C: x = cos(s);
➩ D: x = cos(x);

end; s


# Loop over a Matrix

📗 A single for loop loops over a vector: for t = 1:n \(...v\left(t\right)...\) end, where v is a vector of length n.
📗 A nested for loop loops over a matrix: for r = 1:n for c = 1:m \(...m\left(r, c\right)...\) end end, where m is a matrix of size n rows by m columns.
TopHat Quiz
📗 This is the cumsum function.
📗 s = [5 4 3 2 1];
📗 for c = 2:5
📗   s(c) = s(c - 1) + s(c);
📗 end; s
➩ A: \(\begin{bmatrix} 5 & 4 & 3 & 2 & 1 \end{bmatrix}\)
➩ B: \(\begin{bmatrix} 5 & 7 & 10 & 14 & 19 \end{bmatrix}\)
➩ C: \(\begin{bmatrix} 5 & 9 & 12 & 14 & 15 \end{bmatrix}\)
➩ D: \(\begin{bmatrix} 5 & 9 & 7 & 5 & 3 \end{bmatrix}\)

TopHat Quiz
📗 s = [1 1 1 1; 1 0 0 0; 1 0 0 0];
📗 for r = 2:3
📗   for c = 2:4
📗     s(r, c) = s(r - 1, c - 1) + s(r, c - 1);
📗   end
📗 end; s
➩ A: \(\begin{bmatrix} 1 & 1 & 1 & 1 \\ 1 & 0 & 0 & 0 \\ 1 & 0 & 0 & 0 \end{bmatrix}\)
➩ B: \(\begin{bmatrix} 1 & 1 & 1 & 1 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 4 & 7 \end{bmatrix}\)
➩ C: \(\begin{bmatrix} 1 & 1 & 1 & 1 \\ 1 & 2 & 3 & 4 \\ 1 & 3 & 5 & 7 \end{bmatrix}\)
➩ D: \(\begin{bmatrix} 1 & 1 & 1 & 1 \\ 1 & 2 & 3 & 4 \\ 1 & 3 & 6 & 10 \end{bmatrix}\)



📗 Notes and code adapted from the course taught by Professors Beck Hasti and Michael O'Neill.
📗 You can expand all TopHat Quizzes and Discussions: .
📗 If there is an issue with TopHat during the lectures, please submit your answers on paper (include your Wisc ID and answers) or this Google form Form at the end of the lecture.





Last Updated: April 18, 2025 at 6:18 PM