Project

simple pendulum

clear all

close all

clc

%system parameters

m=0.3;

l=0.5;

g=9.81;

b=0.05;

% for simplification

r=g/l;

k=b/(m*l);

% the pendulum ODE

f=@(t,x)[x(2);-k*x(2)-r*sin(x(1))];

%initial conditions

init=[pi/2;0];

% solve ODE.The time interval is set from 0 to 10 seconds

[t,x]=ode45(f,[0 10],int);

% plot the solution

figure(1)

plot(t,x(:,1));

grid on;

hold on;

plot(t,x(:,2),\'k\');

xlabel(\'time (s)\');

ylabel(\'Amplitude\');

legend({\'Angle(rad)\',\'Angular speed (rad/s)\');

% animation

% origin

figure(2)

O=[0 0];

axis(gca,\'equal\'); %aspect ration of the plot

axis([-0.7 0.7 -0.7 0.2]);

grid on;

%loop for animation

for i=1:length(t)

% mass point

P=L*[sin(x(i,1))-cos(x(i,1))];

%circle of origin

O_circ=viscircles(0,0.01);

%pendulum

pend = line ([O(1) P(1)],[O(2) P(2)]);

%Ball

ball=viscircles(P,0.05);

% time interval to update the plot

pause(0.001);

% delete previous objects if it is not the final loop

if i<length(t)

delete(pend);

delete(ball);

delete(O_circ);

end

end

\"\"

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.