Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q14 of 15
MATLAB - Numerical Methods
Identify the error in this code snippet:
f = @(y,t) y^2 - t;
[t,y] = ode45(f, [0 1], 0);
plot(t,y);
ATime span must be a column vector.
BInitial value cannot be zero.
CFunction handle arguments are in wrong order.
DPlot command is missing labels.
Step-by-Step Solution
Solution:
  1. Step 1: Check function handle argument order

    ODE functions for ode45 must have arguments in order (t,y), but here it is @(y,t), which is incorrect.
  2. Step 2: Verify other parts of the code

    Initial value zero is allowed, time span as [0 1] is valid, and plot labels are optional, so these are not errors.
  3. Final Answer:

    Function handle arguments are in wrong order. -> Option C
  4. Quick Check:

    Function handle must be @(t,y), not @(y,t) [OK]
Quick Trick: Always use @(t,y) for ode45 function handles [OK]
Common Mistakes:
  • Swapping t and y in function handle
  • Thinking initial value can't be zero
  • Believing time span must be column vector

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes