Challenge

Week 3 - Solving second order ODEs

clear all 
close all
clc

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

%iintial conditions conditions
theta_0 =[0;3]
% time points
t_span =linspace(0,20,300)


%apply ode solver
[t, results] =ode45(@(t,theta) func_ode(t,theta,b,g,l,m),t_span,theta_0)

%plot
ct =1
for i =1:length(results(:,1))
    theta1 =results(i,1)
    
    x1 =l*sin(theta1);
    y1 =-l*cos(theta1);
    plot([-1,1],[0,0],'linewidth',3,'color','g')
    hold on
    plot([0 x1],[0 y1],'LineWidth',3,'Color','r')
    hold on
    plot(x1,y1,'o','color','b')
    axis([-2 2 -2 2])
    hold off
    %pause(0.003)
    
    %frames store in
   
    M(ct)=getframe(gcf)
    ct =ct+1
    
end

%creating a videofile m
%movie(M)
videofile =VideoWriter('ode solving.avi')
open(videofile)
writeVideo(videofile,M)
close(videofile)
 
close all

%plotting


plot(t,results(:,1))
hold on
plot(t,results(:,2))
axis([0 20 -5 5]);
xlabel('time')
ylabel('plot')
title('displacement vs velocity')
legend('displacement','velocity')

MAIN PROGRAM

YOUYUBE LINK FOR THE MOTION OF THE PENDULUM:https://studio.youtube.com/video/8COb_e9Loxk/edit

AIM :to evaluate the motion of the pendulum by solving ode using matlab

DESCRIPTION OF THE PROJECT :by usng the give 2nd order ode we will decsrimate into the combination of two 1st order ode's like which we did in the ode function program in this solution.

AIM :to evaluate the motion of the pendulum by solving ode using matlab

DESCRIPTION OF THE PROJECT :by usng the give 2nd order ode we will decsriminate into the combination of two 1st order ode's like which we did in the ode function program in this solution.here the second order ode is described the position of the 'bob'with respect to the time.

according to the given equation m is the mass of the bob and L is the length of the string where one end is fixed and another end is fixed with the bob.

according to the give equation where,

b =damping coefficient

theta =angular displacement,

g = gravitational force

t =time,now the given second order ode can be converted into two first order ode's which is solved in the the second program.which is solved by using ode45 solver.

program explained :

1) as usual add the commands clear all,close all,clc...

2)add inputs of the given function and add intial conditions.

3)find the values of angular displacement and angular velocity w.r.to the time by using ode solver(ode45) and adding constants in it.

4)by using for loop we have to make an animation for the pendulum.here we have to add the length of i for the 1st column in the results(angular displacement)

5)find the x1,y1 values.creat a fixed support for the pendulum by giving the x,y values in plotting

6)ct is the command which is used to add the frames one by one.

7)add hold on command and creat the length of the string and add hold on again plot the bob at the free end of the string.

8)by using getframe store all the frames in current frame.ct=ct+1 describes you add one by one to the current frame.

9)finally create the videofile of the pendulum's animation.

10)hera again we have to plot for the displacement and velocity w.r.t time bby adding axis values and labels of x&y.

ode45 solver :

function [dtheta_dt] =func_ode(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



doe function 
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.