Challenge

Week 3 - Solving second order ODEs

This is the link for the video: https://youtu.be/8tOWd6zqcfw

This is the Matlab code for 2nd order ODE

clear all
close all
clc

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

% initial condition
theta_0=[0;3];

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

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

plot(t,results(:,1),'color','r')
hold on
plot(t,results(:,2),'color','b')

x0 = 0; y0 =0; ct = 1;
theta = results(:,1);
for i = 1:length(theta)
    x1 = l*sin(theta(i));
    y1 = -l*cos(theta(i));
    plot([-2 2],[0 0],'linewidth',3,'color','k') % pivot point/line/slab
    hold on
    plot(x1,y1,'o','markersize',20,'markerfacecolor','r') % pendulum bob
    plot([x0 x1],[y0 y1]) % pendulum string

    hold off
    axis([-2 2 -2 0.5])
    pause(0.03)
    M(ct)=getframe(gcf);
    ct=ct+1;
    ylabel('Plot')
    xlabel('time')
end

movie(M)
videofile = VideoWriter('second_order.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

for ode_func see below

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

This is the theriotecal portion of 2nd order ODE

`frac[d^2theta][dt^2]+b/m frac[d theta][dt]+g/L sin theta=0`

b=damping coeffient

m=mass

g=gravity

L=Length

We are solving for this `theta`and also the rate at which the `theta`value is changing with respect to time, which is nothing but the velocity.

The above equation is second order ODE

Say, `theta=theta1`

`frac[d theta][d t]=frac[d theta1][d t]=theta2---[Equation 1]`

`frac[d^2 theta][d t^2]=frac[d^2 theta1][d t^2]=frac[d][d t][frac[d theta1][d t]]=frac[d (theta2)][d t]----[Equation 2]`

If we go back to our original equation

`frac[d^2 theta][d t^2]+b/m frac[d theta][d t]+g/L sin theta=0`

`frac[d^2 theta1][d t^2]+b/m frac[d theta 1][d t]+g/L sin theta1`

`frac[d theta2][d t]+b/m theta2+g/L sin theta1=0`

In order to solve this original 2nd order ODE, that i have created two ODE's

`[frac[d theta1][d t]=theta2] [frac[d theta2][d t]=-frac[b theta2][m]-g/L sin theta1]`

`[[frac[d][d t][frac[theta1][theta 2]]]=[frac[theta2][-frac[b theta2][m]-g/L sin theta]]`

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.