MATLAB: Sum two plots and plot the result

sum plots

Is there a simple way to take the sum of two plots that overlap and plot the result? X and Y are the same length for both.
Test.jpg

Best Answer

One approach (assuming that they are functions of the same independent variable vector, so in addition to being the same lengths, they are evaluated at the same values of the independent variable):
XplusY = sum([X(:),Y(:)],2);
This creates a column vector result, since we don’t know the sizes of the original ‘X’ and ‘Y’. If you want it as a row vector, transpose it.
To sum only the values where ‘X’ is greater than or equal to ‘Y’:
L = X >= Y;
XL = X(L);
YL = Y(L);
XplusY = sum([XL(:),YL(:)],2);
since I am not certain what ‘overlap’ means here.