Bird
0
0

Find the error in this MATLAB code snippet for forward difference:

medium📝 Debug Q7 of 15
MATLAB - Numerical Methods
Find the error in this MATLAB code snippet for forward difference:
f = @(x) log(x);
x = 2;
h = 0.001;
df = (f(x+h) - f(x)) * h;
AUsing log instead of ln
BMultiplying by h instead of dividing by h
Cx should be a vector
Dh should be larger for accuracy
Step-by-Step Solution
Solution:
  1. Step 1: Check forward difference formula

    Forward difference is (f(x+h) - f(x)) / h, not multiplied by h.
  2. Step 2: Identify the error in code

    Code multiplies by h, which is incorrect and will give wrong derivative value.
  3. Final Answer:

    Multiplying by h instead of dividing by h -> Option B
  4. Quick Check:

    Forward difference divides by h [OK]
Quick Trick: Divide difference by h, don't multiply [OK]
Common Mistakes:
  • Multiplying difference by h
  • Confusing log with ln (both same in MATLAB)
  • Thinking h must be large for accuracy

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes