MATLAB: Creating a code that calculates forward difference from a set of data

data setforward differenceMATLAB

clc
clear all
close all
x = [0, 10, 20, 30, 40, 50]; %time in minutes
y = [0, 60, 120, 280, 150, 40]; %distance travelled in x minutes
xForward = x(1:end-1) % define the domains to perform forward differences for x
yForward = y(1:end-1) % define the domains to perform forward differences for y
diff(y) %used to verify the forward difference calculated above
I have been able to calculate forward difference using the 'diff' command, but I'm unable to do so with my attempt at writing forward difference coding. Code currently, only returns the x and y values. Is there some iteration/loop or other code entity I'm missing?

Best Answer

x = [0, 10, 20, 30, 40, 50]; %time in minutes
y = [0, 60, 120, 280, 150, 40]; %distance travelled in x minutes
xForward = x(1:end-1); % define the domains to perform forward differences for x
yForward = y(1:end-1); % define the domains to perform forward differences for y
diffX = x(2:end) - xForward;
diffY = y(2:end) - yForward;
diff(y) %used to verify the forward difference calculated above