Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to calculate the forward difference approximation of the derivative.
MATLAB
h = 0.01; x = 1; f = @(x) x^2; derivative = (f(x + [1]) - f(x)) / [1];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using x instead of h as the step size.
Using zero as the step size, which causes division by zero.
✗ Incorrect
The forward difference formula uses a small step size h to approximate the derivative as (f(x+h) - f(x))/h.
2fill in blank
mediumComplete the code to calculate the central difference approximation of the derivative.
MATLAB
h = 0.01; x = 2; f = @(x) sin(x); derivative = (f(x + [1]) - f(x - [1])) / (2 * h);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different values for the forward and backward steps.
Using x instead of h for the step size.
✗ Incorrect
The central difference formula uses (f(x+h) - f(x-h)) divided by 2h to approximate the derivative.
3fill in blank
hardFix the error in the code to correctly compute the numerical derivative using backward difference.
MATLAB
h = 0.001; x = 3; f = @(x) exp(x); derivative = (f(x) - f(x [1] h)) / h;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using plus instead of minus, which changes the formula.
Using multiplication or division operators incorrectly.
✗ Incorrect
The backward difference formula subtracts the function value at (x - h) from f(x), so the operator must be '-'.
4fill in blank
hardFill both blanks to create a function that returns the central difference derivative approximation.
MATLAB
function d = central_diff(f, x, [1]) d = (f(x + [2]) - f(x - h)) / (2 * h); end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using x or a constant instead of the step size variable.
Using the function name f instead of the step size.
✗ Incorrect
The function uses h as the step size, so both blanks should be h to correctly compute the central difference.
5fill in blank
hardFill all three blanks to create a function that computes the forward difference derivative with input validation.
MATLAB
function d = forward_diff([1], [2], [3]) if h <= 0 error('Step size must be positive'); end d = (f(x + h) - f(x)) / h; end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing the order of inputs.
Using variable names not related to the function inputs.
✗ Incorrect
The function inputs are the function handle f, the point x, and the step size h, in that order.