Challenge

Week 3 - Solving second order ODEs

%program on simple pendulum 2nd order differential equation

clear all

clc

%input
b=0.05;         %damping coefficient
g=9.81;         %in m/sec^2
l=1;                %in meters
m=0.1;             %MASS in kg

ct=1;
%initail conndition
tet_0=[0;3]; %tet(1) gives the initial displacement and tet(2) gives initial velocity 


%time points
time_span=linspace(0,20,150);

%to solve ODE we use function ode45 which is represented in the form
% [t,y] = ode45(@(t,y) 2*t, tspan, y0); where the formula y'=2t to be solved
% and the function defines y' as a function of y and t, with t span(initial
% and final of t) and initial condition of y.

%similarly for our condition with the calling function ODE_FUN

[t,reults]=ode45(@(t,tet) ode_fun(t,tet,b,g,l,m), time_span, tet_0); 
%this code gives two outputs in results - 1st colum is of displacements and
%2nd colum is of velocites. and t gives the time spam.

plot(t,reults(:,1))
hold on
plot(t,reults(:,2))
ylabel('plot');
xlabel('time');

%diplacement equation of a pendulum with damping is similar to the above
%equation.
 %program for a motion of a pendulum

x0=0;
    y0=0;
    m=reults(:,1); %as forst colum of results gives displacement we took it as x coordinate
for i=1:150
    
x1=m(i);
    y1=-1; %length of bar 1meter.
      figure(2)
    hold on 
    plot([x0 x1], [y0 y1],'Marker','o','Color',[rand rand rand])
    pause(0.1);
     M(ct)=getframe(gcf);
    ct=ct+1;
end

%for saving the output animations as a video
movie(M);
videofile=VideoWriter('ode_simple_pendulum.avi','Uncompressed AVI'); %video name is defined and also the type of video (uncompressed) is defined in
%videowriter command and saved in videofile function
open(videofile) %Open the file for writing
writeVideo(videofile,M) %Write the image data in M to the video file. Then, close the file
close(videofile)

as the description for codes were already mentioned in coding, report only consists of errors and output

youtube link: https://youtu.be/md04ffjdc7Q

%function ode45 to solve nonstiff differential equation
function [dteta_dt]=ode_fun(t,tet,b,g,l,m)

tet1 =tet(1);
tet2 =tet(2);

dteta1_dt=tet2;
dteta2_dt=(-b/m)*tet2-(g/l)*sin(tet1);

dteta_dt=[dteta1_dt;dteta2_dt];
end
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.