Challenge

Week 4.1 - Solving second order ODEs

% function for ODE

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 


% Main script for ODE

clear 
close 
clc

% Solving the Second order ODE and show the animation

b = 0.05;                     % damping coefficient
l = 1;                        % length of pendulum in metre
m = 1;                        % mass of bob in kg
g = 9.81;                     % gravity in m/s^2

% 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,b,g,l,m),t_span,theta_0);

figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('Time')
ylabel('Velocity')
legend('Displacement','Angular velocity')
grid on
hold off

displacement = results(:,1);
velocity=results(:,2);

%fixed coordinates
x0=1;
y0=1;
ct=1;

for i = 1:length(displacement)
    
    %coordinates
    x1=1-l*sin(displacement(i));
    y1=1-l*cos(displacement(i));
    
    figure(2)
     %plot fixed link
     plot([0 2],[1 1],'linewidth',15,'color','b')
     hold on                              
     plot([1 x1],[1 y1],[x1 x1],[y1 y1],'-O','markersize',25,'markerfacecolor','r','linewidth',4)
     plot(x0,y0,'-O','markersize',3,'markerfacecolor','b')
     %axis setting
     axis([0 2 -0.5 2])
     
     M(ct)= getframe(gcf);                 % to change the frame of the movie
     ct=ct+1;
     hold off
end
movie(M)                                                                %for creating animation video
videofile=VideoWriter('simple pendulum.avi','uncompressed AVI');
open(videofile)                                                         %open the videofile while running the code
writeVideo(videofile,M)                                                 %for writing the video file
close(videofile)                                                        %for closing the videofile after code run




Code Explanation - 
1)Firstly, the given details are assigned as variables in the code. 
2)Then time span of the simple pendulum is set by using linspace command then, theta value is given as a column matrix. After this we use ode45 function and corresponding 'ode_func' function call in main program.
3)Then use plot command to plot graph corresponding to time and velocity,legend command is used for identify each values in the graph.
4)For getting the accurate simulation of the simple pendulum corresponding to the second order ODE with damping we need to use the looping statements, here for loop is used to create this.
5)The pendulum has fixed coordinates mentioned as x0,y0 and x1, y1 are the other coordinates.
6) Marker command is used for making bob of the pendulum. The axis is fixed using axis command. The counter command is used to make the value changes in each for loop. Then videoWriter command is used to create the video file. 
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.