Challenge

Week 3 - Solving second order ODEs

A program to solve the equation of 2nd order differential equation

The equation for damping is as follow :

where In the above equation,

g = gravity in m/s2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient.

and

L=1 meter,

m=1 kg,

b=0.05.

g=9.81 m/s2.

first writing a ode function to use ode45 inbuild fuction of matlab but as ode45 only solves the single order differential equation we have to convert the second order differential equation by considering dtheta_dt = theta2 therefore sustititing the values in equation we get seconder order differential equation

function is as follows :

function [dtheta_dt] = ode_function(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

after writing this ode function we use inbuild ode45 to create a plot of velocity and theta vs time by providing the set of time span and theta

After that to create the movie we consider x0,y0 as fixed points and find the x and y coordinate with the help of length and theta of the pendulum and using for loop we capture the all the plots as frame and creat movie

code :

close all
clear all
clc

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

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

%initial conditions
theta0 = [0;5];

%solve ode
[t,result] = ode45(@(t,theta) ode_function(theta,b,g,l,m),t_span, theta0);

figure(1)
plot(t,result(:,1))
hold on
plot(t,result(:,2))
xlabel('plot')
ylabel('time')

%creating movie
ct = 1;
result = result(:,1);
for i=1:length(result(:,1))
    theta = result(i);
    
    %fix position o the string of pendulum
    x0 = 0;
    y0 = 0;
    
    x_cord = 1*sin(theta);
    y_cord = -1*cos(theta);
    
    %ploting 
    figure(2)
    plot([-1,1],[0,0], 'linewidth',2,'color','black')  % base of pendulum
    line([x0 x_cord],[y0 y_cord],'linewidth',2,'color','b') % string of pendulum
    hold on 
    plot(x_cord,y_cord,'marker','o','markersize', 10,'markerfacecolor','green','markeredgecolor','green') % sphere of pendulum
    hold off
    axis([-1.2 1.2 -1.2 0.5])
    pause(0.01)
    M(ct) = getframe(gcf); %capturing frames
    ct=ct+1;
end
%generating video
movie(M)
videofile = VideoWriter('Moment_of_pendulum.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close

Solution video url : https://youtu.be/X-cnF8hjTnU

Error faced during challenge :

1.dot indexing is not supoorted

2.error in axis solved by puttung proper axis

3.challenge of creating proper plot solved by using a hold off

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.