Project

solving second order ode

clear all
close all
clc
L=1;                        %Length of string
f=1;                        %frame number
m=1;                        %Mass of the ball
b=0.05;                     %Damping coefficient
g=9.81;                     %Acceleration due to gravity
x_0=[0;3];                  %Initial Conditions
t_l=0:0.05:20;              %Time limits

[t, r]=ode45(@(t,x) ode(t,x,b,L,g,m),t_l,x_0);
%Graph of Angular Displacement(rad) and Velocity(rad/s) VS Time(s)
plot(t_l,r(:,1),t_l,r(:,2));
xlabel(\\\'Time(s)\\\')
ylabel(\\\'Angular Displacement(rad) and Velocity(rad/s)\\\')
legend(\\\'Angular Displacement(rad)\\\',\\\'Velocity(rad/s)\\\')
for i=1:length(t_l)
    x0=0;                   %x coordinate of fixed end of string
    y0=0;                   %y coordinate of fixed end of string
    x1=L*sin(r(i,1));       %x coordinate of free end of string
    y1=-L*cos(r(i,1));      %y coordinate of free end of string
    %line joining the two ends
    plot(x1,y1,\\\'-.or\\\',[x0,x1],[y0,y1],\\\'linewidth\\\',2)
    axis([-1 1 -2 0])
    pause(0.01)
    M(f)=getframe(gcf);     %Frame Capture
    f=f+1;                  %Increment in frame number
end
movie(M)
videofile=VideoWriter(\\\'Pendulum.mp4\\\');
open(videofile)
writeVideo(videofile,M)
close(videofile)


#Definition of ode function

function [dxdt]= ode(t,x,b,L,g,m)
    dx1dt=x(2);
    dx2dt=-((b/m)*(x(2)))-((g/L)*sin(x(1)));
    dxdt=[dx1dt; dx2dt];
end
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.