Challenge

Week 3 - Solving second order ODEs

Link to the animation - https://www.youtube.com/watch?v=dBKp1lk1-wc

clear all - Clears workspace.
close all - Closes all the figures.
clc - Clear command window.

b=0.05; - Input variable for damping coefficient.
g=9.81; - Input variable for gravity in m/s^2.
l=1; - Input variable for length of the pendulum in m.

m=1;- Input variable for mass of the bob in kg.

%INITIAL CONDITION
theta=[0;3]; - Variable consisting the values of initial displacement and velocity

%TIME POINTS
t_span=linspace(0,20,200); - Getting time intervals from 0 to 20 sec.

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

User defined annonymous function to calculate angular displacement and velocity for respective time intervals which are stored in results. The function returns time which is stored in t.

%FOR LOOP VARIABLE
ang_disp = results(:,1); - Storing angular displacement in new varaible.
n=length(results); - Determing the for loop runs.
ct=1;- Creating a counter variable.

for i=1:n - For loop running n times.

THETA=ang_disp(i); - Creating local variable for getting angular displacement;

%FIXED END - Creating the fixed end.
x0=0;
y0=0;

%DETERMINING THE OTHER END
y1=-l*cos(THETA);
x1=l*sin(THETA);

%FIGURE OF THE PENDULUM
plot([x0 x1],[y0 y1],'linewidth',3,x1,y1,'o','MarkerSize',20,'MarkerFaceColor','red',x0,y0,'s','MarkerSize',5,'MarkerFaceColor','blue',y=0,y=1);
axis([-1 1,-1.5 0]);
pause(0.03);

Plotting the fixed end, length and the bob. Setting the axis limits.

%GENERATING ANIMATION FILE
printf('Creating GIF- process... %d/%dn',ct,length(ct));-To know the progress of the execution.
img=print('-RGBImage'); - To store the frames in variable img in rgb format.
imwrite(img,'simple_pendulum2.gif','DelayTime', .005, 'Compression','bzip','WriteMode','Append'); - Writing the animation file.
ct=ct+1; Updating the counter.

end

function [dtheta_dt]=ode_func(t,theta,b,g,l,m); Declaring function with the arguments

theta1=theta(1); Storing initial angular displacement in local variable.
theta2=theta(2); Storing velocity in another local variable.
dtheta1_dt=theta2;

Computing the differential equation.
dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1);
dtheta_dt=[dtheta1_dt;dtheta2_dt]; - Stores variables in two coloumns.

end

There were several syntax errors which were resolved by using matlab help resources.

Error Occured - Horizontal dimensions mismatch while storing the angular displacement into local variable theta.

Resolving Error - Created seperate variable for storing the angular displacement outside the for loop.

clear all
close all
clc

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

%INITIAL CONDITION
theta=[0;3];

%TIME POINTS
t_span=linspace(0,20,200);

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

%FOR LOOP VARIABLE
ang_disp = results(:,1);
n=length(results)
ct=1;

for i=1:n
  
  THETA=ang_disp(i);
  
  %FIXED END
  x0=0;
  y0=0;
  
  %DETERMINING THE OTHER END
  y1=-l*cos(THETA);
  x1=l*sin(THETA);

  %FIGURE OF THE PENDULUM
  plot([x0 x1],[y0 y1],'linewidth',3,x1,y1,'o','MarkerSize',20,'MarkerFaceColor','red',x0,y0,'s','MarkerSize',5,'MarkerFaceColor','blue',y=0,y=1);
  axis([-1 1,-1.5 0]);
  pause(0.03);
  
  %GENERATING ANIMATION FILE
  printf('Creating GIF- process... %d/%dn',ct,length(ct));
  img=print('-RGBImage');
  imwrite(img,'simple_pendulum2.gif','DelayTime', .005, 'Compression','bzip','WriteMode','Append');
  ct=ct+1;

  end


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
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.