Challenge

Week 3 - Solving second order ODEs

% 2ND ORDER ODE
clear all
close all
clc

% Input the variables
b = 0.05;        
g = 9.81;         
l = 1;            
m = 1;            

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

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

% Plotting Function
figure(1)
plot(t,results(:,1))
hold on;
plot(t,results(:,2))
xlabel('Time');
ylabel('Plot');
plot([0,20],[0,0],'color','black');

%For the next Figure
ct = 1;
figure(2)
for i = 1:length(results(:,1))
    theta0 = results(i,1);
    x0=0;
    y0=0;
    x1=l*sin(theta0);
    y1=-l*cos(theta0);

% Motion Plotting
figure(2)
plot([-1,1],[0,0],'linewidth',4,'color','b');
line([x0,x1],[y0,y1],'linewidth',3)
hold on
plot(x1,y1,'o','markers',15,'markerfacecolor','b');
axis([-1.5 1.5 -1.5 1.5]);
hold off
pause(0.03)
M(ct)=getframe(gcf);
ct = ct + 1;
end

% Creating the Movie for the Plot
movie(M)
videofile=VideoWriter('Pendulum_simulation.avi','uncompressed AVI');
open(videofile)
writevideo(videofile,M) 
close(videofile)

% Sucessful Completed 

%Function for the 2ND ORDER ODE

function [dtheta_dt] = ode_function(t,theta,b,g,l,m) %initializing the function
    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.