Write an M-file that draws a swinging pendulum in the Grand Father Clock. The st
ID: 3573147 • Letter: W
Question
Write an M-file that draws a swinging pendulum in the Grand Father Clock. The steps are as follows: Draw the pendulum weight as a circle with the center in the origin and diameter equal to 5-units; and the pendulum rod as a line starting from the origin and 32-unit long. Rotate the pendulum by angle given by user with specified simulation duration time. Let the animation starts with graphical output (PLOTS) showing pendulum swing motion, displacement and velocity vs time, and velocity vs displacement.Explanation / Answer
%This is a numerical simulation of a pendulum with a massless pivot arm.
%% User Defined Parameters
%Define external parameters
g = -9.8;
deltaTime = 1/50; %Decreasing this will increase simulation accuracy
endTime = 16;
%Define pendulum
rodPivotPoint = [2 2]; %rectangular coordinates
theta = input('Enter the angle: ');
rodLength = 32;
mass = 1; %of the bob
radius = 5; %of the bob
velocity = [0 0]; %cylindrical coordinates; first entry is radial velocity,
%second entry is angular velocity
%% Simulation
assert(radius < rodLength,'Pendulum bob radius must be less than the length of the rod.');
position = rodPivotPoint - (rodLength*[-sind(theta) cosd(theta)]); %in rectangular coordinates
%Generate graphics, render pendulum
figure;
axesHandle = gca;
xlim(axesHandle, [(rodPivotPoint(1) - rodLength - radius) (rodPivotPoint(1) + rodLength + radius)] );
ylim(axesHandle, [(rodPivotPoint(2) - rodLength - radius) (rodPivotPoint(2) + rodLength + radius)] );
rectHandle = rectangle('Position',[(position - radius/2) radius radius],...
'Curvature',[1,1],'FaceColor','g'); %Pendulum bob
hold on
plot(rodPivotPoint(1),rodPivotPoint(2),'^'); %pendulum pivot
lineHandle = line([rodPivotPoint(1) position(1)],...
[rodPivotPoint(2) position(2)]); %pendulum rod
hold off
vs = [];
ts = [];
%Run simulation, all calculations are performed in cylindrical coordinates
for time = (deltaTime:deltaTime:endTime)
drawnow; %Forces MATLAB to render the pendulum
%Find total force
gravitationalForceCylindrical = [mass*g*cosd(theta) mass*g*sind(theta)];
totalForce = gravitationalForceCylindrical;
%If the rod isn't massless or is a spring, etc., modify this line
%accordingly
rodForce = [-totalForce(1) 0]; %cylindrical coordinates
totalForce = totalForce + rodForce;
ts(end+1) = time;
acceleration = totalForce / mass; %F = ma
velocity = velocity + acceleration * deltaTime;
vs(end+1) = velocity;
rodLength = rodLength + velocity(1) * deltaTime;
theta = theta + velocity(2) * deltaTime; % Attention!! Mistake here.
% Velocity needs to be divided by pendulum length and scaled to degrees:
% theta = theta + velocity(2) * deltaTime/rodLength/pi*180;
position = rodPivotPoint - (rodLength*[-sind(theta) cosd(theta)]);
%Update figure with new position info
set(rectHandle,'Position',[(position - radius/2) radius radius]);
set(lineHandle,'XData',[rodPivotPoint(1) position(1)],'YData',...
[rodPivotPoint(2) position(2)]);
end
plot(t,v);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.