Challenge

Week 3 - Solving second order ODEs

clear all
close all
clc
%initial inputs 
b=0.05;% damping coefficient
g=9.81;% gravitational constant
l=1;% length of rope
m=1;% mass of ball
theta_0=[-60,45];% angle of motion 

t_span=linspace(0,300,500);% time span of 300 secs and 500 intervals
%ode45 is a pre defined matlab function which solve the problem in most
%accurate way
[t,results]= ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
a=1;
% a is defined to give the increment for the for loop
% for loop is taken to plot each position of the pendulam
for i= 1:length(results( :,2))
    % x,y gives the cordinate position for the ball of mass m
    x=-l*sind(results(a,2));
    y=-l*cosd(results(a,2));
    x1=0;
    y1=0;
    % x2,x3 are taken to design the wall on which the ball will be hanging
    x2=-2;
    x3=2;
    % y2,y3 are taken draw a vertical straight line at 90 degrees to
    % understand the motion of the pendulum
    y2=0.2;
    y3=-1.2;
    % commands below are used to plot the graphs for animation of the
    % motion of the pendulam
    title('motion of the pendulam')
    plot([x1 x], [y1 y],Color='b')
    hold on
    plot(x,y,'o',Color='r')
    plot([x2 x3],[y1 y1],LineWidth=5,Color='g')
    plot([x1 x1],[y2 y3],':',Color=[0 0 0])
    pause(0.05)
    axis([-2.5,2.5,-1.25,.25])
    M(a)=getframe(gcf);
    a=a+1;
end
% commands below are used to generate animation
movie(M)
videofile=VideoWriter('ode.avi');
open(videofile)
writeVideo(videofile,b)
close(videofile)
% this set of program is to create the plot between displacement and
% velocity
close all 
figure(2)
plot(t,results(:,1))
hold on 
plot(t,results(:,2))
axis([0,130,-100,130])
title("displacement vs angular velocity")
xlabel('time')
ylabel('plot')
%legend is the predefined command to give data into text box of the plot 
legend('displacement','angular velocity')
% user defined function ode_func to solve the second order difrential equation and where difrentiation is done in which the values are passed from one to another.
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/1)*sind(theta1);
dtheta_dt=[dtheta1_dt; dtheta2_dt];
end

the google link of animation created is "https://drive.google.com/file/d/17b0t7nKDQOPELo4cQ0hCMRUieV-P9H-N/view?usp=sharing"

in the programs first the initial inputs are given which are mass, length , damping co efficient, angle theta_0 and time span the a variable a is taken to do the itiration in the for loop

1) for loop is used in this program to create the figures of the motion of the pendulum

2)x,y is the co ordinates of string attached to the ball i,e it represents the length of string

3) x1,y1 are 0,0 i.e., origin

4) x2,x3,y2,y4 are the lines and points used to beautify the annimation i.e.,

i) plot is given between x2 x3, y1 y1 it gives a straigth line on x axis which is shown as the wall on which weight is hanging.

ii) when the plot is given of " : " on x1 x1 y2 y3 a straigth line with : is ploted on the y axis by which we can see the movement of the ball clearly

5) ode45 is a predefined function in the matlab which is a solver which gets the results on perticular time t from the user defined function ode_func

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.