Challenge

Week 3 - Solving second order ODEs

Introduction :-

A pendulum is a weight suspended from a pivot so that it can swing freely. When a pendulum is displaced sideways from its resting, equilibrium position, it is subject to a restoring force due to gravity that will accelerate it back toward the equilibrium position.

A simple pendulum consists of a mass m hanging from a string of length L & fixed at a pivot point P. When displaced to an initial angle & released, the pendulum will swing back & forth with periodic motion. by applying Newton's second law for rotational systems, the equation of motion for the pendulum may be obtained

r = I*alpha = - mgsin(theta)L = mL^2[d^2(theta)/dt^2]

Code for pendulum motion :-

clear all
close all
clc

b = 0.05; % damping coefficinet
g = 9.81; % acceleration due to gravity - m/s^2
l = 1;    % length of pendulum - m
m = 1;    % mass of the ball - kg

% initial conditions

theta_0 = [0;5];

% time plot

t_span = linspace(0,20,500);

% solve for ode

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

% pendulum

count = 1;
for i = 1 : length(results(:,1));
    x0 = 0;
    y0 = 0;
    x1 = 1*sin(results(i,1));
    y1 = -1*cos(results(i,1));
    plot([-0.5,0.5],[0,0],'linewidth',3);
    line([x0,x1],[y0,y1],'linewidth',3)
    hold on
    plot(x1,y1,'o','markers',15,'markerfacecolor','r');
    grid on
    axis([-1.5 1.5 -1.5 0.5]);
    pause(0.02);
    M(count) = getframe(gcf);
    hold off
    count = count + 1;
end
movie(M)
videofile = VideoWriter('pendulum.avi','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)

Function Calling :-

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

Explanation :-

The constants of differential equation are defined as given in the problem statement.

The initial conditions are assigned to the variable .

The differential equation is solved by using the formula and matlab code .

At the time function call is made to the function ode_fun which will take the values of defined variables.

the for loop is used to calculate the motion of pendulum at different time intervals & positions.

marker command is used to create the ball.

getframe(gcf) is used to get all the images at every single point & assign it to variable M.

Movie is created of extension'.avi' using movie command.

Errors :-

Basic errors like using colon instead of semi colon and vice versa, few places using lowercase letters etc were made & were corrected by reading & executing code again & again.

The link for the video is :-

https://drive.google.com/file/d/15yJ--gkD90uq0E6KzbzyTEQ0lDjH0Y-k/view?usp=sharing

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.