Challenge

Week 3 - Solving second order ODEs

function [dtheta_dt] = ode_func(t,theta,b,g,l,m)
b = 0.05;
m = 1 ;
g = 9.81;
l = 1;


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
 
Expilination :
  
              This is the function for genrating the values for theta1 and theta2.
clear all
close all 
clc


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

% initial conditions
theta_0 = [0;3];

% time points
t_span = linspace(0,20,200);

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

subplot(2,1,1)
plot(t,results(:,1),'-b','linewidth',3);
xlabel('time t in sec');
ylabel('Angular displacement');
subplot(2,1,2)
hold on;
plot(t,results(:,2),'-k','linewidth',3);
xlabel('time t in sec');
ylabel('Angular velocity');
grid on;

% ANIMATION 
X0 = 0;
Y0 = 0;

ct = 1;
for i = 1 : length(t)
    X1 = l * sin(results(i,1));
    Y1 = -l * cos(results(i,1));

    figure(2)
    plot([-1 1],[0 0],'LineWidth',6,'Color','g');
    axis([-2 2 -2 2]);
    line([X0 X1],[Y0 Y1],'LineWidth',2,'Color','b');
    hold on;
    plot(X0, Y0,'O','markersize',3,'markerfacecolor','y');
    plot(X1, Y1,'O','markersize',20,'markerfacecolor','r');
    title('Damping of simple pendulum');
    legend('support rod','length of pendulum','Fixed point','Bod of pendulum');
    grid on;
    hold off;
    M(ct) = getframe(gcf);
    ct = ct+1;
end


movie(M);
videofile = videowriter('simple_pendulum','Uncompressed AVI');
open(videfile);
writeVideo(videofile,M);
close(videofile);


EXPLATION :
     
            For solving second order differancial equation(ODE) we require some inputs those are damping,mass,gravity,length.In (ODE) there are two constants they are t,theta.
 
           I got some inputs for the challenge those are angular displacement=0,angular velocity=3 rad/sec at time t=0.By using this values we will generate the plots and a movie of pendulum movement.

          This is the equation for solving the (ODE) [t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span, theta_0).

          We use subplot for creating two plots in one plot for 1(time,angular displacement) and 2(time,angular velocity).

          We created animation of moving pendulum in movie form.

youtube link: https://https://youtu.be/5puI-CkonIQ
          


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.