Challenge

Week 3 - Solving second order ODEs

SOLVING ODE USING MATLAB

clear all
close all
clc


%damping coefficient
b=0.05;

%accerlation due to gravity
g=9.81;

%pendulum length
l=1;

%bob mass
m=0.1;

%input for pendulum
theta_0 = [0;3];

%time variation in the motion of pendulum
t_span = linspace(0,20,500);

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

%plotting

plot(t,results(:,1))
hold on
plot(t,results(:,2))


ode_func 

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


PENDULUM PLOTTING

clear all
close all
clc

%damping cofficient
b=0.05;

%accerlation due to gravity
g=9.81;

%pendulum length
l=1;

%bob mass
m=0.1;

%input for pendulum
theta_0 = [0;3];

%time variation in the motion of pendulum
t_span = linspace(0,20,500);

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

%plotting
%subplotting divides panel
%subplot 1
subplot(5,4,[13,14,17,18]);
plot(t,results(:,2),'color','r');
hold on
plot(t,results(:,2),'color','r');
hold off

%subplot 2
subplot(5,4,[15,16,19,20]);
plot(results(:,1),results(:,2));

%labelling
xlabel('Postion Radians')
ylabel('Angular velocity')

%inititaing the counter value
ct=1

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

%plotting
line([x0 x1],[y0 y1],'linewidth',3,'color','g');
hold on

plot(x1,y1,'marker','o','markersize',20,'markerfacecolor','b');
grid on

axis([-1.5 1.5 -1.5 0.5]);
pause(0.01);
hold off;

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

end

movie(M)
 videofile = VideoWriter('solveodeusingmatlabavi','Uncompressed AVI');
 open(videofile)
 writeVideo(videofile,M)
 close(videofile)

###youtube link -  https://youtu.be/fSEnScYOQ30


ERRORS FACED:

1. syntax was a bit tricky to catch up with.
2. sub plotting was new to me.
3. To plot a bob I was using the wrong syntax.


SOLUTION :

1. Watched the video twice and get help from Ajay Sir and Raj Kumar sir.
2. Watched MatLab tutorial in the MathWorks.
3. Technical support team help me to plot a Bob using "Marker"


EXPLANATIONS:

1. Code Logic:

  First of all, I break the 2nd order ODE into simpler ODE, then I use the function "ode_func" in which we assign the variable (theta1, theta2) and make the formulae of dtheta1_dt, dtheta2_dt, and dtheta_dt in an array.

  Now, in pendulum plotting, we assign the variable (b,g,l,m) along with initial theta_0 and t_span. Then wrote the syntax of the ode45.

  In plotting, we divide the plots into subplots and label them, the counter value was given to initialize and the for loop was used to generate the multiple results. And again the plotting was done along with the labeling and the for loop was closed.

 At last, the plot was converted into a movie using the code and converted into AVI format.

2. Syntex of ODE45:

   ode 45 is an inbuild solver in Matlab, the main inputs are (t, theta), theta and t are independent and dependent variables of the ode system.
     So our ODE is the function of t, theta and then we are defining ode_func().
   So ode_func is t, theta followed by b,g,l,m which are the input parameters we are passing.
   After that, we would be providing the initial condition in a time array and integration and initial conditions.

3. Loops :

   Only one For Loop is used, where 'i' was variable and assigned to 1 to the length, where length forms an array with 1 step. Then the values of x0 and yo were assign along with the x1 and y1 using formulae.

  



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.