Challenge

Week 3 - Solving second order ODEs

1. We start with with close all clear all clc
2.The inputs for the simple pendulum are specified
3.t_span is acommand used in the ode45 solver which specifies the time span of the iterations.
4.ode45 is a function in MATLAB that solves ordinary or partial differential equations using a numerical method.
5.the function should take atleast 2 inputs to solve the equation to the approximate result.
6.The ode45 function has a syntax in which we need to specify which should be 't' and Y
and the output may be of (t,y,args) and a time span for the function to work and the initial value of the equation.
7. syntax of a ode45 solver
8.plotting the values for angular velocity and angular displacement by using plot command
9.The label command is used to label the X&Y axis.
10.Legend command is used to label the angular velocity and angular displacement.
11.To annimate the motion of simple pendulum, a for loop is used to create the specified set of points to keep the bob at different locations.
12.At the end of the for loop there will be set of positions for each and every point.
13.Finally we creat the animation movie.
Code:
function [dtheta_dt]=ode_func(t,theta,b,g,l,m);
theta1=theta(1);
theta2=theta(2);
dtheta1_dt=theta(2);
dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1);
dtheta_dt=[dtheta_dt;dtheta2_dt];
end
clear all
close all
clc
% values
b=0.05 % damping coefficient
g=9.81 % gravity(m/s^&2)
m=0.1 % mass of the bob (kg)
l=1 % length of the spring (m)
%initial condition
%angular displacement is 0 and angular velocity is 5
theta_0=[0,5];
% time span
% pendulum oscillates between 0-20 seconds
t_span=linspace(0,20,600)
% solving ODE
[t,results]=ode45(@(t,theta) ode_func(t, theta, b,g,l,m), t_span, theta_0);
%plotting
figure(1)
hold on
plot(t,results(:,1))
plot(t,results(:,2))
title('damped harmonic motion of pendulum')
xlabel('time in sec')
ylabel('angular displacement'&'angular velocity')
legend('angular displacement','angular velocity')
% for loop
% before starting the for loop, a counter value was defined
% animation
ct=1;
for i=1:length(results(:,1)
x_start=0
y_start=0
x_end =l*sin(results(i,1))
y_end=-l*cos(results(i,1))
figure(2)
plot([-1 1],[0 0],'linewidth',5,'color','g');
hold on;
plot([x_start x_end],[y_start y_end],'lineidth',3);
hold on;
plot([x_end,y_end],'o','markersize',20,'markerFacecolor','y');
title('Animation of transient behaviour of a pendulum')
axis([-1.5 1.5 -1.5 1])
pause(0.03)
hold off
%animation
M(ct)=getframe(gcf);
ct=ct+1;
end
% animation of the pendulum
movie(M)
videofile=VideoWriter('pendulum Animation.avi','uncompressed avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)

link:https://youtu.be/Rw5TtFJO6Ow

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.