Matlab Tutorial on Sine Wave animated plot

Pubblicato il: 31 agosto 2024
sul canale di: 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.


In questa pagina del sito puoi guardare il video online Matlab Tutorial on Sine Wave animated plot della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Fishing Activity Daily 31 agosto 2024, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 276 volte e gli è piaciuto 3 spettatori. Buona visione!