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

Please provide working code that can be easily understood. Thanks! Derivative of

ID: 3535681 • Letter: P

Question


Please provide working code that can be easily understood.


Thanks!


Derivative of a Function The derivative of a continuous function f(x) is defined by the equation d/dx f(x) = f(x + Delta x) - f(x)/Delta x (5-16) In a sampled function, this definition becomes f'(xi) = f(xi+1) - f(xi)/Delta x (5-17) where Delta x = xi+1 - xi. Assume that a vector vect contains nsamp samples of a function taken at a spacing of dx per sample. Write a function that will calculate the derivative of this vector from Equation (5-17). The function should check to make sure that dx is greater than zero to prevent divide-by-zero errors in the function. To check your function, you should generate a data set whose derivative is known and compare the result of the function with the known correct answer. A good choice for a test function is sin x. From elementary calculus, we know that d/dx (sin x) = cos x. Generate an input vector containing 100 values of the function sin x starting at x = 0 and using a step size Delta x of 0.05. Take the derivative of the vector with your function, and then compare the resulting answers to the known correct answer. How close did your function come to calculating the correct value for the derivative?

Explanation / Answer

Save the following as derivative.m

function dy = derivative ( nsamp1,nsamp,dx ) % taking inputs of nsamp(i+1) and nsamp(i) at the same time

if(dx)>0

dy= (nsamp1 - nsamp)/(dx); % the given foemula for calculating derivative at a point

end

end


save this with any other file name in the same directory


clear all

clc

dx=0.05

x=0:dx:5 % 101 samples to obtain 100 derivatives

nsamp=sin(x) % sample values of sin

for i=1:100

dy(i)=derivative(nsamp(i+1) ,nsamp(i) ,dx); % derivative through formula

end

disp(dy)

dyo=cos(x) % original derivative

for i=1:100

error(i)=((dyo(i)-dy(i))/dyo(i))*100 ;

end

disp('Error is :') % Error in percent

disp( error)

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