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 Kommentare

Aquatris
Aquatris am 3 Aug. 2018
Please format the question properly and include the "following scheme" for us to be able to help.
alburary daniel
alburary daniel am 3 Aug. 2018
I did

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Aquatris
Aquatris am 3 Aug. 2018

0 Stimmen

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 Kommentare

alburary daniel
alburary daniel am 3 Aug. 2018
I feeling confused because I do not how include the time data in the above representation for 4 and 5 points, any suggestion?
Aquatris
Aquatris am 3 Aug. 2018
Bearbeitet: Aquatris am 3 Aug. 2018
dt is your timestep information. Which part are you confused with?
Edit: My bad. There is a typo in the code. It should be;
dt = 1e-3;
t = 0:dt:20;
to make it clear dt is the time step. Also I thought it was clear t is the time.
alburary daniel
alburary daniel am 3 Aug. 2018
Bearbeitet: alburary daniel am 3 Aug. 2018
if true
% code
end
t= 0:dt: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=(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));
m=length(y);
d4=zeros(size(y));
d5=zeros(size(y));
for j = 3:m-2 d4(j) = -1/6/dt*(-2*y(j+1)-3*y(j)+6*y(j-1)-y(j-2)); d5(j)= 1/12/dt*(y(j-2) - 8.*y(j-1) + 8.*y(j+1) - y(j+2)); end
d4(1:2)=d4(3); d4(m-1:m)=d4(m-2); d5(1:2)=d5(3); d5(m-1:m)=d5(m-2); plot(t,dfdx,'blue',t,d4,'r--',t,d5,'m-.')
solutions are not equal
Aquatris
Aquatris am 3 Aug. 2018
I recommend you look into limitations of numeric differentiation to understand the reason. Feels like you are not that familiar.
Mohammad Ezzad Hamdan
Mohammad Ezzad Hamdan am 13 Mär. 2019
What do you mean by the terms below;
d5(1:2)=d5(3);
d5(n-1:n)=d5(n-2);
Aquatris
Aquatris am 13 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 3 Aug. 2018

Kommentiert:

am 13 Mär. 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by