Cody

# Problem 46053. Construct finite difference approximations of derivatives

Solution 2752846

Submitted on 28 Jul 2020 by Tim
• Size: 85
• This is the leading solution.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
% First-order forward difference for the first derivative n = 1; terms = [1 0]; coeff_correct = [1 -1]; errOrder_correct = 1; errCoeff_correct = -1/2; [coeff,errOrder,errCoeff] = FDderiv(n,terms); assert(all(abs(coeff-coeff_correct) < 1e-6)) assert(isequal(errOrder,errOrder_correct)) assert(abs((errCoeff-errCoeff_correct)/errCoeff_correct) < 1e-4)

2   Pass
% First-order backward difference for the first derivative n = 1; terms = [0 -1]; coeff_correct = [1 -1]; errOrder_correct = 1; errCoeff_correct = 1/2; [coeff,errOrder,errCoeff] = FDderiv(n,terms); assert(all(abs(coeff-coeff_correct) < 1e-6)) assert(isequal(errOrder,errOrder_correct)) assert(abs((errCoeff-errCoeff_correct)/errCoeff_correct) < 1e-4)

3   Pass
% Second-order centered difference for the first derivative n = 1; terms = [1 -1]; coeff_correct = [1/2 -1/2]; errOrder_correct = 2; errCoeff_correct = -1/6; [coeff,errOrder,errCoeff] = FDderiv(n,terms); assert(all(abs(coeff-coeff_correct) < 1e-6)) assert(isequal(errOrder,errOrder_correct)) assert(abs((errCoeff-errCoeff_correct)/errCoeff_correct) < 1e-4)

4   Pass
% Second-order forward difference for the first derivative n = 1; terms = [2 1 0]; coeff_correct = [-1/2 2 -3/2]; errOrder_correct = 2; errCoeff_correct = 1/3; [coeff,errOrder,errCoeff] = FDderiv(n,terms); assert(all(abs(coeff-coeff_correct) < 1e-6)) assert(isequal(errOrder,errOrder_correct)) assert(abs((errCoeff-errCoeff_correct)/errCoeff_correct) < 1e-4)

5   Pass
% Second-order backward difference for the first derivative n = 1; terms = [0 -1 -2]; coeff_correct = [3/2 -2 1/2]; errOrder_correct = 2; errCoeff_correct = 1/3; [coeff,errOrder,errCoeff] = FDderiv(n,terms); assert(all(abs(coeff-coeff_correct) < 1e-6)) assert(isequal(errOrder,errOrder_correct)) assert(abs((errCoeff-errCoeff_correct)/errCoeff_correct) < 1e-4)

6   Pass
% Fourth-order centered difference for the first derivative n = 1; terms = [2 1 0 -1 -2]; coeff_correct = [-1 8 0 -8 1]/12 errOrder_correct = 4; errCoeff_correct = 1/30; [coeff,errOrder,errCoeff] = FDderiv(n,terms); assert(all(abs(coeff-coeff_correct) < 1e-6)) assert(isequal(errOrder,errOrder_correct)) assert(abs((errCoeff-errCoeff_correct)/errCoeff_correct) < 1e-4)

coeff_correct = -0.0833 0.6667 0 -0.6667 0.0833

7   Pass
% Second-order centered difference for the second derivative n = 2; terms = [1 0 -1]; coeff_correct = [1 -2 1]; errOrder_correct = 2; errCoeff_correct = -1/12; [coeff,errOrder,errCoeff] = FDderiv(n,terms); assert(all(abs(coeff-coeff_correct) < 1e-6)) assert(isequal(errOrder,errOrder_correct)) assert(abs((errCoeff-errCoeff_correct)/errCoeff_correct) < 1e-4)

8   Pass
% Fourth-order centered difference for the second derivative n = 2; terms = [2 1 0 -1 -2]; coeff_correct = [-1 16 -30 16 -1]/12; errOrder_correct = 4; errCoeff_correct = 1/90; [coeff,errOrder,errCoeff] = FDderiv(n,terms); assert(all(abs(coeff-coeff_correct) < 1e-6)) assert(isequal(errOrder,errOrder_correct)) assert(abs((errCoeff-errCoeff_correct)/errCoeff_correct) < 1e-4)

9   Pass
% Second-order centered difference for the third derivative n = 3; terms = [2 1 0 -1 -2]; coeff_correct = [1/2 -1 0 1 -1/2]; errOrder_correct = 2; errCoeff_correct = -1/4; [coeff,errOrder,errCoeff] = FDderiv(n,terms); assert(all(abs(coeff-coeff_correct) < 1e-6)) assert(isequal(errOrder,errOrder_correct)) assert(abs((errCoeff-errCoeff_correct)/errCoeff_correct) < 1e-4)

10   Pass
% Second-order centered difference for the fourth derivative n = 4; terms = [2 1 0 -1 -2]; coeff_correct = [1 -4 6 -4 1]; errOrder_correct = 2; errCoeff_correct = -1/6; [coeff,errOrder,errCoeff] = FDderiv(n,terms); assert(all(abs(coeff-coeff_correct) < 1e-6)) assert(isequal(errOrder,errOrder_correct)) assert(abs((errCoeff-errCoeff_correct)/errCoeff_correct) < 1e-4)

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!