
170 Accelerating MATLAB
®
Performance
disp(max(abs(realGradient - y2)))
5.932e-09
% Use diff - 3x faster but much less accurate than gradient()
y3 = [1,diff(y)/dt];
⇨ Elapsed time is 0.000709 seconds.
disp(max(abs(realGradient - y3)))
5e-05
The gradient function computes at each data point the central difference between the
data points on both sides of the point. This provides a more accurate result than diff, which
simply computes the difference of each adjacent pair of data points. Some alternative algo-
rithms have been suggested for varying data sizes.
461
As an alternative, consider using Jan Simon’s DGradient utility on the File Excha ...