These m files demonstrate, that square wave and triangular wave consists of sine components.
The more components ( harmonics) you add, more perfect wave you will get.
Similarly you can create sawtooth wave, half cycle sine wave or any periodic waveform, by adding/deleting sine cosine harmonics.
Rajan Chalotra (2020). Waveform generator using harmonics (https://www.mathworks.com/matlabcentral/fileexchange/55294-waveform-generator-using-harmonics), MATLAB Central File Exchange. Retrieved .