Challenge - 5 Problems
Simulation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of a simple simulation loop
What is the output of the following MATLAB code that simulates a simple process?
MATLAB
result = 0; for i = 1:5 result = result + i^2; end fprintf('%d\n', result);
Attempts:
2 left
💡 Hint
Remember that i^2 means i squared and the loop adds these values.
✗ Incorrect
The loop sums the squares of numbers from 1 to 5: 1+4+9+16+25 = 55.
🧠 Conceptual
intermediateUnderstanding simulation time steps
In a simulation that updates a system every 0.1 seconds for 2 seconds, how many iterations will the simulation loop run?
Attempts:
2 left
💡 Hint
Divide total time by time step to find iterations.
✗ Incorrect
2 seconds divided by 0.1 seconds per step equals 20 iterations.
🔧 Debug
advancedIdentify the error in this simulation code
What error will this MATLAB code produce when run?
MATLAB
time = 0:0.5:5; for t = time y = sin(t); end fprintf('Final y: %f\n', y);
Attempts:
2 left
💡 Hint
Check the loop variable and how MATLAB handles for loops over arrays.
✗ Incorrect
The loop iterates over each element in time, assigning y = sin(t). The final y is sin(5) ≈ -0.958924.
📝 Syntax
advancedSyntax error in simulation update step
Which option contains the correct syntax to update the variable 'position' by adding velocity times time step 'dt' in MATLAB?
Attempts:
2 left
💡 Hint
MATLAB supports both explicit assignment and '+=' (R2016b+).
✗ Incorrect
Option B uses correct MATLAB syntax in all versions. Option B uses '+=' which is valid since R2016b. Option B is a typo. Option B uses '.' incorrectly.
🚀 Application
expertResult of a stochastic simulation
Consider this MATLAB code simulating a random walk for 3 steps starting at 0. What is the possible output of 'position' after running the code once?
MATLAB
position = 0; steps = 3; for i = 1:steps move = randi([-1 1]); position = position + move; end fprintf('%d\n', position);
Attempts:
2 left
💡 Hint
Each step can move -1, 0, or +1, so position can vary widely.
✗ Incorrect
Since each step can be -1, 0, or +1, after 3 steps position can be any integer between -3 and 3.
