Challenge

Week 3 - Solving second order ODEs

aim:

To write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum withdamping

in the above equation

n 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.

to find the motion of the pendulam i refered my collage book

the given equation is in first order so we change that equation in to second order

theta1=theta(1)
theta2=theta(2)
dtheta1_dt=theta2;
dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1);
dtheta_dt=[dtheta1_dt;dtheta2_dt];
Then write the program and create a function for this program
problems faced during write the program
%write a program to solve the following ode
%d^2theta/dt^2+b/mdtheta/dt+g/l*sintheta=0

%in the above equation g=gravity,b=damping coefficient, m=mass of the
%ball,l=length of the pendulam
 


%lets creat a program to simulate the trasient behavior of a simple
%pendulam



%the given inputs are
l=1;
b=0.05;
g=9.81;
m=1;

%in intial condition 
theta_0=[0;3];
% time points
t_span=linspace(0,20,500);


%solve the ode 
[t,results]=ode45(@(t,theta)ode_function(t,theta,b,g,l,m),t_span,theta_0);
%plotting
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('time in sec')
ylabel('plot')

%to animate the pendulam with the help of for loop
figure(2)
ct=1;
theta_1=results(:,1);
for i=1:length(results(:,1))
    %initial coordinates of the pendulam
x_start=0;
y_start=0;

theta_2= theta_1(i);

%finel coordinates of the pendulam
    x_end= 1*sin(theta_2);
    y_end=-1*cos(theta_2);


    %to create the animation
    figure(2)
    %base of the pendulam
    plot([-1,1],[0,0],'linewidth',3,'color','r');
    hold on

    %string the pendulam
    line([x_start x_end],[y_start y_end])
    hold on
    %bob the pendulam
plot(x_end,y_end,'marker','o','MarkerSize',28,'MarkerFaceColor','auto');
axis([-2 2 -2 2])
pause(0.03)
hold off
M(ct)=getframe(gcf)
ct=ct+1
end
%create a movie for the animation
 movie(M)
    videofile= VideoWriter('simple_pendulam.avi','uncompressed AVI');

    open(videofile)
    writeVideo(videofile,M);
    close(videofile)

%program for ode function
function[dtheta_dt]=ode_function(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
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.