animated sine wave continously

34 Ansichten (letzte 30 Tage)
nirwana
nirwana am 30 Mär. 2023
Kommentiert: Les Beckham am 31 Mär. 2023
Hi alll, i'd like to make sine wave with three variation and walk continously, but i end up make it uncontinously,
here the script that i modified, any suggestion to solve it?
TIA
close all
subplot(1,2,1)
t = 0:0.01:10*pi;
phase=pi;
y = cos(t+phase);
yH=hilbert(y);
for j = 1:length(t)
hold on
plot(t,y)
plot(t,0.5*imag(yH))
plot(t,y+imag(yH))
hold off
axis([0 8*pi -2 2]) % moving 2 cycles would mean the end would be 4pi + 2*2pi = 8pi
% you can keep the axis endpoints according to your need
grid on
pause(0.1)
%hold on
if j ~= length(t)
clf
end
t = t + 0.1; % used 0.1 increment as elements in t have 0.1 difference
% Also, as y is already defined earlier, this change in t will not change y
end;
  1 Kommentar
Adam Danz
Adam Danz am 30 Mär. 2023
@nirwana, creating an animation by iteratively calling plot() is very inefficient. In your demo, three lines are created and and eliminated over 3000 times. Instead, re-use the same line(s) and update the values on each iteration. @Les Beckham demonstrates this in the answer below.
Another approach is to use animatedline.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Les Beckham
Les Beckham am 30 Mär. 2023
Bearbeitet: Les Beckham am 30 Mär. 2023
This should get you started. Adjust as desired.
Note that the animation won't show here, but I tested it my local copy of Matlab.
t = linspace(0, 4*pi, 1000);
y = sin(t);
hl = plot(t, y);
grid on
xlim tight
ylim padded
for i=1:1000
set(hl, 'YData', circshift(get(hl, 'YData'), 1))
% you may want -1 here ^
% depending on which direction you want to scroll
drawnow
end
  2 Kommentare
nirwana
nirwana am 31 Mär. 2023
thanks @Les Beckham, it helps alot !!
Les Beckham
Les Beckham am 31 Mär. 2023
You are quite welcome.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Animation finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by