Challenge

Week 3 - Solving second order ODEs

step wise explanation of te code

1.All the inputs as given are entered in the inputs section like mass,gravity,damping coefficient,length

2.then first order ode thetha_0 is given with angular displacement of 0and and velocity 3 rad/sec

3.then linspace command is used in time_span where the time is fixed between 0 and 20 with 500 values between them

4.A ode_function is created with the given formula and called into the original function and [t,results] values are stored which is evaluated with built in ode 45 function

5. the above inputs are used to plot the t,results values in a graph and hold on command is used to generate a second plot in the same figure window the plot shows the displacement and velocity

6.the loop is developed and then initial position and final position of the pendulum is defined using the formula

7.the base line, the pendulum line and the pendulum ball is created with the help of hold on command and ball is created with the help of marker command with specified axis and interval with grid on

8.and finally te cmmands for creating a avi fomat is used and stored

Errors faced while programming

1.spelling mistake in thetha_0 and the thetha used in ode45 command

2.using asteris command instead of o command during plotting the pendulum ball

3.only pendulum ball visible and the othet two plots not visible

4.axis defined wrong

Steps taken to overcome these errors

1.corrected the thetha spelling in the first case

2.used 'o' command and adjusted marker size

3.used hold on command to insert all three plots in the same figure window

4.adjusted axis untill smooth animation was available

5.reduced intervals between 0 to 20 from 500 to 100 to decrease the file size while uploading to youtube

youtube link for solving second order ode's(pendulum)

https://youtu.be/ZqvSXP3HL8A

function[dthetha_dt]=ode_func(t,thetha,b,g,l,m)

thetha1=thetha(1);
thetha2=thetha(2);
dthetha1_dt=thetha2;
dthetha2_dt=-(b/m)*thetha2 - (g/1)*sin(thetha1);
dthetha_dt=[dthetha1_dt;dthetha2_dt];
end




clear all
close all
clc

%inputs( L=length of pendulum in metre,m=mass of the ball in kg,b=damping coeff,g=gravity in m/s2)
l=1;
m=1;  
b=0.05;  
g=9.81;  

%initial conditions(angular displacement=0,angular velocity=3 rad/sec)
thetha_0=[0;3];

%time plots
t_span=linspace(0,20,100);

%solve ode
[t,results] = ode45(@(t,theta) ode_func(t, theta, b,g,l,m),t_span, thetha_0);

%plotting second order ode
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('time')
ylabel('plot')

pend=1

for i = 1:length(t)
    
    %initial position of the pendulum
    
    x0 = 0;
    y0 = 0;
    
    %final position
    
    x1 = l*sin(results(i,1));
    y1 = -1*cos(results(i,1));
    
    %plotting
    figure(2)
    %Base line
    plot([-1 1],[0 0],'linewidth',3,'color','b');
    %pendulum line
    line([x0 x1],[y0 y1],'linewidth',3,'color','r');
    hold on
    %pendulum ball
    plot(x1,y1,'o','markers',20,'markerfacecolor','y');
   
    grid on
    axis([-1.5 1.5 -2 0.5]);
    pause(0.05);
    hold off
    M(pend)=getframe(gcf);
    pend=pend+1;
end

%for video file in avi format
movie(M)
videofile=VideoWriter('ode_second_order','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

    
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.