filename = 'MyAnimation.gif';
miny = min(W1); minminy = min(miny);
maxy = max(W1); maxmaxy = max(maxy);
H = plot(nan, nan, '-b', nan, nan, '-r', 'LineWidth', 3);
H(1).XData = x; H(1).YData = W1(:,i);
H(2).XData = x; H(2).YData = W2(:,i);
axis([min(x) max(x) minminy maxmaxy])
imwrite(F.cdata, F.colormap, filename, 'writemode', 'overwrite', 'DelayTime', 1/30);
FrameSize = [size(F.cdata,1), size(F.cdata,2)];
imwrite( imresize(F.cdata, FrameSize), F.colormap, filename, 'writemode', 'append');
Caution: when your x is not constant and you do not know ahead of time what the maximum and minimum possible x are, then you can run into situations where a captured frame might not be exactly the same size as previous frames. The imresize() of frames after the first frame ensures that particular problem cannot occur. But when it would have occurred, the frame might shift slightly visually -- the right border (especially) might flicker moving back and forth a few pixels.
This problem can be avoided if the x values are constant for all iterations, or at least if the minimum and maximum x and y are known ahead of time.