Matlab Tutorial on Sine Wave animated plot

Publicado el: 31 agosto 2024
en el canal de: Fishing Activity Daily
276
3

To create an animated sine wave plot in MATLAB, you can use a loop to update the plot in real-time. Here’s a step-by-step example of how to create an animated sine wave:

MATLAB Code for Animated Sine Wave Plot
...............................................................................................................
% MATLAB Code to Create an Animated Sine Wave Plot

% Define parameters
A = 1; % Amplitude
B = 2 * pi / 10; % Frequency
C = 0; % Phase shift
speed = 0.1; % Speed of animation

% Define the range for x
x = linspace(0, 20, 1000);

% Create a figure
figure;
h = plot(x, A * sin(B * x + C), 'b-', 'LineWidth', 2);
title('Animated Sine Wave');
xlabel('x');
ylabel('y');
axis([0 20 -1.5 1.5]); % Set axis limits
grid on;

% Animation loop
while ishandle(h)
for k = 1:100
% Update the phase shift
C = C + speed;

% Update the y data
y = A * sin(B * x + C);
set(h, 'YData', y);

% Pause to create animation effect
pause(0.05);
end
end
.......................................................................................................................................

Explanation
Parameters:

A: Amplitude of the sine wave.
B: Frequency of the sine wave.
C: Phase shift, which changes over time to animate the wave.
speed: Controls how quickly the wave animates.
Plot Setup:

x: Defines the range of x-values for the plot.
figure: Creates a new figure window.
plot: Plots the sine wave and sets initial properties.
Animation Loop:

while ishandle(h): Ensures that the loop continues as long as the figure is open.
for k = 1:100: Loops through a series of updates to create the animation effect.
C = C + speed: Updates the phase shift to animate the wave.
set(h, 'YData', y): Updates the y-data of the plot.
pause(0.05): Pauses briefly to control the animation speed.
Adjustments
speed: Change the speed of the animation by adjusting this value.
pause: Modify the pause duration to speed up or slow down the animation.
You can further enhance this animation by adding features such as labels, legends, or additional waveforms.


En esta página del sitio puede ver el video en línea Matlab Tutorial on Sine Wave animated plot de Duración hora minuto segunda en buena calidad , que subió el usuario Fishing Activity Daily 31 agosto 2024, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 276 veces y le gustó 3 a los espectadores. Disfruta viendo!