Here is the matlab code: ex.m.
And here is the matlab code for the piecewise linear function: sp1.m.
Here is the output I got: diary.
And here is the plot: