Challenge

Week 3 - Solving second order ODEs

% program to solve 2nd order ODE.
clear all
close all
clc

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

% initial conditions.
theta_0 = [0;3];

% time points.
t_span = linspace(0,20,200);

% solve ODE.  call the function.
[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))
grid on

% Animating the transient motion of the simple pendulum.


ct = 1;
for i = 1:length(results(:,1))
    x0 = 0;
    y0 = 0;

    x1 = l*sin(results(i,1));
    y1 = -l*cos(results(i,l));

figure (2)
    plot([0 0],[-1.5 1.5])
    hold on
    plot([x0 x1],[y0 y1],'LineWidth',2,'Color','b')
    axis([-1.5 1.5 -1.5 1.5])
    plot(x1,y1,'o','markersize',20,'MarkerfaceColor','r')
   
    M(ct) = getframe(gcf)
    hold off
    ct = ct+1

end

% animation 
movie(M);
videofile = VideoWriter('pendulum.avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)



ODE function program.
% function program to solve 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


Report.
program explanation-
* program starts with clear all,close all,clc to clear the work space ,graphs and the 
  command window.

* I collect the inputs.

* t_span is a command used in the ode45 solver which specifies the time span of the 
  iterations that are going to solve the ordinary differential equations.

* 0de45 is a function in MATLAB that solves ordinary or partiar differential equations 
  using a numerical method. in this problem the highest order is 2. so, we have to take 
  tow constatnt values so that to equation chamges to simple differential equations.

* legend commant is used to lable the angular velocity and the displacement.

* to animate the motion of pendulum for loop is used.

* movie use to make film.

probles while programing-

   i have get the animation of pendulum in opposite direction , thent i contat to my technical faculty and ask them and i get where is problem in my program.


pendulum animation link 
https://youtu.be/WgzTqdagEzk
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.