Prev: W2 Next: W4

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

Slide:


# Script

📗 .m files are MATLAB scripts and can be used to store a list of commands or the definition of a function.
📗 The script and its output can be published as a PDF file or an HTML web page.

# Curves

📗 A curve can be the graph of a function described by \(y = f\left(x\right)\), or the trace of a moving point, in which the movement of the point is described by its position \(\left(f_{x}\left(t\right), f_{y}\left(t\right)\right)\) at time \(t\).
📗 A curve is plotted using a large number of line segments.
➩ To plot \(y = f\left(x\right)\) from \(x = x_{1}\) to \(x = x_{n}\), find \(x_{1} < x_{2} < x_{3} < ... < x_{n}\) and use lines to connect the following points, \(\left(x_{1}, f\left(x_{1}\right)\right), \left(x_{2}, f\left(x_{2}\right)\right), \left(x_{3}, f\left(x_{3}\right)\right), ..., \left(x_{n} f\left(x_{n}\right)\right)\).
➩ To plot \(\left(f_{x}\left(t\right), f_{y}\left(t\right)\right)\) from \(t = t_{1}\) to \(t_{n}\), find \(t_{1} < t_{2} < t_{3} < ... < t_{n}\) and use lines to connect the following points, \(\left(f_{x}\left(t_{1}\right), f_{y}\left(t_{1}\right)\right), \left(f_{x}\left(t_{2}\right), f_{y}\left(t_{2}\right)\right), \left(f_{x}\left(t_{3}\right), f_{y}\left(t_{3}\right)\right), ..., \left(f_{x}\left(t_{n}\right), f_{y}\left(t_{n}\right)\right)\)

# Curve Discretization

📗 \(t_{1}, t_{2}, t_{3}, ..., t_{n}\) is a partition of the domain \(t \in \left[t_{1}, t_{n}\right]\).
➩ The partition is usually uniform, meaning \(t_{i} = t_{i-1} + \delta\) with \(\delta = \dfrac{t_{n} - t_{1}}{n}\) and some large \(n\).
➩ \(t_{i}\) can also be sampled randomly.
➩ \(t_{i}\) can also be chosen according to how fast the function is changing.
➩ \(t_{i}\) can also be chosen so that the lengths of the line segments are the same.

# Curve Plotting

📗 Suppose \(x, y\) are vectors of length \(n\), plot(x, y) plots line segments connecting \(\left(x_{1}, y_{1}\right), \left(x_{2}, y_{2}\right), ..., \left(x_{n}, y_{n}\right)\).
➩ For example, define x = 0:0.01:1 and use plot(x, f(x)) to plot \(f\left(x\right)\) between \(0\) and \(1\) with a partition of size \(100\).
➩ Another example, define t = 0:0.01:1 and use plot(fx(t), fy(t)) to plot \(\left(f_{x}\left(t\right), f_{y}\left(t\right)\right)\) between \(0\) and \(1\) with a partition of size \(100\).
TopHat Quiz
📗 Plot a unit square.
➩ A: plot([0, 1], [0, 1])
➩ B: plot([0, 0, 1, 1], [0, 1, 1, 0])
➩ C: plot([0, 0, 1, 1, 0], [0, 1, 1, 0, 0])
➩ D: plot([0, 1, 0, 1, 0], [0, 1, 1, 0, 0])

TopHat Quiz
📗 Plot a full circle.
➩ A: plot(sind(0:360), sind(0:360))
➩ B: plot(sind(0:360), cosd(0:360))
➩ C: plot(-1:0.01:1, sqrt(1 - (-1:0.01:1).^2))
➩ D: plot(-1:0.01:1, -sqrt(1 - (-1:0.01:1).^2))


# Line Specs

📗 plot(x, y, s) where \(s\) specifies the style, marker, and color of the lines.
➩ Line style: - solid, -- dashed, : dotted, -. dash-dotted.
➩ Marker: o circle, . dot, x cross, s square, d diamond ...
➩ Color: r red, g green, b blue, k black, w white ...
📗 plot(x1 y1, s1, x2, y2, s2, ...) plots multiple lines in the same figure.
TopHat Quiz
📗 Plot a horizontal dashed line at \(y = 0\).
➩ A: plot(0:10:1800, sind(0:10:1800), '--')
➩ B: plot(0:90:1800, sind(0:90:1800), '--')
➩ C: plot(0:100:1800, sind(0:100:1800), '--')
➩ D: plot(0:180:1800, sind(0:180:1800), '--')


# Plotting Features

📗 Texts can be added to the plot.
title(t) adds title \(t\).
xlabel(t) adds x-axis label \(t\).
ylabel(t) adds y-axis label \(t\).
legend(c1, c2, ...) adds legend (names of the curves \(c_{1}, c_{2}, ...\)).
text(x, y, t) adds text \(t\) at position \(\left(x, y\right)\).
axis([x0, x1, y0, y1]) changes the range of the axes to \(x \in \left[x_{0}, x_{1}\right]\) and \(y \in \left[y_{0}, y_{1}\right]\).

# 3D Curve Plotting

📗 Suppose \(x, y, z\) are vectors of length \(n\).
📗 plot3(x, y, z, s) plots the lines in 3D connecting \(\left(x_{1}, y_{1}, z_{1}\right), \left(x_{2}, y_{2}, z_{2}\right), ..., \left(x_{n}, y_{n}, z_{n}\right)\), with specs \(s\).

# Surface Plotting

📗 A surface can be a graph of a function described \(z = f\left(x, y\right)\), or the trace of a moving point, in which the movement of the point is described by its position \(\left(f_{x}\left(s, t\right), f_{y}\left(s, t\right), f_{z}\left(s, t\right)\right)\).
📗 A surface is plotted using a large number of faces, usually triangles, but in MATLAB, four sided polygons.
📗 Suppose \(x, y, z\) are matrices representing points on the surface.
contour(x, y, z, n) plots \(n\) contours of the surface, and contour3(x, y, z, n) plots them in 3D.
mesh(x, y, z) plots the surface mesh.
surf(x, y, z) plots the surface.
📗 If \(x\) and \(y\) are omitted, the \(x\) and \(y\) coordinates are assumed to be the column and row indices of the elements in \(z\).
TopHat Quiz
📗 Plot a unit height pyramid centered at \(\left(2, 2\right)\).
➩ A: surf([1 1 1; 1 0 1; 1 1 1]
➩ B: surf([0 0 0; 0 1 0; 0 0 0]
➩ C: surf([0 1 0; 0 1 0; 0 1 0]
➩ D: surf([0 0 0; 1 1 1; 0 0 0]

TopHat Quiz
📗 Plot \(z = x + 2 y\) for \(x = y = \begin{bmatrix} 1 & 2 & 3 \end{bmatrix}\) using surf(x + 2 * y).
➩ A: x = repmat([1 2 3], [3 1]); y = repmat([1 2 3]', [1 3])
➩ B: x = repmat([1 2 3]', [1 3]); y = repmat([1 2 3], [3 1])
➩ C: x = repmat([1 2 3], [1 3]); y = repmat([1 2 3]', [3 1])
➩ D: x = repmat([1 2 3]', [3 1]); y = repmat([1 2 3]', [1 3])


# Mesh Grid Shortcut

📗 [x, y] = meshgrid(u, v) creates x = repmat(u, [length(v) 1]) and y = repmat(v', [1 length(u)]). The matrices \(x, y\) then can be used to plot the surface \(z = f\left(x, y\right)\) using surf(x, y, f(x, y)).
📗 [x, y, z] = sphere() and [x, y, z] = cylinder() create meshes of a unit sphere and a unit cylinder. The surface then can be plotted using surf(x, y, z).
📗 Under "PLOTS" tab, many other plots can be created based on a matrix.
TopHat Quiz
📗 Plot \(z = x^{2} + y^{2}\) for \(x = y = \begin{bmatrix} -2 & -1 & 0 & 1 & 2 \end{bmatrix}\) using [x, y] = meshgrid(-2:2, -2:2)
➩ A: surf(x, y, x .^ 2 + y .^ 2)
➩ B: surf(x, y, x ^ 2 + y ^ 2)
➩ C: surf(x, y, x' * x + y' * y)
➩ D: surf(x, y, x * x' + y * y')



📗 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: March 03, 2025 at 12:52 AM