Challenge

Week 3 - Solving second order ODEs

% Simple pendulam 
clear all
close all
clc

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

% Initial condition
theta_0 = [0;3];

% Time points
t_span = linspace(0,10,250);

% solving ODE
[t,results] = ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);

% Plotting
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('Time')
ylabel('Theta')
legend('angular displacement','angular velocity')
hold off
ct=1;

for i = 1:length(results(:,1));
  x0 = 0;
  y0 = 0;
  x1 = 1*sin(results(i,1));
  y1 = -1*cos(results(i,1));
 

figure(2)
plot([1 -1],[0 0],'linewidth',4,'color','r')
hold on
plot([x0 x1],[y0 y1],'linewidth',3,'color','b')

plot(x1,y1,'o','markersize',35,'markerfacecolor','m')
axis([-2 2 -2 2])
pause(0.03)
hold off

M(ct) = getframe(gcf);
ct =ct+1;
end

movie(M)
videofile= VideoWriter('pendulam_ODE.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
  1. The inputs for the following code was (Damping(b)=0.05,Gravity(g)=9.81,Length(l)=1,Mass(m)=1)
  2. The initial condition was (theta_0=[0;3]),where the time points are (t_span = linspace(0,10,250)).
  3. By using this values we can solve the ODE by applying this formula : [t,results] = ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0)
  4. Where the (ode_func) is a function given below

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

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

  • By acquiring the values i have used the plot command to plot the graph to show the ('Angular displacement' and 'Angular velocity')
  1. I have named the graph figure(1) and used [plot(t,results(:,1)) and plot(t,results(:,2))] to plot the coordinates and used 'Hold on' and 'Hold off' commands. And i have represented 'xlabel' as "Time" and 'ylabel' as "theta", I have given the 'legend' command to represent the plots.
  2. I have used 'for' loop command to create an animation video of fig(2).Where the plots are[plot([1 -1],[0 0],'linewidth',4,'color','r') and plot(x1,y1,'o','markersize',35,'markerfacecolor','m')] and i have the given the "axis([-2 2 -2 2])" to show the axis and used pause,hold on and hold off commands.
  3. plot(x1,y1,'o','markersize',35,'markerfacecolor','m') was used to make the BOB size and colour.
  4. i have used the counter comand(ct) and GetFrame(gcf) command to get the frames and stitch them together.
  5. I have used this below code to save the video file.

movie(M)
videofile= VideoWriter('pendulam_ODE.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

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.