0
0
MATLABdata~20 mins

Anonymous functions in MATLAB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Anonymous Functions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of an anonymous function with vector input
What is the output of the following MATLAB code?
MATLAB
f = @(x) x.^2 + 2*x + 1;
result = f([1 2 3]);
disp(result);
A[4 9 16]
B[4 8 12]
C[4 9 15]
D[1 4 9]
Attempts:
2 left
💡 Hint
Remember that the operator .^ applies element-wise power to each element in the vector.
Predict Output
intermediate
2:00remaining
Output of nested anonymous functions
What is the output of this MATLAB code?
MATLAB
g = @(x) x + 1;
h = @(f, x) f(x) * 2;
result = h(g, 3);
disp(result);
A6
B7
C8
D9
Attempts:
2 left
💡 Hint
Evaluate the inner function g(3) first, then multiply by 2.
🔧 Debug
advanced
2:00remaining
Identify the error in anonymous function definition
Which option will cause an error when defining the anonymous function in MATLAB?
Af = @(x) x^2 + 3*x + 2;
Bf = @(x) x.^2 + 3*x + 2;
Cf = @(x) (x^2) + 3*x + 2;
Df = @(x) x**2 + 3*x + 2;
Attempts:
2 left
💡 Hint
Check the operator used for power in MATLAB.
Predict Output
advanced
2:00remaining
Output of anonymous function with conditional expression
What is the output of this MATLAB code?
MATLAB
f = @(x) (x > 0) .* x + (x <= 0) .* 0;
result = f([-2 0 3]);
disp(result);
A[0 0 3]
B[-2 0 3]
C[0 0 0]
D[1 0 3]
Attempts:
2 left
💡 Hint
The function returns x if x is positive, otherwise 0.
🧠 Conceptual
expert
2:00remaining
Behavior of anonymous functions capturing variables
Consider the following MATLAB code. What is the value of y after running it?
MATLAB
a = 5;
f = @(x) x + a;
a = 10;
y = f(3);
A8
B13
C3
DError due to variable scope
Attempts:
2 left
💡 Hint
Anonymous functions use the current value of workspace variables at the time of execution.