Bird
0
0

Find the error in this MATLAB code:

medium📝 Debug Q7 of 15
MATLAB - Numerical Methods
Find the error in this MATLAB code:
f = @(x) x^2;
result = integral(f, 0, 1);
Aintegral requires vector inputs, not function handles
BFunction handle uses scalar operator ^ instead of element-wise .^
CLimits 0 and 1 are invalid for integral
DMissing semicolon after function handle
Step-by-Step Solution
Solution:
  1. Step 1: Check function handle definition

    Function handle uses x^2, which is scalar power; for vector inputs, use x.^2.
  2. Step 2: Understand integral input

    integral passes vector inputs to function handle, so element-wise operator is needed.
  3. Final Answer:

    Function handle uses scalar operator ^ instead of element-wise .^ -> Option B
  4. Quick Check:

    Use .^ in function handles for integral [OK]
Quick Trick: Use .^ for element-wise power in function handles [OK]
Common Mistakes:
  • Using ^ instead of .^ in anonymous functions
  • Assuming integral accepts scalar-only functions
  • Ignoring vectorized operations

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes