0
0
MATLABdata~10 mins

Numerical differentiation in MATLAB - Step-by-Step Execution

Choose your learning style9 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.