Challenge

Week 3 - Solving second order ODEs

% 2nd order differncial equation

clear all
close all
clc

b=0.05;    here b= damping coefficient
g=9.81;         g= acceleration due gravity
m=0.1;          m= mass of bob
l=1;            l=length of string

% initial condition
theta_0=[0;3];

% time point
t_span=linspace(0,20,500);

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

plot(t,results(:,1))     here ode45 is inbuild command for solve order differntial 
                                   equation

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

ylabel('angular velocity')
xlabel('time')
hold off


figure(2)
plot(results(:,1),results(:,2))
xlabel('position')
ylabel('angular velocity')

theta2=results(:,1)
ct=1;
for i=1:length(theta2);
    theta3=theta2(i);

    x0=0;
    y0=0; 
    x1=l*sin(theta3);
    y1=-l*cos(theta3);
    
    figure(3)
    plot([-1.1 1.1],[0 0])
    hold on
    plot([x0 x1],[y0 y1],'color','r')
    hold on
    plot(x1,y1,'o','markers',20,'markerfacecolor','b')
    hold off
    grid on
    axis([-1.5 1.5 -1.5 1.5])
    pause(0.003)
    M(ct)=getframe(gcf);
    ct=ct+1;
end

movie(M)
videofile=videoWriter('pendulum.avi','uncompressed avi');
open(videofile)
writevideo(videofile,M)
close(videofile)



here we have explain ode function

function[dtheta_dt]=ode_func(t,theta,b,g,l,m)  
theta1=theta(1)               here we convert 2nd order to 1st order equation which is 
                                     easy for expalin in MATLAB
theta2=theta(2)
dtheta1_dt=theta2;
dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1)
dtheta_dt=[dtheta1_dt;dtheta2_dt];
end

https://youtu.be/gOh-emPghs8
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.