Challenge

Week 3 - Solving second order ODEs

Aim:

To write a program to solve the given ODE in the challenge,and also to write a code for making a video of motion of simple pendulum and also to write a code to find the position of the pendulum.

Solution:

The given below code is a function which comprise of the above given equation and it is used in the

main codes for making the pendulum action.

function [dtheta_dt] = ode_f(t,theta,d,f,n,O)
         theta1 = theta(1)
         theta2 = theta(2)
         dtheta1_dt = theta2;
         dtheta2_dt = -(d/O)*theta2-(f/n)*sin(theta1);
         dtheta_dt = [dtheta1_dt; dtheta2_dt];
end

The given below code is coded for getting the graph ,pendulum action and position

clear all
close all
clc
%given inputs in challenge
d = 0.05;
f =9.81;
n = 1;
O = 1;

%initial condition of pendulum
theta_0 = [0;3];
t_span = linspace(0,20,600);

%condition
[t,result] =  ode45(@(t,theta) ode_f(t,theta,d,f,n,O),t_span,theta_0);

%plot of solution
figure(1)
plot(t,result(:,1),'color','g','linewidth',2)
hold on
plot(t,result(:,2),'color','b','linewidth',2)
xlabel 'position'
ylabel 'Angular Velocity'


%making pendulum video
theta_2 = result(:,1);
ct = 1;
for i = 1:length(theta_2)
    theta_3 = theta_2(i);
    
    x0 = 0;
    y0 = 0;
    
    x1 = O*sin(theta_3);
    y1 = -O*cos(theta_3);
   
    figure(2)
    plot([-1 1],[0 0],'color','g','linewidth',6);
    hold on 
    plot([x0 x1],[y0 y1],'color','m','linewidth',3)
    plot([0 0],[0.75 -1.5],'k--')
    hold on 
    plot(x0,y0,'o','markers',7,'markerfacecolor','k')
    plot(x1,y1,'o','markers',20,'markerfacecolor','c')
    hold off 
   
    axis([-1.5 1.5 -1.5 0.5])
    pause(0.3)
    s(ct) = getframe(gcf);
    ct = ct+1;
end
movie(s)
videofile = VideoWriter('pendulum_osc_1.avi','uncompressed avi');
open(videofile)
writeVideo(videofile,s)
close(videofile)

%position of pendulum
hold on
figure(3)
plot(result(:,1),result(:,2),'color','y','linewidth',2)
xlabel 'position'
ylabel 'Angular Velocity'

Step wise Explaination :

1. The initial conditions are given as

d = 0.05;
f =9.81;
n = 1;
O = 1;

2. position and time span are given as follow

theta_0 = [0,3]

t_span = linspace(0,20,600)

here when it reaching or the given interval is splite into 600 times than it will goes to make 20seconds video

3.condition for getting the plot

[t,result] = ode45(@(t,theta) ode_f(t,theta,d,f,n,O),t_span,theta_0)

ode45

[t,y] = ode45(odefun,tspan,y0), where tspan = [t0 tf], integrates the system of differential equations y′=f(t,y) from t0 to tf with initial conditions y0. Each row in the solution array y corresponds to a value returned in column vector t.

All MATLAB® ODE solvers can solve systems of equations of the form y′=f(t,y), or problems that involve a mass matrix, M(t,y)y′=f(t,y). The solvers all use similar syntaxes. The ode23s solver only can solve problems with a mass matrix if the mass matrix is constant. ode15s and ode23t can solve problems with a mass matrix that is singular, known as differential-algebraic equations (DAEs). Specify the mass matrix using the Mass option of odeset.

ode45 is a versatile ODE solver and is the first solver you should try for most problems. However, if the problem is stiff or requires high accuracy, then there are other ODE solvers that might be better suited to the problem. See Choose an ODE Solver for more information.[This is refered from MATLAB Documentation].

4.figure (1) shows that frequency and damping effect of the simple pendulum based on the output from the condtion or result from the solver, the output plot is shown below,

5.figure(2) give the pendulum motion and compiles a video for motion of pendulum and the final picture which is a stand by image after compiling the video using MATLAB using the 'videowrite' command and the video is uploaded in the youtube and a link is shown below for evaluation.

youtube link:https://youtu.be/T3NLDvgJU94

6.figure(3) gives the position of the pendulum based on the results obtained from the condition and the plot is shown below

Result:

Finally by using ODE45 command, plot Command and video command i have created pendulum motion and position and also the frequency and damping of the pendulum.

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.