Projectile Motion:
____________________________________________________________________
clear; clc; close;
%data
g=9.81;
theta=60;
v0=20;
y0=50;
x0=0;
tf=20;
dt=0.1;
%eqs of motion
t=0:dt:tf;
y=y0+v0*t*sind(theta)-0.5*g*t.^2;
x=x0+v0*t*cosd(theta);
%trimming y values
ind=find(y<0);
t(ind)=[];
x(ind)=[];
y(ind)=[];
%plotting
plot(x, y, '--co');
hold on;
plot([0 60], [0 0], '-g', 'linewidth', 5)
h= plot(x0, y0, 'o', 'markerfacecolor', 'g');
for ii=2:length(x)
set(h, 'XData', x(ii));%we update already defined func. with data
set(h, 'YData', y(ii));
drawnow;
pause(0.02);
end
____________________________________________________________________
Animation of plot:
____________________________________________________________________
x=0:0.01:10;
y=sin(x);
fi=figure;
for i=1:length(x)
plot(x(i), y(i), '-oc', 'linewidth', 1);
axis([0 10, -1 1])
hold on;
drawnow;
if ~ishandle(fi)%stops after close bttn
break
end
end
____________________________________________________________________
Simple pendulum:
____________________________________________________________________
%data
t=0:0.1:9;
tht=pi/4;%initial angle
l=5;
g=9.81;
x=2;%pivot coordinates
y=2;
theta=tht*cos(sqrt(g/l)*t);
%plotting
fi=figure;
for i=1:length(t)
axis([x-l-1 x+l+1, y-l-1 y+1]);
pivot_rod=plot([x-0.5 x+0.5], [y y], 'linewidth', 2, 'r');
hold on;
string=plot([x+l*sin(theta(i)) x], [y-l*cos(theta(i)) y],'g-', 'linewidth', 2);
ball=plot(x+l*sin(theta(i)), y-l*cos(theta(i)), 'ok', 'markerfacecolor', 'y', 'markersize', 10);
trace=plot(x+l*sin(theta(i)), y-l*cos(theta(i)), 'ok', 'markerfacecolor', 'y', 'markersize', 1);
drawnow;
pause(0.03);
if ~ishandle(fi)
break
end
if i<length(t)
delete(pivot_rod);
delete(string);
delete(ball);
end
end
____________________________________________________________________
Horizontally moving pivot pendulum:
____________________________________________________________________
clear; clc; close;
%parameters:
g=9.81;
w=1;
r=2;
l=1;
theta0=pi/4;
x0=2;
y0=2;
t=0:0.1:10;
%diff. equation:
diff_eq = @(t, phi) ...
[phi(2); ...
r * w * w * cos(w * t) * cos(phi(1)) - g * sin(phi(1))/l ];
options = odeset('reltol', 1e-9);
[t, phi] = ode45(diff_eq, t, [theta0, 0], options);
%plotting:
fi=figure;
for i=1:length(t)
if ~ishandle(fi)
break
end
%xlim([x0-r-l-0.5, x0+r+l+0.5]);
hold off;
pivot_rod=plot([x0+r*cos(w*i)-0.5, x0+r*cos(w*i)+0.5], [y0 y0], 'linewidth', 2, 'b');
hold on;
%ball=plot(x0+r*cos(w*i), y0, 'ok', 'markerfacecolor', 'c', 'markersize', 10);
xlim([-5 5]);
ylim([y0-l-0.5, y0+0.5]);
axis equal;
%drawnow;
string=plot([x0+r*cos(w*i), x0+r*cos(w*i)+l*sin(phi(i, 1))], [y0, y0-l*cos(phi(i, 1))],
'linewidth', 2, 'g');
ball=plot(x0+r*cos(w*i)+l*sin(phi(i, 1)), y0-l*cos(phi(i, 1)), 'ok', 'markerfacecolor', 'c',
'markersize', 10);
pause(0.1);
%if i<length(t)
% delete(pivot_rod);
% delete(string);
% delete(ball);
%end
end
____________________________________________________________________
Vertically moving pivot pendulum:
____________________________________________________________________
clear; clc; close;
%parameters:
g=9.81;
w=2;
r=2;
l=2;
theta0=pi/4;
x0=2;
y0=2;
t=0:0.1:10;
%diff. equation:
diff_eq = @(t, theta) ...
[theta(2); ...
-r* w * w * cos(w * t)*sin(theta(1))/l - g * sin(theta(1))/l];
options = odeset('reltol', 1e-9);
[t, theta] = ode45(diff_eq, t, [theta0, 0], options);
%plotting:
fi=figure;
for i=1:length(t)
if ~ishandle(fi)
break
end
hold off;
pivot_rod=plot([x0-0.5, x0+0.5], [y0+r*cos(w*i), y0+r*cos(w*i)], 'linewidth', 2, 'b');
hold on;
string=plot([x0+l*sin(theta(i, 1)), x0], [y0+r*cos(w*i)-l*cos(theta(i, 1)), y0+r*cos(w*i)],
'linewidth', 2, 'g');
ball=plot(x0+l*sin(theta(i, 1)), y0+r*cos(w*i)-l*cos(theta(i, 1)), 'ok', 'markerfacecolor', 'c',
'markersize', 10);
xlim([-2, 5]);
ylim([-5, 8]);
axis equal;
drawnow;
pause(0.1);
%if i<length(t)
% delete(pivot_rod);
% delete(string);
% delete(ball);
%end
end
____________________________________________________________________
Pendulum attached on a pivot, moving on an ellipse:
____________________________________________________________________
clear; clc; close;
%parameters:
g=9.81;
w=3;
a=4;
b=2;
l=3;
theta0=pi/4;
x0=2;
y0=2;
t=0:0.00001:10;
%diff. equation:
diff_eq = @(t, theta) ...
[theta(2); ...
a* w * w * sin(w * t) * cos(theta(1))/l- g * sin(theta(1))/l - b * (w^2) * cos(w * t) *
sin(theta(1))/l ];
options = odeset('reltol', 1e-9);
[t, theta] = ode45(diff_eq, t, [theta0, 0], options);
%plotting:
fi=figure;
for i=1:length(t)
if ~ishandle(fi)
break
end
hold off;
pivot_rod=plot([x0+a*sin(w*i)-1, x0+a*sin(w*i)+1], [y0+b*cos(w*i), y0+b*cos(w*i)],
'linewidth', 2, 'b');
hold on;
string=plot([x0+a*sin(w*i)+l*sin(theta(i, 1)), x0+a*sin(w*i)], [y0+b*cos(w*i)-l*cos(theta(i,
1)), y0+b*cos(w*i)], 'linewidth', 2, 'g');
ball=plot(x0+a*sin(w*i)+l*sin(theta(i, 1)), y0+b*cos(w*i)-l*cos(theta(i, 1)), 'ok',
'markerfacecolor', 'c', 'markersize', 10);
xlim([-8, 8]);
ylim([-8, 8]);
axis equal;
drawnow;
pause(0.1);
%if i<length(t)
% delete(pivot_rod);
% delete(string);
% delete(ball);
%end
end
____________________________________________________________________