Challenge

Week 4.1 - Solving second order ODEs

code for the function

clc

close all

clear all

b=0.05;

g=9.81;

l=1;

m=0.1;

%initial condition

theta_0=[0;3];

%time points

t_span=linspace(0,20,100);

[t,result]=ode45(@(t,theta) pendulum(t,theta,b,g,l,m),t_span,theta_0);

hold on

plot(t,result(:,1))

plot(t,result(:,2))

xlabel('plot')

ylabel('time')

ct=1;

for i=1:length(t)

    x=l*sin(result(i,1));

    y=-l*cos(result(i,1));   

    figure(2)

    plot([-1 1],[0 0],'linewidth',3,'color','r')

    hold on

    plot([0 x],[0 y],'linewidth',3,'color','g')

    plot(x,y,'o','markers',15,'markerfacecolor','blue')

    axis([-2 2 -2 .5])

    hold off

    pause(0.1)

    M(ct)=getframe(gcf);

    ct=ct+1;

end

movie(M);

videofile=VideoWriter('pendu.avi','uncompressed AVI');

open(videofile);

writeVideo(videofile,M);

close(videofile);

 

explanation for the code-

  • first define and give values for the variables -b,mass, lenght of string or swinging rod,gravitaional acceleration.
  • give the initial condition; the angular displacement is 0 and velocity is 3.
  • the represent array of time for which the pendulum swings.here the pendulum swings for 20 sec.
  • then initialize operation with in build operator ode45 where we have to first represent what are the functional variable in the equation.here that is time(t) and theta(angular displacement.the we represent the function.in this case the function us called and arguments are taken to the function named 'pendulum'.apart from that we gave time array and initial condition.since it is an inbuild function this is the format.
  • in 'pendulum' function;first represent theta1 as theta(1) and theta2 as theta(2).
  • then we give the  differential seperately for theta1 and theta2 according to eqation.then combine both of them together and denote as theta.
  • we get answer for theta in array of 2 column where first coloum represent displacement and other column velocity.  
  • then we plot the displacement and velocity in same graph wit respect to time.
  • so the next procedure is to create an animation video of the pendulum.so first create a support roof where the pendulum can be fixed.
  • then for each rows of displacement plot the x and y position in graph.and also place a circular mass at the end of the x and y coordinate.pause the graph for 0.1 second and save the frames in M array.
  • create a movie file named 'pendu' in incompressed format.first open the movie file,then write it and close them.

function code for the following-

function dtheta_dt=pendulum(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

result for the following-

the animation video attached in upload file inbox.

difficulties faced during challenge-

Since iam using a cracked version the result didn't obatined.so i used online matlab.

Also other difficultiy which i faced is to digest the ode45 function and to plot the animation video in graph.

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.