Challenge

Week 3 - Solving second order ODEs

Report

1. Defined the constants alond with time & ics array.

2. Plot Angular Displacement vs Time & Angular Velocity vs Time graphs.

3. Made multiple images for the pendulum at different instances using for loop.

4. created a video animation for it using videowriter.

5. Save animation in drive. Link: https://drive.google.com/file/d/1hGIcl9dJOOFs-7jOtO1BMx7dl6XwWqpm/view?usp=sharing

Errors & Rectification:

1. Mistake in writing hold off as hold of

ans: corrected it to hold off.

2. Forgot hold on after plotting x_0, x_1,y_0 & y_1.

ans: added hold on to rectify it.

clear all
close all
clc

%Pendulum Challenge
%Give values
L=1;                 %length in meters
m=1;                 %mass in kg
b=0.05;              % Damping coefficient
g=9.81;

t=linspace(0,20,500); %time array
ics=[0;3];            % angular velocity condition

%Calling the diff_eqn function to solve ODE
[t,theta]=ode45(@(t,theta)diff_eqn(t,theta,L,m,b,g),t,ics);

%Plot Angular Displacement vs Time & Angular Velocity vs Time graphs
plot(t,theta(:,1))
hold on
plot(t,theta(:,2))
ylabel('Plot')
xlabel('Time')

%Animation
writeObj = VideoWriter('Pendulum.avi');
open(writeObj);
figure()
count=1;

for i=1:length(t)
    x_0=0;
    y_0=0;
    x_1=x_0-sin(theta(i,1));
    y_1=y_0-cos(theta(i,1));
    plot([-2 2],[0,0],'linewidth',2.5,'color','k');
    hold on;
    grid on;
    plot([x_0 x_1],[y_0,y_1],'linewidth',2,'color','b');
    hold on;
    %Plot pendulum
    plot(x_1,y_1,'o','MarkerSize',20,'MarkerFaceColor','r','MarkerEdgecolor','r');

    xlim([-1.5 1.5]);
    ylim([-1.5 1.5]);
    hold off;
    %saveas(gcf,sprintf('figure_%d.jpg',count)); 
    F(count)=getframe(gcf);
    count=count+1;
end

for i=1:length(F)
    frame=F(i);
    writeVideo(writeObj, frame);
end
close(writeObj);


%Function

function [theta_dot]= diff_eqn(t,theta,b,g,l,m)

theta1= theta(1)
theta2= theta(2)
dtheta1_dt=theta2;
dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1);
theta_dot= [dtheta1_dt; dtheta2_dt];
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.