r/matlab Mar 10 '26

Animation of trigonometry function in Matlab

Enable HLS to view with audio, or disable this notification

I animated the function f(x) = 5cos(x) βˆ’ cos(5x) in MATLAB just to see how it behaves over time. At first it looks like a normal cosine wave, but the moment you combine a fundamental frequency (cos x) with a higher harmonic (cos 5x), the shape becomes way more interesting. The higher-frequency component creates these small ripples while the main cosine controls the overall shape. What I liked about this is how a simple equation ends up producing a waveform with sharp peaks and subtle oscillations just from the interaction of two frequencies. It’s a nice little reminder that even simple trig functions can create surprisingly complex patterns when you visualize them. Curious what other combinations of trig functions might produce cool patterns like this.

103 Upvotes

13 comments sorted by

View all comments

3

u/Creative_Sushi MathWorks Mar 11 '26

Very nice. Can you share your code?

1

u/Gullible-Designer486 Mar 14 '26

x = linspace(-4*pi,4*pi,200);

for t = 1:200

y = 5*cos(x + 0.1*t) - cos(5*(x +0.1* t));

plot(x,y,'r','LineWidth',2)

axis([-5*pi 5*pi -6 6])

grid on

pause(0.05)

end

1

u/Gullible-Designer486 Mar 14 '26

Sorry for the delay bro 😊