Challenge

Week 3 - Solving second order ODEs

AIM:- To solve a second order differential equation using MATLAB

INTRODUCTION:- Solving a second order differential equation by breaking down into a set of first order differential equation in MATLAB

EQUATION:-

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

Where,

g = gravity

L = length of the string in ‘m’

m = mass of the bob

b = damping coefficient

`theta` = angle made by the string

Assume

`theta = theta_1`

`(d theta)/dt = theta_2`

`(d^2 theta)/dt^2 =(d^2 theta_1)/dt^2 =d/dt ((d theta_1)/dt) =(d theta_2)/dt`

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

`(d theta_2)/dt + b/m*theta_2 + g/l *sin theta_1=0`

ODE FUNCTION CODE:-

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

MATLAB CODE FOR SOLVING ODE:-

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

%initial conditions
theta_0=[0;3];

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

%solve ode
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
figure(1)
plot(t,results(:,1));
hold on
plot(t,results(:,2));

%For creating animation of pendulum
theta=results(:,1);
ct=1;
for i=1:length(results)
    THETA=results(i,1);
    x=0
    y=0
    x0=l*sin(THETA);
    y0=-l*cos(THETA);
    
    figure(2)
    hold on
    plot([x,x0],[y,y0],'linewidth',2);
    plot(x0,y0,'-o','markersize',20);
    axis([-2 2 -2 2]);
    pause(0.005);
    
    M(ct)=getframe(gcf)
    ct=ct+1;
    clf();
end
movie(M);
videofile=VideoWriter('pendulumn_animation','Uncompressed avi');
open(videofile);
writeVideo(videofile,M);
close(videofile);

GRAPH OF ODE FUNCTION:-

ERRORS FACED:-

I didn't used the ')' symbol in 34th line

Used y=l*cos(theta) instead of y=-l*cos(theta)

CONCLUSION:- Here we have written a code to simulate the motion of pendulum using a second order differential equation using MATLAB

LINK FOR MOTION OF PENDULUM:-

https://youtu.be/03GBK0Gytpw

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.