take derivative of a ramp function in matlab

To take the derivative of a ramp function in Matlab using the diff function, first define your ramp function as a vector. Let's assume the ramp function is a simple linear equation y = mx + b for the range x = [-5:5]. We can define this ramp function in Matlab as follows:

main.m
m = 0.5;        % slope of ramp function
b = 2;          % y-intercept of ramp function
x = -5:0.1:5;   % range of function
y = m*x + b;    % ramp function defined as vector
174 chars
5 lines

To take the derivative of this ramp function in Matlab, simply use the diff function as follows:

main.m
dydx = diff(y)./diff(x);   % derivative of ramp function
57 chars
2 lines

Here, diff(y) calculates the difference between consecutive values in the y vector, and diff(x) calculates the difference between consecutive values in the x vector. Dividing these two values will give you the derivative of the ramp function.

gistlibby LogSnag