Project

Solving the ODE using ode functions and animating the motions as per this ode

To write a program that could solve the second-order ode through 'ode' function. The equation which needs to be solved is

`(d^2theta)/(dt^2) + (b/m)(d(theta))/dt +(g/l)sintheta = 0`

where theta is angular displacement

b = damping
m = mass
g = gravity
l = length
It is given that, initial angular velocity is = 3 rad/s, at time t = 0 sec.

This equation can be solved bu Finite difference method or ODE function usage method. Here we are following ode to solve the function

Matlab function code:

function [p] = pendulum(t,x,b,m,l,g)
%x=theta%

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

p = [x(2) ; ((-b/m)*(x(2))- ((g/l)*(sin(x(1)))))];

Code to solve the given program

clear all
close all
clc
tspan = linspace(0,20,50)
yo = [0;3]
[t,p] = ode45(@pendulum,tspan,yo) 
%length%
l = 1
figure(1)
plot(t,p(:,1),'color','b')
hold on
plot(t,p(:,2),'color','r')
hold off
grid on

figure(2)
n=1
for i = 1:length(tspan)
    x0 = 0
    y0 = 0
    x1 = l*sin(p(i,1))
    y1 = -l*cos(p(i,1))
    plot([x0,x1],[y0,y1],[-0.5,0.5],[0,0],'linewidth',3);
    hold on
    plot(x1,y1,'o','MarkerSize',30,'MarkerFacecolor','r');
    axis([-1.5 1.5 -1.5 0.5])
    grid on
    pause(0.001)
    hold off
    m(n) = getframe(gcf)
    n = n+1;
end

movie(m)
videofile = VideoWriter('rat.avi','uncompressed AVI');

open(videofile)
writeVideo(videofile,m)
close(videofile)

    
    
    

  • Input the data of constants given in the function which are b.l.g.m
  • Create a function file where the function is an array of two differential functions angular velocity and angular acceleration
  • This created function could be used to solve the required problem
  • This function is ode45 which integrates the differential equation. here the differential equations are angular velocity and angular acceleration. Using ODE45 we get angular displacement and angular velocity
  • Create a new file; specify the time intervals which need to be simulated.
  • Plot the graphs for both angular displacement and angular velocity along time
  • Then our aim is to create an animation of 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.