Challenge

Week 4.1 - Solving second order ODEs

YOUTUBE LINK FOR ANIMATION

https://youtu.be/NxZ4eJsbXEo

MATLAB CODE

clear all
close all
clc

% inputs
l= 1; % length of the pendulam
m=1; % Mass in Kg
b=0.05; % Dampning Co-efficient
g=9.81; % Acc due to gravity in m/s^2

% Initial Inputs
theta_0 =[0;3];  % theta_0 is the intial condition corrosponding starting time.

% time span
t_span = linspace(0,20,500);

% solve ode
% For solving ode we use inbuilt matlab function name ode45 which has a
% syntax ode45(@(odefunc,timespan)
[t,thetad_v]=ode45(@(t,theta) ode_fnc(t,theta,b,g,l,m),t_span,theta_0);
% Here t and theta are the dependent and indipendent variable of our ode
% system
A_D = thetad_v(:,1); 
A_V = thetad_v(:,2);

%Animation
% Giving counter
ct=1;
for i= 1:length(A_D)
    w=A_D(i);
    x0=0; %Initial position of the Pendulum string, which is attached to the support
    y0=0; %Initial position of the Pendulum string, which is attached to the support
    
    x1=l*cos(w)
    y1=l*sin(w)
    
    % Plotting
    plot([y0 y1],-[x0 x1],'linewidth',2,'color','b')
    hold on
    plot(y1,-x1,'-o','Markersize',10)
    hold on
    plot([-1 1],[0 0]) %This can be considered as a wall or the support holding the Pendulum.
    hold off 
    axis([-2 2 -2 2])
    pause(0.003)
    M(ct)=getframe(gcf);
    ct=ct+1;
end
movie(M)
Videofile = VideoWriter('Pendulum_Animation.avi', 'uncompressed AVI');
open(Videofile) %This is the 1st step  to open the video file
writeVideo(Videofile,M)  %this command helps to write the frames(Which is stored in M) into this video file
close(Videofile) 

figure
plot(t,thetad_v(:,1))
hold on
plot(t,thetad_v(:,2))
ylabel('Angular disp and velocity')
xlabel('time')
    




                  FUNCTION CODE



function[dtheta_dt]= ode_fnc(t,theta,b,g,l,m)

theta1 = theta(1) % here we define theta 1 
theta2 = theta(2) % here we define theta 2
dtheta1_dt= theta2 % here we defined first ode
dtheta2_dt= -(b/m)*theta2-(g/l)*sin(theta1); % this is 2nd ode
dtheta_dt=[dtheta1_dt;dtheta2_dt]; % here we combined them into an array and semicolon means this is going to be coloum  matrix.
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.