4-point first derivative
17 views (last 30 days)
Show older comments
I am given data t=[0 1 2 3 4 5 6 7 8 9 10] and
y(t)=[1 2.7 5.8 6.6 7.5 9.9 10.2 11.2 12.6 13.6 15.8]
and have to evaluate the derivative of y at each given t value
using the following finite difference schemes
at 4-point first derivative
My code at finite difference is
t= 0: 1: 10;
y= [1 2.7 5.8 6.6 7.5 9.9 10.2 11.2 12.6 13.6 15.8];
n=length(y);
dfdx=zeros(n,1);
dfdx(t)=(y(2)-y(1))/(t(2)-t(1));
for i=2:n-1
dfdx(1)=(y(i+1)-y(i-1))/(t(i+1)-t(i-1));
end
dfdx(n)=(y(n)-y(n-1))/(t(n)-t(n-1));
but I have interesting of method of 4-point first derivative for more accuracy thanks in avance!
2 Comments
Aquatris
on 3 Aug 2018
Please format the question properly and include the "following scheme" for us to be able to help.
Answers (1)
Aquatris
on 3 Aug 2018
I found a source where the equations for the differentiation are shown, with some typos. Here is an example code, where d4 is 4 point d5 is 5 point differentiation.
dt = 1e-3;
t = 0:0.001:20;
x = sin(0.4*t)+exp(-0.9*t); % sample signal
xd = 0.4*cos(0.4*t)-0.9*exp(-0.9*t);% sample signals exact derivative
n=length(x);
d4=zeros(size(x));
d5=zeros(size(x));
for j = 3:n-2;
d4(j) = -1/6/dt*(-2*x(j+1)-3*x(j)+6*x(j-1)-x(j-2));
d5(j)= 1/12/dt*(x(j-2) - 8.*x(j-1) + 8.*x(j+1) - x(j+2));
end
d4(1:2)=d4(3);
d4(n-1:n)=d4(n-2);
d5(1:2)=d5(3);
d5(n-1:n)=d5(n-2);
plot(t,xd,t,d4,'r--',t,d5,'m-.') % comparison plot
6 Comments
Mohammad Ezzad Hamdan
on 13 Mar 2019
What do you mean by the terms below;
d5(1:2)=d5(3);
d5(n-1:n)=d5(n-2);
Aquatris
on 13 Mar 2019
It is not possible to calculate the first 2 or last 2 elements for the d5, I just equate them to some value. For the last 2 elements I hold the last calculated value of d5 and for the first 2 elements I hold the first calculated value. Depending on the application this might be acceptable.
See Also
Categories
Find more on Mathematics and Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!