Challenge

Week 3 - Solving second order ODEs

https://youtu.be/J5m6voyY8lQ

% Creating a function
function [dtheta_dt] = ode_func(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











% Solving ODE for Simple Pendulum
clear all
close all 
clc

b = 0.05; %Damping coefficient
g = 9.81; % Gravity(m/s^2)
l = 1; % Length(m)
m = 1; % Mass of ball (Kg)

% initial condition
theta_0 = [0;3]; %give initial condition for displacement and velocity
%Time Points
t_span = linspace(0,20,500); % give starting time, end time, no. of values

%solve ode
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0); % call function and solve ODE

%Plot a graph for ODE of simple pendulum
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('Time')
ylabel('Plot')
title('Simple Pendulum')
legend({'Displacement','Velocity'},'Location','northeast')
grid on


%Plot Simple Pendulum Animation
ct = 1;
for i = 1:length(results(:,1))
    THETA = results(i);
    x = 0;
    y = 0;
    x1 = x + l*sin(THETA); % Equation for find the coordinate of x1
    y1 = -(y + l*cos(THETA)); % Equation for find coordinate of x2

    figure(2)
    plot([x x1],[y y1],'-o','markersize',10,'MarkerEdgeColor','red','MarkerFaceColor','blue','LineWidth',3,'Color','g')
    axis([-1.5 1.5 -1.5 1])
    grid on
    title('Simple Pendulum') % Give title
    p = [0.4 0.5];
    w = [0.7 0.6];
    annotation('textarrow',p,w,'string','Pivot Point') % it give text arrow
    pause(0.1)
    M(ct) = getframe(gcf); % collect frame or image
    ct = ct+1;
end

movie(M) %create movie
videofile = VideoWriter('pendulum.aiv','Uncompressed AVI'); % create a file where movie is dump or store
open(videofile) % now open folder whose name is video file
writeVideo(videofile,M) % Now we create a video here 
close(videofile) % now we close that file
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.