Prev: W10 Next: W12

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

Slide:


# User Input Validation

📗 A while loop can be used to keep asking for user input until the input is valid.
valid = 0;
while ~valid
  x = input(...)
  if ... check valid
    valid = 1
  end
end
📗 Membership functions treat a string \(x\) as a vector and returns a Boolean vector specifying whether each character in \(x\) belongs to some set of characters.
isletter(x) or isstrprop(x, 'alpha') checks if the string \(x\) contains characters that are letters 'a' to 'z' or 'A' to 'Z'.
isstrprop(x, 'digit') checks if the string \(x\) contains characters that are number '0' to '9'
ismember(x, y) checks if the string \(x\) contains characters that are characters in the other string \(y\).
📗 Substring functions treat a string \(x\) as a single string and checks if it contains certain substrings.
contains(x, y) checks if string \(x\) contains a substring \(y\).
count(x, y) counts the number of occurrences of \(y\) in the string \(x\).
startsWith(x, y) and endsWith(x, y) checks if the string \(x\) starts with or ends with the substring \(y\).
TopHat Quiz
📗 Check if the string \(s\) contains a letter from \(t\). s = 'abc'; t = 'cde';.
➩ A: contains(s, t)
➩ B: ismember(s, t)
➩ C: max(ismember(s, t))
➩ D: min(ismember(s, t))

TopHat Quiz
📗 Check if the string \(s\) contains at least two letters and a number. s = 'abc';.
➩ A: sum(isletter(s) + isstrprop(s, 'digit')) >= 3
➩ B: sum(isletter(s) >= 2 + isstrprop(s, 'digit') >= 1) >= 3
➩ C: sum(isletter(s)) >= 2 | sum(isstrprop(s, 'digit')) >= 1
➩ D: sum(isletter(s)) >= 2 & sum(isstrprop(s, 'digit')) >= 1

TopHat Quiz
📗 Check if \(s\) is a permutation of \(t\). s = 'aacc'; t = 'abbc'.
➩ A: sum(ismember(s, t)) == length(s)
➩ B: sum(ismember(s, t)) == length(t)
➩ C: sum(sort(s) == sort(t)) == length(s)
➩ D: sum(sort(s) == sort(t)) == length(t)


# Debugging

📗 Syntax error is an error in spelling or grammar.
➩ MATLAB displays red messages for syntax errors, so they are easy to find and fix.
📗 Semantic error is an error in meaning or logic.
➩ For small programs, compare the program outputs with expected outputs computed by hand to find and fix the semantic error.
➩ For large programs, break into smaller programs and debug each one.
📗 A debugger can set break points.
➩ A break point stops the program so that the current variable values can be viewed in Workspace.
➩ It is useful to check if loops and conditionals are written correctly.


# Input Argument Validation

📗 The inputs to a function can be validated so that an input that does not satisfy the conditions will cause an error instead of incorrect outputs.
function f(x)
  arguments
    x (size) class {function} = default value
  end
  ... actual function
end
📗 In x (size) class {functions}, the size is specified by a comma-separated list.
x (n, m) ... requires \(x\) to be an \(n \times m\) matrix.
x (n, :) ... requires \(x\) to be a matrix with \(n\) rows or a vector with \(n\) elements.
x (:, m) ... requires \(x\) to be a matrix with \(m\) columns.
📗 In x (size) class {functions}, the class is specified by its class name.
x (...) char ... and x (...) string ... require \(x\) to be a string.
x (...) single ... and x (...) double ... require \(x\) to be a number.
x (...) logical ... requires \(x\) to be a Boolean variable.
📗 In x (size) class {functions}, the functions are special functions that raise an error when some conditions are not satisfied: Link.
➩ For example, x ... {mustBeGreaterThanOrEqual(x, l), mustBeLessThanOrEqual(x, u)} requires \(x\) to be between \(l\) and \(u\), and raises an error when \(x < l\) or \(x > u\).
➩ For example, x ... {mustBeMember(x, [u, v, w])} requires \(x\) to be one of \(u\) or \(v\) or \(w\).

# Function of Functions

📗 A functional (noun.) is a function that can take another function as an input, or returns another function as an output. Functionals are also called higher-order functions.
📗 y = @f creates a variable \(y\) that represents the function \(f\). The variable \(y\) is a function handle.
➩ Function handles provide a way to pass a function as an input argument to another function.
📗 y = @(x) f(x) creates an anonymous function and stores it in the variable named \(y\).
➩ Anonymous functions provide a way to write a function handle without defining a separate file for the function.

# Function Max Example

📗 The max function can be defined to find the maximum of two function \(f\) and \(g\) at a point \(x\).
function mfg = maxFun(f, g, x)
  mfg = max(f(x), g(x));
end
➩ For example, maxFun(@sin, @(x)(sin(2 * x)), 1) finds the maximum between \(\sin\left(1\right)\) and \(\sin\left(2 \cdot 1\right)\).
📗 The max function can also be defined to find the maximum of two functions \(f\) and \(g\) and returns a function.
function mfg = maxFun(f, g)
  mfg = @(x)(max(f(x), g(x)));
end
➩ For example, if h = maxFun(@sin, @(x)(sin(2 * x))), then h(1) finds the maximum between \(\sin\left(1\right)\) and \(\sin\left(2 \cdot 1\right)\).
TopHat Quiz
📗 function h = noise(f, g, v)
📗   h = @(x)(f(x) + v * g(x))
📗 end
📗 h = noise(@sqrt, @(x)(x .^ 2), 0.5);
📗 h(4)
➩ A: 18
➩ B: 10
➩ C: 6
➩ D: Error

TopHat Quiz
📗 function h = noise(f, g)
📗   h = @(x)(f(x) + rand() * g(x))
📗 end
📗 h = noise(@sqrt, @(x)(x .^ 2));
📗 h(4) == h(4)
➩ A: 0
➩ B: 1
➩ C: 2
➩ D: Error


📗 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