Hi all,
Trying to create a moving plot of a sine wave. I'm essentially after the sine wave to start on the screen showing two cycles and then have the sine wave move while the plot moves to the right by two cycles and then back to the left by two cycles. I have this bit below to animate the sin wave but can't seem to get the plot to move as well. Cheers!
t = 0:0.1:4*pi;
y = sin(t);
for k = 1:length(t)
plot(t(k),y(k),'x')
hold on
plot(t(1:k),y(1:k))
axis([0 4*pi -1 1])
grid on
pause(0.1)
if k ~= length(t)
clf
end
end

답변 (1개)

Mrunmayee Gaikwad
Mrunmayee Gaikwad 2020년 9월 23일

0 개 추천

Hi,
To make the plot move, you will need to keep updating the ‘t’ vector in for/while loop and then plot the sine wave.
For example, this will move the plot to the right:
for j = 1:length(t)
plot(t,y)
axis([0 8*pi -1 1]) % 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
To move to left you can vary j as: j = length(t):-1:1

댓글 수: 1

Harrison Seymour
Harrison Seymour 2020년 10월 22일
Hi Mrunmayee and thank you for your answer.
That is helping me to get the wave to move along the horizontal axis.
I was wondering though how to have the sine wave cycling while it moves along the axis. With your code the wave stays staionary and moves along the axis. I wanted the wave to cycle as well as move along the axis.
I will try and attach a file for a video for you to see what i mean.
No rush on the answer and thanks again.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Animation에 대해 자세히 알아보기

질문:

2020년 9월 18일

댓글:

2020년 10월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by