Challenge

Week 3 - Solving second order ODEs

Link for animation : https://drive.google.com/file/d/1zfkrCxXKWUCOpbSJOPYKB_ljBYX3k_g7/view?usp=sharing

1. Codes for animating the pendulum motion and plotting t,theta;

clear all
close all
clc

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

theta_0 = [0;3];
t_span = linspace(0,20,500);

%ode45
[t,theta] = ode45(@(t,theta)ode_function(theta,b,m,g,l),t_span,theta_0);

title('plot using ode45 commend')
hold on
figure(1)
plot(t,theta(:,1),'linewidth',2,'color','r')
plot(t,theta(:,2),'linewidth',2,'color','b')
xlabel('time')
ylabel('theta')
  
count = 1;
for i= 1:length(theta)
    Theta = theta(i);
    x =0;
    y = 0;
    x1 = l*sind(Theta);
    y1 = l*cosd(Theta);
    figure(2)
    plot ([x,x1],[y,y1],'color','r');
    axis([-0.02 0.02 -1.1 0.1]);
    pause(0.03)
    N(count) = getframe(gcf);
    count = count+1;
end

movie(N)
videofile = VideoWriter('pendulum');
open(videofile)
writeVideo(videofile,N)
close(videofile)

2.Codes for the ODE function

function [dtheta_dt] = ode_function(theta,b,m,g,l)

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

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.