Creating a video giving oscillatory motion to a point

1 view (last 30 days)
How do I create a video having a single point (or a solid circle) moving in a oscillatory motion with a predefined frequency and amplitude?
  2 Comments
Soumya Kedia
Soumya Kedia on 17 Dec 2019
Yes. Basically I just want any object to be having to and fro motion with a fixed frequency and amplitude.

Sign in to comment.

Accepted Answer

KSSV
KSSV on 17 Dec 2019
A = 0.5 ;
f = 1 ;
t = linspace(0,10,100) ;
x = A*sin(2*pi*f*t) ;
h = plot(0.,x(1),'s','MarkerSize',10) ;
for i = 1:length(x)
set(h,'YData',x(i)) ;
drawnow
end
If you want more, you may refer:
  2 Comments
Soumya Kedia
Soumya Kedia on 17 Dec 2019
Thank you so much. This is exactly what I needed.
Soumya Kedia
Soumya Kedia on 17 Dec 2019
I have added few lines to help me save the video. The problem is, for each frame, the y axis keeps changing and I'm unable to fix it, and hence my video is not proper.
Can you help with that?
Here is the code that I'm using now.
A = 0.5 ;
f = 1 ;
t = linspace(0,1,100) ;
x = A*sin(2*pi*f*t) ;
h = plot(0,x(1),'o','MarkerSize',10) ;
xlim([-1 1]);
%% Initialize video
myVideo = VideoWriter('myVideoFile'); %open video file
myVideo.FrameRate = 10; %can adjust this, 5 - 10 works well for me
open(myVideo)
for i = 1:length(x)
set(h,'YData',x(i)) ;
pause(0.01) %Pause and grab frame
frame = getframe; %get frame
writeVideo(myVideo, frame);
end
close(myVideo)

Sign in to comment.

More Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 17 Dec 2019
Edited: KALYAN ACHARJYA on 17 Dec 2019
Here is the MATLAB File Exchange File for Pendulum Link
The oscillatory motion of a simple pendulum is simulated using Euler-Cromer method.
I have remove the energy calculation part, hope you can change change the amplitude and frequency of the pendulum.
%-------------------------------------------------------------------------
%---Simple pendulum using Euler-Cromer Numerical Method------------------
%Credit:Sathyanarayan Rao
%------------------------------------------------------------------------
clc;
clear all;
close all;
L= 1; % Length of Pendulum
g=9.8; % Gravity
M=1; % mass
N=300; % Maximum number of time steps
dt=0.01; % Step size for time
w(1:N)=0; % Angular velocity array
Theta(1:N)=0; % Angular coordinate array
t(1:N)=0; % Time array
Theta(1)=0.5; % Initial position of pendulum
for i=1:N-1
w(i+1)=w(i)-(g/L)*Theta(i)*dt;
Theta(i+1)=Theta(i)+w(i+1)*dt;
X(i)=L*sin(Theta(i)); % polar to cartesian
Y(i)=-L*cos(Theta(i));
t(i+1)=t(i)+dt;
end
for i=1:N
figure(1)
plot(X(i),Y(i),'.','markersize',65);
axis([-0.5 0.5 -1.05 0]);
line([0 X(i)], [0 Y(i)],'Linewidth',2);
xlabel('X ');
ylabel('Y');
titlestring = ['pendulum motion at t =',num2str(i*dt), 'seconds'];
title(titlestring ,'fontsize',14);
h=gca;
get(h,'FontSize') ;
set(h,'FontSize',14);
fh = figure(1);
set(fh, 'color', 'white');
Yr=getframe;
end
movie(Yr)
  1 Comment
Soumya Kedia
Soumya Kedia on 17 Dec 2019
Thank you, the code works, but I just needed a point in a straight line going to and fro, since I need that to ckeck another one of my code and i need to find the motion of the centroid to check it's oscillatory. I'll see if I can make changes in this, or get an idea from this one to get what I want.

Sign in to comment.

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!