Bird
Raised Fist0
MATLABdata~10 mins

Numerical differentiation in MATLAB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Numerical differentiation
Start with function f(x)
↓
Choose point x0 and step h
↓
Calculate f(x0 + h) and f(x0 - h)
↓
Apply formula: (f(x0 + h) - f(x0 - h)) / (2*h)
↓
Result: Approximate derivative at x0
↓
End
Numerical differentiation approximates the slope of a function at a point by using values of the function near that point.
Execution Sample
MATLAB
f = @(x) x.^2;
x0 = 2;
h = 0.01;
derivative = (f(x0 + h) - f(x0 - h)) / (2*h);
This code approximates the derivative of f(x) = x^2 at x = 2 using a small step h.
Execution Table
StepExpressionEvaluationResult
1Calculate f(x0 + h)f(2 + 0.01)4.0401
2Calculate f(x0 - h)f(2 - 0.01)3.9601
3Subtract f(x0 + h) - f(x0 - h)4.0401 - 3.96010.08
4Divide by 2*h0.08 / (2*0.01)4
5ResultDerivative at x04
💡 Finished calculation: derivative approximated as 4 at x = 2
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
f(x0 + h)-4.04014.04014.04014.04014.0401
f(x0 - h)--3.96013.96013.96013.9601
Numerator---0.080.080.08
Denominator----0.020.02
derivative-----4
Key Moments - 2 Insights
Why do we use (f(x0 + h) - f(x0 - h)) instead of just (f(x0 + h) - f(x0))?
Using values on both sides (x0 + h and x0 - h) gives a better approximation by balancing errors, as shown in steps 1 and 2 of the execution_table.
What happens if h is too large or too small?
If h is too large, the approximation is less accurate; if too small, rounding errors increase. The chosen h=0.01 balances this, as seen in the stable results in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of f(x0 - h) at step 2?
A0.08
B4.0401
C3.9601
D4
💡 Hint
Check the 'Evaluation' column in row for step 2 in execution_table.
At which step is the numerator (f(x0 + h) - f(x0 - h)) calculated?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the subtraction operation in the 'Expression' column in execution_table.
If h was doubled to 0.02, how would the denominator in step 4 change?
AIt would become 0.04
BIt would become 0.01
CIt would stay 0.02
DIt would become 0.08
💡 Hint
Denominator is 2*h, so doubling h doubles the denominator (see step 4 denominator in variable_tracker).
Concept Snapshot
Numerical differentiation approximates f'(x0) by (f(x0 + h) - f(x0 - h)) / (2*h).
Choose a small step h for accuracy.
This is called the central difference method.
It uses function values near x0 to estimate slope.
Useful when derivative formula is unknown or complex.
Full Transcript
Numerical differentiation finds the slope of a function at a point by using nearby function values. We pick a point x0 and a small step h. Then we calculate the function at x0 + h and x0 - h. Subtract these two values and divide by 2*h. This gives an approximate derivative at x0. For example, for f(x) = x^2 at x=2 with h=0.01, the derivative is about 4. This method balances errors by using points on both sides. Choosing h carefully is important to avoid big errors or rounding problems.

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