MATLAB please
Required information
Consider the forward finite-difference, centered finite-difference, and backward finite-difference formulas given below with O(h^2), respectively.
Forward finite-difference formula:
dy/dx ≈ (y(i+1) - y(i)) / h
Centered finite-difference formula:
dy/dx ≈ (y(i+1) - y(i-1)) / (2h)
Backward finite-difference formula:
dy/dx ≈ (y(i) - y(i-1)) / h
Second derivative formulas:
Forward finite-difference formula:
d^2y/dx^2 ≈ (y(i+2) - 2y(i+1) + y(i)) / h^2
Centered finite-difference formula:
d^2y/dx^2 ≈ (y(i+1) - 2y(i) + y(i-1)) / h^2
Backward finite-difference formula:
d^2y/dx^2 ≈ (y(i) - 2y(i-1) + y(i-2)) / h^2
Develop an M-file function that computes first- and second-derivative estimates of order O(h^2) based on the given formulas. The function's first line should be set up as function [dydx, d2ydx2] = diffeq(x,y) where x and y are input vectors of length n containing the values of the independent and dependent variables, respectively, and dydx and d2ydx2 are output vectors of length n containing the first-derivative and second-derivative estimates at each value of the independent variable. The function should generate a plot of dydx and d2ydx2 versus x. Have your M-file return an error message if the input vectors are not the same length, or the values for the independent variable are not equally spaced.
(Please upload your response/solution using the controls below.)