Challenge

Week 3 - Solving second order ODEs

objective:To write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping

code:

close all
clear all
clc
%inputing required variables
b=0.05;
g=9.81;
l=1;
m=0.1;
%initial condition
theta_0=[0;5];
%timepoints
t_span=linspace(0,10,500);
% to solve ode eqation
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
%plotting output
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ang=results(:,1);
ct=1;
%for loop to get required animation
for i=1:length(ang)
    x=l*sin(ang(i));
    y=-l*cos(ang(i));
    x_int=0;
    y_int=0;
    figure(2)
    plot([-1 1] , [0 0], 'linewidth',2)
    hold on
    plot([x_int x],[y_int y] , 'linewidth',2)
    hold on
    plot(x,y,'.','markersize',40)
    axis([-2 2 -2 2])
    hold off
    title('damped pendulum oscillations');
    xlabel('displacement in x direction');
    ylabel('displacement in y direction');
    pause(0.03)
     M(ct)=getframe(gcf);
        ct=ct+1;
end
%creating animation file in to movie file
movie(M)
videofile=VideoWriter('odeq.avi','Uncompressed AVI');
open(videofile);
writeVideo(videofile,M);
close(videofile);
%function for solving ode
function [d_theta_dt]=ode_func(t,theta,b,g,l,m)
theta1=theta(1);
theta2=theta(2);
d_theta1_dt=theta2;
d_theta2_dt=-(b/m)*theta2-(g/l)*sin(theta1);
d_theta_dt=[d_theta1_dt ; d_theta2_dt];
end

codelogic:

first i input the required initial conditions and then by using linspace command i created timespan between 0to10secs with 500 intervalsto solve the paramter t and results by using ode therefore ode45 command is used.

ode syntax in code :

[t,x] = ode45(@fname,tspan, xinit, options)

therefore the function in a ode passes the variables in to the tspan therefore function is executed and xinit , options values declared intially)

after that i plotted the results.

To create animation in code:

for loop is used to create animatio in which in the range of varying angle correspoinding variables x and y coordinates are also changed and by using marker command i created the bob which is moving and then i also fixed the axis at which one end of pendulum is fixed and then i made a line horizantally through fixed end by plotting line with required width and then finally by using getframe command to store current figure and by using movie command i creadyed the animation

errorsfaced:

while inputing arguments i faced error due to not enough input arguments

after representing iinput arguments correctly i came throgh solution easily

2.error

i forgot to give neagative direction to ycoordinate therefore the diagram is reversed

so after giving proper direction to yccordinate i get correct picture

so after i rectified all errors i executed the output :

Animation output:

https://youtu.be/COBZJYTT4k8

final obsercvations:

since it is damped oscillation the amplitude is decreased at every one oscillation.

in daily life we see these damped oscillations due to external forces like friction , airdragforce etc

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.