Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Task 1: A central difference for Numeric differentiation Routine (8 pts) Use MAT

ID: 3733019 • Letter: T

Question

Task 1: A central difference for Numeric differentiation Routine (8 pts) Use MATLAB to write a function called centraldiff.m that takes sample locations and function samplings at those locations as inputs and returns an approximation of the derivative at the sample points based on central finite difference method. The function should display nothing to the screen unless an error or warning occurs. The detailed specification is as follows The approximation of the derivative by central difference scheme is given as: 2 h D = centra!diff(x, fx) x : (N-element vector) numerical data for sample locations fx : (N-element vector) function data taken at locations defined in inputx D : Approximation for the first derivative of fx at a given data point. Valid call: Inputs: Output: D should have a length N Since the derivative of the first and the last point can't be found . using this method, assign zeros to the first and last elements of D Assumptions: Validation: The ith element of x corresponds to the ith element of fx and D Your code should throw an error if any of the following are true: » x and fx are not the same length either of the inputs are not vectors (e.g., a matrix) either of the inputs contains something other than numerical data Hint: functions like isvector() and isnumeric) may come in handy during input validation Do not call any specialized MATLAB functions that perform differentiation. Write it from scratch

Explanation / Answer

function D=centraldiff(x,fx) if ~(isvector(x) && isvector(fx)) error('Either of the inputs are not vectors') elseif length(x)~=length(fx) error('x and fx are not the same length') elseif ~(isnumeric(x) && isnumeric(fx)) error('Either of the inputs contains something other than numerical data') else D=zeros(1,length(x)); h=x(2)-x(1); D(2:end-1)=(fx(3:1:end)-fx(1:1:end-2))/(2*h); end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote