I think that your work is ok.
First, one minor correction. In the line when you plot signal "y", you wrote:
plot(w,y);
but it should be
plot(t,y);
since "y" is in the time domain so you should put "t", not "w".
Second, if you preform an FFT of "y" like you did with "x", and plot the result, you will see on the output of filter only signal of 20Hz (and that is ok since you designed filter with Fstop=40Hz), see picture below.
The distortion that you are talking about is consequence of delay introduced by filter. If you take a look at your "fdatool" picture, you'll notice in upper left corner that the order of filter is 101. Help in Matlab says that "filter" command is implemented as "direct form II transposed structure". That means that the output samples of signal "y" in different time moments are:
y(0) = a(0)*x(0)
y(1) = a(0)*x(1) + a(1)*x(0)
y(2) = a(0)*x(2) + a(1)*x(1) + a(2)*x(0)
y(3) = a(0)*x(3) + a(1)*x(2) + a(2)*x(1) + a(3)*x(0)
...
...
(convolution of input samples and filter coefficients)
So, in the beginning only x(0) gives the contribution to the output, and as the time goes, more and more input samples are contributing to the output.
This means that the output signal will depend upon the length of the filter (in this case is 101), filter coefficients, and input samples. In this case, you have lowpass FIR filter, which has linear phase characteristic (constant group delay). In case of IIR filters it is different, but then you won't have linear phase characteristics.
If you specify filter order in "fdatool" to let's say 10, you will see that this "distortion" is very short.
Hope this helps a little