Challenge

Week 3 - Solving second order ODEs

%inputs
b=0.05           %damping coefficient
g=9.81           %gravity
l=1;             %length of the pendulum
m=0.1;           %mass

%initial conditions
theta_0=[0;5];

%time points
t_span=linspace(0,10,50);
 
%solve ode
[t, results]= ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);


%plotting
figure(1)
hold on
plot(t,results(:,1),'color','b')
plot(t,results(:,2),'color','r')
xlabel('time')
ylabel('plot')


ct=1;

for i=1:length(t_span)
    
    
    x0=0;
    y0=0;
    
    x1=l*sin(results(i,1));
    y1=-l*cos(results(i,1));

    figure(2)
    plot([-2 2],[0 0],'linewidth',5,'color','r');
    hold on
    plot([x0 x1],[y0 y1],'linewidth',3);
    plot(x1,y1,'-o','markers',20,'markerfacecolor','g');
    grid on
    pause(0.03)
     M(ct)=getframe(gcf);
  hold off
  ct=ct+1;

       
end
movie(M)
videofile=VideoWriter('Second_order_ODE.avi','uncompressed avi')
open(videofile)
writeVideo(videofile,M)
close(videofile)



#ode_function
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/l)*sin(theta1);
dtheta_dt=[dtheta1_dt;dtheta2_dt];
end








For creating animation of motion of a pendulum we have to know the factors depending on it,the motion of the pendulum depend on Newtons second law of motion.And the motion of the pendulum can be represented by an ordinary differential equation.

this is an ode representing the motion of pendulum with damping,this is a second order ode

b-damping coefficient

g-gravity

l-length of the pendulum

m-mass

so we have to breakdown the the second order equation to 2 first order equations

we have to convert this as our solution vector,We have to define the function ode and theta and explain how the ode function is defined,we have the inputs damping coeeficient,gravity,length of pendulum,and mass of the bob,so for solving the ode the ode45 is used,ode45 is a function of t,theta and t.t and theta are the dependent and independent variables of function, then defining the function t,theta,b,g,l,m are the parameters that we are passing .so we should provide the initial condition,integration,time array then initial condition corresponding to the starting of the time.so we are declaring the time span in the range of 0 to 10 if 50different values ,

For the ode_func we have to define theta1 and theta2 ,theta is our solution array,we are combining these to an array called dtheta_dt and it will be a colomn matrix.And the ode_func will give the time ,integration,and result since we are solving 2 odes the results will be a colomn vector .

After this we have to plot the the graph ,the displacement starts at o value and the and the velocity is starting from a value of 5,and these two are damping out,we can change the effect of damping on these two variables by changing the damping coefficient.

For creating an animation we declared counter command and the movie command.

M is the variable which stores all the output figures and used in the loop for 50 intervals and we have variables x1 and y1 for represent the motion of the pendlulum we are plotting a graph with a marker with a face which represents the shape of the pendulum,for getting the frames that are plotting we are using the getframe command and stored in a variable called M.W are using counter command ct for iteration process,and for loop is used.by movie command we are saving these frames as a video file,writevideo command is used to save the video in the desired location.

I got an error because of putting the hold on command before plot command.after fixing this program has no error.

RESULTS-

A graph with velocity and displacement waves are plotted,we can see the effect of damping in both velocity and displacement.the displacement wave starts from 0 and velocity starts from 5 the effects can be varied by varing the damping coefficient.

the link of the animation video of pendulum is given below.

https://drive.google.com/file/d/14WvcByB81X6qFPc9EB-Y6voCcAX299UP/view?usp=sharing

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.