Question
Please help!
The derivative of a continuous function. f(x), is defined by the equation With a sampled function, this definition becomes where Delta x = Xi+1 Assume that a vector, v, contains n samples of a function taken at a uniform spacing dx per sample. Write a function mydiff (v,dx) that will calculate the derivative of the sampled function. The function must ensure that dx 0 to prevent divide-by-zero errors. To check your function, write a script that uses the MATLAB function sin x to generate the sampled data for 0 x 2n. Use your script to calculate the derivative. Since calculus tells us that d(sinx)/dx = cos x, your script should plot the value of the derivative as calculated by your function against the value of cos x. The plot should plot the values of the calculated and analytical derivatives against the index of the values, not against the values of x.
Explanation / Answer
Please rate positively .
copy the following code and save it as a .m file and run
clc
clear all
close all
dx= 2*pi/500;
for i = 1:500
j= i*dx;
A(i)= sin(j);
C(i) = cos(j);
end
A(501) = sin(2*pi/500);
for i=1:500
B(i) = (A(i+1)-A(i))/dx ;
end
figure
plot(B,'r')
hold on
plot(C)
hold off