Question

In: Advanced Math

write a matlab script for double mass spring system with animation.

write a matlab script for double mass spring system with animation.

Solutions

Expert Solution

% call: >> double_pendulum([pi;0;pi;5;9.81;1;1;2;1],100,10,false)
% Or, simply call >> double_pendulum_init
%

function double_pendulum(ivp, duration, fps, movie)

% ---------------------------------------------------------------------
clear All; clf;
nframes=duration*fps;
sol=ode45(@double_pendulum_ODE,[0 duration], ivp);
t = linspace(0,duration,nframes);
y=deval(sol,t);
phi1=y(1,:)'; dtphi1=y(2,:)';
phi2=y(3,:)'; dtphi2=y(4,:)';
l1=ivp(8); l2=ivp(9);
% phi1=x(:,1); dtphi1=x(:,2);
% phi2=x(:,3); dtphi2=x(:,4);
% l1=ivp(8); l2=ivp(9);
h=plot(0,0,'MarkerSize',30,'Marker','.','LineWidth',2);
range=1.1*(l1+l2); axis([-range range -range range]); axis square;
set(gca,'nextplot','replacechildren');
for i=1:length(phi1)-1
if (ishandle(h)==1)
Xcoord=[0,l1*sin(phi1(i)),l1*sin(phi1(i))+l2*sin(phi2(i))];
Ycoord=[0,-l1*cos(phi1(i)),-l1*cos(phi1(i))-l2*cos(phi2(i))];
set(h,'XData',Xcoord,'YData',Ycoord);
drawnow;
F(i) = getframe;
if movie==false
pause(t(i+1)-t(i));
end
end
end
if movie==true
movie2avi(F,'doublePendulumAnimation.avi','compression','Cinepak','fps',fps)
end

% Simply run
% >> double_pendulum_init

%This script calls double_pendulum.
%
% ---------------------------------------------------------------------
phi1 = pi;
dtphi1 = 0;
phi2 = pi;
dtphi2 = 5;
g = 9.81;
m1 = 1;
m2 = 1;
l1 = 2;
l2 = 1;
duration = 100;
fps = 10;
movie = true;
clc; figure;
interval=[0, duration];
ivp=[phi1; dtphi1; phi2; dtphi2; g; m1; m2; l1; l2];
double_pendulum(ivp, duration, fps, movie);

% This function calls is called by double_pendulum.

% ---------------------------------------------------------------------

function xdot = double_pendulum_ODE(t,x)
g=x(5); m1=x(6); m2=x(7); l1=x(8); l2=x(9);
xdot=zeros(9,1);
xdot(1)=x(2);
xdot(2)=-((g*(2*m1+m2)sin(x(1))+m2(g*sin(x(1)-2*x(3))+2*(l2*x(4)^2+...
l1*x(2)^2*cos(x(1)-x(3)))*sin(x(1)-x(3))))/...
(2*l1*(m1+m2-m2*cos(x(1)-x(3))^2)));
xdot(3)=x(4);
xdot(4)=(((m1+m2)(l1*x(2)^2+g*cos(x(1)))+l2*m2*x(4)^2*cos(x(1)-x(3)))...
sin(x(1)-x(3)))/(l2*(m1+m2-m2*cos(x(1)-x(3))^2));


Related Solutions

Write the double-mass-spring equations as a 1st-order system in matrix-vector form.
Write the double-mass-spring equations as a 1st-order system in matrix-vector form.
Simulate the Spring Mass Damper System shown in Fig. Using MATLAB/Simulink, m being the mass of...
Simulate the Spring Mass Damper System shown in Fig. Using MATLAB/Simulink, m being the mass of Vibrating System in Kg. c Being the coefficient of damping in N-sec/m, K being spring stiffness in N/m. For the values m=15kg C=100 N-sec/m k=200 N/m mx''+cx'+kx=f0sinwt
MATLAB: Write as a script in the editor window of matlab. Quadratic roots. Write a program,...
MATLAB: Write as a script in the editor window of matlab. Quadratic roots. Write a program, quadroots.m, for finding the roots of the second- order polynomial ax2 + bx + c. Use the quadratic equation. The inputs are the coefficients a,b, and c and the outputs are z1 and z2. The program should produce (exactly) the following output in the Command window when run with (a, b, c) = (1, 2, −3):
If an undamped spring-mass system with a mass that weighs 24 lb and a spring constant...
If an undamped spring-mass system with a mass that weighs 24 lb and a spring constant 9 lbin is suddenly set in motion at t=0 by an external force of 180cos(8t) lb, determine the position of the mass at any time. Assume that g=32 fts2. Solve for u in feet.
If an undamped spring-mass system with a mass that weighs 6 lb and a spring constant...
If an undamped spring-mass system with a mass that weighs 6 lb and a spring constant 9 lb/in is suddenly set in motion at t=0 by an external force of 99cos(20t) lb, determine the position of the mass at any time. Assume that g=32 ft/s2. Solve for u in feet.
If an undamped spring-mass system with a mass that weighs 6 lb and a spring constant...
If an undamped spring-mass system with a mass that weighs 6 lb and a spring constant 9 lbin is suddenly set in motion at t=0 by an external force of 33cos(20t) lb, determine the position of the mass at any time. Assume that g=32 fts2. Solve for u in feet. Enclose arguments of functions in parentheses. For example, sin(2x). u(t)=?
If an undamped spring-mass system with a mass that weighs 24 lb and a spring constant...
If an undamped spring-mass system with a mass that weighs 24 lb and a spring constant 9 lbin is suddenly set in motion at t=0 by an external force of 288cos(4t) lb, determine the position of the mass at any time. Assume that g=32 fts2. Solve for u in feet. Enclose arguments of functions in parentheses. For example, sin(2x).
If an undamped spring-mass system with a mass that weighs 6 lb and a spring constant...
If an undamped spring-mass system with a mass that weighs 6 lb and a spring constant 4 lbin is suddenly set in motion at t=0 by an external force of 63cos(12t) lb, determine the position of the mass at any time. Assume that g=32 fts2. Solve for u in feet.
If an undamped spring-mass system with a mass that weighs 24 lb and a spring constant...
If an undamped spring-mass system with a mass that weighs 24 lb and a spring constant 16 lbin is suddenly set in motion at t=0 by an external force of 432cos(8t) lb, determine the position of the mass at any time. Assume that g=32 fts2. Solve for u in feet. Enclose arguments of functions in parentheses. For example, sin(2x). u(t) = ?
Part A: Write a MATLAB script to find the volume of the cylinder in gallons, as...
Part A: Write a MATLAB script to find the volume of the cylinder in gallons, as well as the tank dimensions in feet. Assume that the initial measurements are 7 meters in diameter and 11 meters tall. Display your final answers to the screen using disp and a statement without a semicolon, e.g. write the following into your script disp(‘The capacity in U.S. gallons is:’), capacity, where capacity is a variable that you defined in preceding calculations. Part B: In...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT