Challenge

Week 3 - Solving second order ODEs

Aim:-- To Write Matlab Programm tosolve ODE Function.

Objective:-- Objective is to solve given ordenery equation, understand movement of pendulum.

Theory:--

A pendulum is a body suspended from a fixed support so that it swings freely back and forth under the influence of gravity. 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. When released, the restoring force acting on the pendulum's mass causes it to oscillate about the equilibrium position, swinging it back and forth. The mathematics of pendulums are in general quite complicated. Simplifying assumptions can be made, which in the case of a simple pendulum allow the equations of motion to be solved analytically for small-angle oscillations.

The differential equation which represents the motion of a simple pendulum is,

`((d^2theta)/dt^2) + (b/m)((d theta)/(dt)) + (g/l)(sin(theta)) = 0`

In the above equation,

g = gravity in `m/s^2`,

L = length of the pendulum in `m`,

m = mass of the ball in `Kg`,

b=damping coefficient.

Procedure:---

Codes:--

%% Solving Second Orders ODEs using MATLAB
clear all
close all
clc
%% Input Variables
% Input variables given in data
l = 1; %Meter
m = 1; %Kg
b = 0.05;
g = 9.81; %m/s^2
%% Initial Conditions
theta_0 = [0; 3]; %At initial condition ang. displacemenmt is 0 and anguler velocity is 3 red/sec
%% Time Points 
t_span = linspace(0,20,1000);
%% Solve ODE
[t, results] = ode45(@(t,theta) ode_func(t, theta, b, g, l, m), t_span, theta_0) % Here used the ode45 matlab inbuild function for the solving of ODE and created ode_func function for the our equation
%% Plotting
figure(1)
plot(t, results(:,1))
hold on
plot(t, results(:,2))
ylabel('Plot')
xlabel('time')
legend('Displacement','Anguler Velocity')
%% Crating Motion for pendulum for 0-20 Sec
grid on
ct = 1 ;
theta_1 = results(:,1);
for i = 1:length(theta_1)
    % Initial Coordinates
    x0 = 0;
    y0 = 0;
    theta_2 = theta_1(i);
    
    % Final Coordinates
    x_end = -l*sin(theta_2);
    y_end = -l*cos(theta_2);
    
    % Animation
    figure(2)
    
    plot([-1,1],[0,0],'linewidth',20,'color','g')
    hold on
    plot([x0 x_end],[y0 y_end])
   
    plot(x_end,y_end,'o','markers',20,'markerFaceColor','m')   
    axis([-2 2 -2 2])
    grid on
    hold off
    pause(0.03)
    M(ct) = getframe(gcf)
    ct = ct + 1;
end
%% Movie Generation codes
movie(M)
videofile = VideoWriter('Penndulum_movement.avi','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)

Programme was started as use with basic command lines. then after variables l, m, g, b is assigned with given values. Then after with colom array given variable displacements and velocity for the initial condition shown. time is shown by variable t_span.

Then created ODE for the our given equation. Used function command for it. Codes are below for it:-

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

Then used ode45 solver of matlab to solde the ODE. and after it plotted valuse of displacement and velovcity for the given time.

To crate motion we have to use for loop command as we used for the robotic arm previously. And captured frame and made video of it.

Output:--

Above graph is for Displacement and Velocity for the pendulum as per our equation and conditions.

Animation:--

Here is the animation for the pendulum oscillation:-

https://www.youtube.com/watch?v=5TUAFeQWdCs

Problem Faced:--

Problem was faced that so much lines are showing in animattion.

Image:-

Solution:--

Used hold on Command in for loop to solve this error

Solution photo:-

Conclusion:-

We get motion of pendulum by solving ODE.

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.