Challenge

Week 3 - Solving second order ODEs

https://youtu.be/ZaXc5HcfJCY

% ODE Function 

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

     theta1 = theta(1)
     theta2 = theta(2)
     dtheta1_dt = theta2;
     dtheta2_dt = -(b/m)*theta2 - (g/l)*sin(theta1);
     dtheta_dt = [dtheta1_dt; dtheta2_dt];
end

% Explanation 

% Second Order ODe is simplified to the first - order ODE by introducing
% the term Theta1 & Theta 2 
% Theta1 & Theta 2  is differential and the dtheta_dt gives a columan
% matrix 


% Objective to write a program that solves the ODE whihc represents the
% equation of the motion of a simple pendulum with damping 
% from the Equation d2Theta/dt2 + b/m dTheta/dt + g/l Sin Theta = 0 

% g = gravity in m/s^2
% L = length of the pendulum in m
% m = mass of the ball in kg 
% b = damping coefficient

clear all 
close all
clc


% inputs for the calculation 

l = 1;
m = 0.1;
b = 0.05;
g = 9.81;

% Initial Conditions 
theta_0 = [0;3];

% Time Intervels 
tspan = linspace(0,20,200);

% Solving ODE with Matlab ode45 Function 
[t,results] = ode45(@(t,theta) ode_fun(t,theta,b,g,l,m),tspan,theta_0);

% plot 
figure(1)
plot(t,results(:,1))
hold on 
plot(t,results(:,2))
xlabel('Time')
ylabel('Velocity & Displacement')

% Creating Animation 

ct = 1;
for i = 1:length(results(:,1))
    x0 = 0;
    y0 = 0 ;
    x1 = l*sin(results(i,1)+pi);
    y1 = l*cos(results(i,1)+pi);
    figure(2)

    plot([x0 x1],[y0,y1],'-o','MarkerSize',10,'MarkerFaceColor','r','MarkerEdgeColor','r','linewidth',4)
    axis([-2 2 -2 2])
    hold on 
    plot([-1 1],[0,0],'LineWidth',5,'Color','[0 0.3 0]')
    M(ct)=getframe(gcf)
    hold off
    ct = ct + 1;
end

% Creating Movie & Video file 

movie(M)
v_f = VideoWriter('ODE_Pendulum.avi','Uncompressed AVI')
open(v_f)
writeVideo(v_f,M)
close(v_f)

% Explanation 

% Fixed Parameters are given a Length, mass, Damping coefficient &
% acceleration due to gravity 
% time span considered 0 to 20 with 200 equal intervels by using Linspace 
% ODE45 matlab command used for ODE Calculation 
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.