central difference method
10 views (last 30 days)
Show older comments
abdulhadi khalifa
on 18 Apr 2012
Edited: Muhammad Hammad Malik
on 16 Feb 2021
How can I calculate the central difference for set of data using matlab If I have big data.Could any one help me to do it for this small data so I can I apply to my data X 0.225 0.30 0.35 0.40 0.45 0.475 Y 0.251 0.90 2.02 3.63 7.2 9.800
0 Comments
Accepted Answer
Jan
on 18 Apr 2012
You can simply use:
gradient(X, Y)
Then the first and the last element are forward and backward differences respectively. But all interior elements are central differences.
For large data sets FEX: DGradient is faster (10 to 16 times) than Matlab's gradient. In addition it can calulate the 2nd order approximation, when X is not uniformly distributed.
3 Comments
Jan
on 15 Feb 2021
t = linspace(0, 2*pi, 1000);
x = sin(t);
dx = (x(11:5:end) - x(1:5:end-10)) / (10 * diff(t(1:2)));
plot(t(1:5:end-5), cos(t(1:5:end-5)), 'ro');
hold on
plot(t(5:5:end-10),dx)
Muhammad Hammad Malik
on 15 Feb 2021
Edited: Muhammad Hammad Malik
on 16 Feb 2021
@Jan thanks for the kind response. i have checked your code, but unable to understand well. why you use 11:5 and cos.
pls see what i have done, i am attaching here, the issue is i am unable to add window overlap.I also can change the window size to bigger or smaller, not restricted to use just 10.
The one you mentioned is working but how i can plot that with my time vector "IMU_ULISS.time" (1x25805), and i changed this time vector to plot mine as you can see.
Grad =gradient(Data_filtered(1:window:end),window)
figure;
plot(IMU_ULISS.time(1:window:end),Grad)
time_modified=IMU_ULISS.time(1:window:end)
hold onplot(time_modified(ceil(find(IMU_ULISS.step_instant_target(1,:)>0)/window)),Grad(ceil(find(IMU_ULISS.step_instant_target(1,:)>0)/window)),'r');
hold on
More Answers (1)
abdulhadi khalifa
on 18 Apr 2012
1 Comment
Jan
on 18 Apr 2012
The output of GRADIENT *is* the slope. See my former answer.
It would be more helpful if you reply to the different ideas you got in your other postings concerning this problem. Simply posting the problem repeatedly in new threads wastes the time of all who want to assist.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!