Bird
Raised Fist0
MATLABdata~20 mins

Numerical differentiation in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Numerical Differentiation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of forward difference approximation
What is the output of this MATLAB code that approximates the derivative of f(x) = x^2 at x = 2 using the forward difference method with h = 0.01?
MATLAB
f = @(x) x.^2;
x = 2;
h = 0.01;
derivative = (f(x + h) - f(x)) / h;
disp(derivative);
A4.01
B4.00
C3.99
DError: Undefined function or variable 'f'
Attempts:
2 left
💡 Hint
Recall the formula for forward difference: (f(x+h) - f(x)) / h.
❓ Predict Output
intermediate
2:00remaining
Output of central difference approximation
What is the output of this MATLAB code that approximates the derivative of f(x) = sin(x) at x = pi/4 using the central difference method with h = 0.001?
MATLAB
f = @(x) sin(x);
x = pi/4;
h = 0.001;
derivative = (f(x + h) - f(x - h)) / (2*h);
disp(derivative);
A0.7060
B0.7070
C0.7071
DError: Index exceeds matrix dimensions.
Attempts:
2 left
💡 Hint
Central difference formula is (f(x+h) - f(x-h)) / (2*h).
🔧 Debug
advanced
2:00remaining
Identify the error in backward difference code
This MATLAB code attempts to compute the derivative of f(x) = exp(x) at x = 1 using the backward difference method with h = 0.01. What error will it produce when run?
MATLAB
f = @(x) exp(x);
x = 1;
h = 0.01;
derivative = (f(x) - f(x - h)) / h;
AIndex exceeds matrix dimensions.
BNo error; outputs approximately 2.7183
CSyntaxError: Missing semicolon
DUndefined function or variable 'h'
Attempts:
2 left
💡 Hint
Check if the code syntax is correct and variables are defined.
❓ Predict Output
advanced
2:00remaining
Output of second derivative approximation
What is the output of this MATLAB code that approximates the second derivative of f(x) = x^3 at x = 2 using the central difference formula with h = 0.001?
MATLAB
f = @(x) x.^3;
x = 2;
h = 0.001;
second_derivative = (f(x + h) - 2*f(x) + f(x - h)) / (h^2);
disp(second_derivative);
A12.0000
BError: Undefined function or variable 'second_derivative'
C13.0000
D11.9990
Attempts:
2 left
💡 Hint
Recall the second derivative central difference formula: (f(x+h) - 2f(x) + f(x-h)) / h^2.
🧠 Conceptual
expert
2:00remaining
Error behavior in numerical differentiation
Which statement correctly describes the error behavior when using the central difference method to approximate the first derivative of a smooth function as the step size h approaches zero?
AThe error increases exponentially as h decreases.
BThe error is constant regardless of h.
CThe error always decreases as h approaches zero without limit.
DThe error decreases linearly with h and then increases due to round-off errors for very small h.
Attempts:
2 left
💡 Hint
Consider both truncation and round-off errors in numerical differentiation.

Practice

(1/5)
1. Which method in numerical differentiation uses points on both sides of the target point to estimate the derivative?
easy
A. Forward difference method
B. Euler's method
C. Backward difference method
D. Central difference method

Solution

  1. Step 1: Understand difference methods

    Forward difference uses the point and the next point; backward difference uses the point and the previous point.
  2. Step 2: Identify method using both sides

    Central difference uses points before and after the target point for better accuracy.
  3. Final Answer:

    Central difference method -> Option D
  4. Quick Check:

    Both sides points = Central difference [OK]
Hint: Central difference uses points on both sides [OK]
Common Mistakes:
  • Confusing forward with central difference
  • Thinking Euler's method is for differentiation
  • Assuming backward difference uses both sides
2. Which of the following MATLAB code snippets correctly computes the forward difference approximation of the derivative of function f at point x with step size h?
easy
A. df = (f(x+h) - f(x)) / h;
B. df = (f(x) - f(x-h)) / h;
C. df = (f(x+h) - f(x-h)) / (2*h);
D. df = (f(x) + f(x+h)) / h;

Solution

  1. Step 1: Recall forward difference formula

    Forward difference formula is (f(x+h) - f(x)) / h.
  2. Step 2: Match code to formula

    df = (f(x+h) - f(x)) / h; matches this formula exactly.
  3. Final Answer:

    df = (f(x+h) - f(x)) / h; -> Option A
  4. Quick Check:

    Forward difference = (f(x+h)-f(x))/h [OK]
Hint: Forward difference subtracts current from next point [OK]
Common Mistakes:
  • Using backward difference formula instead
  • Dividing by 2*h instead of h
  • Adding function values instead of subtracting
3. Consider the MATLAB code:
f = @(x) x^2;
x = 2;
h = 0.01;
df = (f(x+h) - f(x-h)) / (2*h);

What is the value of df approximately?
medium
A. 5.00
B. 2.00
C. 4.00
D. 3.99

Solution

  1. Step 1: Calculate f(x+h) and f(x-h)

    f(2+0.01) = (2.01)^2 = 4.0401; f(2-0.01) = (1.99)^2 = 3.9601.
  2. Step 2: Compute central difference

    df = (4.0401 - 3.9601) / (2*0.01) = 0.08 / 0.02 = 4.00.
  3. Step 3: MATLAB precision

    In MATLAB, df is computed and displayed as 4.0000.
  4. Final Answer:

    4.00 -> Option C
  5. Quick Check:

    Central difference ≈ 4 [OK]
Hint: Central difference approximates slope near x [OK]
Common Mistakes:
  • Using forward difference formula instead
  • Forgetting to divide by 2*h
  • Incorrect function evaluation
4. The following MATLAB code attempts to compute the derivative using forward difference but gives incorrect results:
f = @(x) sin(x);
x = pi/4;
h = 0.001;
df = (f(x) - f(x+h)) / h;

What is the error in this code?
medium
A. Step size h is too large
B. The subtraction order is reversed; should be f(x+h) - f(x)
C. Function handle syntax is incorrect
D. Variable x should be a vector

Solution

  1. Step 1: Recall forward difference formula

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

    The code subtracts f(x+h) from f(x), reversing the order and causing incorrect sign.
  3. Final Answer:

    The subtraction order is reversed; should be f(x+h) - f(x) -> Option B
  4. Quick Check:

    Forward difference = (f(x+h)-f(x))/h [OK]
Hint: Forward difference subtracts future minus current value [OK]
Common Mistakes:
  • Reversing subtraction order
  • Using too large step size without reason
  • Misunderstanding function handle syntax
5. You want to estimate the derivative of f(x) = exp(-x^2) at x = 1 using central difference in MATLAB. Which code snippet correctly implements this with step size h = 0.0001?
hard
A. f = @(x) exp(-x^2); x = 1; h = 0.0001; df = (f(x+h) - f(x-h)) / (2*h);
B. f = @(x) exp(-x^2); x = 1; h = 0.0001; df = (f(x) - f(x-h)) / h;
C. f = @(x) exp(-x^2); x = 1; h = 0.0001; df = (f(x+h) + f(x-h)) / (2*h);
D. f = @(x) exp(-x^2); x = 1; h = 0.0001; df = (f(x+h) - f(x)) / h;

Solution

  1. Step 1: Recall central difference formula

    Central difference formula is (f(x+h) - f(x-h)) / (2*h) for better accuracy.
  2. Step 2: Match code to formula

    f = @(x) exp(-x^2); x = 1; h = 0.0001; df = (f(x+h) - f(x-h)) / (2*h); matches the central difference formula exactly with correct function and step size.
  3. Final Answer:

    f = @(x) exp(-x^2); x = 1; h = 0.0001; df = (f(x+h) - f(x-h)) / (2*h); -> Option A
  4. Quick Check:

    Central difference = (f(x+h)-f(x-h))/(2*h) [OK]
Hint: Central difference uses symmetric points around x [OK]
Common Mistakes:
  • Using forward or backward difference instead
  • Adding function values instead of subtracting
  • Dividing by h instead of 2*h
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard