Challenge

Week 3 - Solving second order ODEs

% damping coefficient = b

b = 0.05;

%gravity = g m/s^2

g = 9.81;

% length of the pendulum string = 1 metre

l = 1;

% mass in kg = m

m = 1;

% inital condition

for solving first ode it required to provide value for displacement and velocity

initial displacement of the pendulum is 0

initial velocity is 3m/s

theta_0 = [0 3 ];

%time intervals

t_span = linspace(0,20,100);

% solving ODE

[t , results] = ode45(@(t,theta) ode_func(t, theta, b, g, l, m),t_span,theta_0);

outcome = results(:,1);

%contor_command

ct = 1;

for i = 1:length(out come)

pos = out come (i);

%origin_points

x0 = 0;

y0 = 0;

% max positions

x1 = 1*sin(pos);

y1 = -1*cos(pos);

% plotting

plot([x0 x1], [y0 y1],x1, y1 ,'g','markersize',15,'linewidth',2)

axis ([-1.5 1.5 -1.5 1.5 ])

hold on

grid on M(ct) = getframe(gcf);

ct = ct+1;

end

movie(M)

videofile = VideoWriter('second_order_pendulum');

open(videofile)

writeVideo(videofile,M)

close(videofile)

% script to produce gragh

%plotting

figure(1)

plot(t,result(:,1))

hold on

xlabel ('time')

plot (t, result(:,2))

hold off

axis([0 22 -3.5 3.5])

ylabel('plot')

legend ('angular displacement/time','angular velocity/time') it is used for indicating specific line

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.