Challenge

Week 3 - Solving second order ODEs

clear all
close all
clc

%inputs
b=0.05;
g=9.81;
l=1;
m=1;

%initial condition

theta_0 = [0;3];

%time points

t_span = linspace(0,20,500);

%solve ODE

    [t,results] = ode45(@(t,theta) ode_func(t,theta,m,l,b,g),t_span,theta_0);
figure(1)
    plot(t,results(:,1))
    hold on
    plot(t,results(:,2))
    ylabel('plot')
    xlabel('time')

    x0 = 0;
    y0 = 0;

    ct = 1

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

       figure(2)
       plot([-1 1],[0 0],'LineWidth',6,'Color','r');
       hold on
       line([x0 x1],[y0 y1],'linewidth',2,'color','b');
       plot(x1,y1,'o','markersize',30,'MarkerFaceColor','r','MarkerEdgeColor','r');
       hold off
       axis([-2 2 -2 2]);
       M(ct)= getframe(gcf);
       ct = ct+1;
   end
   

movie(M)
videofile = VideoWriter('abc.avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Ode cycle

This program executes the 2nd order ODE of simple pendulum by using codes in MATLAB . 2nd order ode differential eqn of pendulum is considered , by solving the equation the rate in which pendulum moves with change in theta which determines the rate of change of velocity wrt to time.

To compute this initially break 2nd order ode into series of 1st order ode. By breaking down the equation in matrix form is obtained which represents the solution vector .

Let the inputs value be given to b(damping coff), g(acc due to gravity ), m(mass), l(length of pendulum)

Theta is the angle created due to pendulum movement.

Initial condition theta is given with respect to displacement and velocity .

Rate of change of velocity with respect to time is considered with using time array.

With linspace time array is given . linspace command gives required incremental values .inside the array end term defines the incremental value which divides equally the position of the pendulum for the change in angles of theta and the rate of change of velocity that varies with time span given in the time array.

Using in built ode45 command time , theta and ode function could solve the 2nd order eqn , which is a column matrix

To solve the this an equation is formed with, first use the syntax ode45 which is in built ode solver command in the matlab. Consider independent and dependent functions t(time) & theta. This says e ode is function of t & theta.

Next part, in the equation function is defined with input parameters . separate matlab program is written to form a function . inside function theta1 & theta2 is defined which represents the solution vector as per the derivation done for breaking down the 2nd order ode to the series of 1st order ode’s.

Then , initial condition, integration & how far the integration to be carried out is defined in the equation by with time array . then initial condition for displacement and initial condition of velocity is defined by including theta matrix in the equation with.

With this eqn (column vector) the displacement and the velocity of the pendulum is determined for the given time.

Now with this results graph can be plotted to see the variation in the displacement and velocity.

With the help of matlab commands we can create animation for pendulum movement.

The graphical representation can be viewed with animated movement .

Use for loop give the x1, y1 values inside the for loop which represents pendulum movement. as it need to be plotted hanging down use negative sign.

Figure 2 – create animated pendulum using plot commands . initially create horizontal line using linewidth .

Next pendulum arm is created using linewidth command and giving x0,x1 & y0,y1 positions.

X1 and y1 is plotted by using markersize, markerfacecolour and markeredge colour to create pendulum ball .

ct is inserted in input which represents the individual frames , ct+1 counter is given to display consecutive frames .

All this frames at be created as a movie/video . Individual Frames (ct) is assigned to M array by using command getframe(gfc).

After closing the for loop , animation of pendulum movement Video/movie is create by assigning the M array to the movie command , Videofile name is set with command video writer with avi format also can use uncompressed videos if you expect lower quality video or concerned about storage can remove uncompressed . To display frames write video command and give file name “video file” and M which contains the frames then close command is used to stop/close the video once all frames are displayed. Movie file is then created in the given location.

By varying input value of linspace the FPS (frames per second ) can be varied ..

error occured

graph orentation

error solved by changing to -ve sign

video link - https://drive.google.com/file/d/1qDsFTa1hxxiv9ptq0D0uH2ZCEDbiMEnu/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.