Challenge

Week 4.1 - Solving second order ODEs

AIM :-Solving second order ODEs

Theory:- This ODE represents the equation of motion of a simple pendulum with damping

`(d^2(theta))/dt^2+(b/m)*(d(theta))/dt+g/lsin(theta)=0`

`theta1=(d(theta))/dt`

`(d(theta1))/dt=theta2`

`theta2=-(b/m*theta1+g/l*sin(theta))`

In the above equation,

g = gravity in m/s2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient.

CODE IN METLAB:

Here

L=1 metre,

m=1 kg,

b=0.05.

g=9.81 m/s2.

clear all
close all
clc
b=0.05;
g=9.81;
l=1;
m=1;
%inicial condition
theta_0=[0;3];
%time point
t_span=linspace(0,20,500);
%solve ODE
[t,results]=ode45(@(t,theta) ode_function(t,theta,b,g,l,m),t_span,theta_0);
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')
%inputs2
theta=results(:,1)
x0=0;
y0=0;
ct=1
for i=1:length(theta) 
            x1=l*sin(theta(i))
            y1=-l*cos(theta(i))
    figure(2)
    plot([-1 1],[0 0])
    hold on
    plot([x0 x1],[y0 y1],'marker','o','MarkerIndices',2,'MarkerFaceColor','red','MarkerSize',15)
    hold off
    axis([-1 1 -1 1])
    pause(0.003)
    M(ct)=getframe(gcf);
    ct=ct+1;
end
movie(M)
videofile=VideoWriter('challenge4_1.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
ode_function:
function [dtheta_dt]=ode_function(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

OUT PUT OF METLAB CODE:

Plot of in between time anglar displacement and angler velocity .

Stimulated vedio :

https://youtu.be/WcvuZ6O-eEc

CODE EXPLANATIONS:

clear all:It remove all the variables stored in the current workspace.

close all:It close all the fig window opened if any from previous execution .

clc: This clear all the text from the commond windows.

'[t,results]=ode45(@(t,theta) ode_function(t,theta,b,g,l,m),t_span,theta_0)' INTEGRATE THE ODE_FUNCTION .
'ode_function(t,theta,b,g,l,m)' To call the ode_function.
'plot(t,results(:,1))'Plot the curve of angular displacement.
'plot(t,results(:,2))' Plot the curve of angular velocity.
'ylabel,xlabel' Name the axis.
Run the for loop to plot the pendulam in a perticular angle .
'M(ct)=getframe(gcf)' It is use to capture the fig as a frame.
'gcf ' If refers to current fig handle in metlab .
'movie(m)' plays the movies freams in array .
'video writer' this command will creat a video file of spacifayed name .
'Open' Open the specifyed file .
'close' Write the creat specifed file .
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.