close all
clear all
clc
%inputs
b=0.05
g=9.81
l=1
m=0.1
%intial conditions
theta_0=[0;5]
%time
tspan=linspace(0,20,150);
%solving ode
[t,results]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),tspan,theta_0);
figure(1)
plot(t,results(:,1));
hold on
plot(t,results(:,2));
legend('angular displacement','angular velocity');
% to animate the motin of simple pendulum
figure(2)
ct=1
for i=1:length(results(:,1))
x0=0;
y0=0;
plot([-1 1],[0 0],'linewidth',4,'color','k');
%position of the bob
x1= 1*sin(results(i,1));
y1= -1*cos(results(i,1));
hold on
plot([x0 x1],[y0 y1],'linewidth',3,'color','r');
%plotting of fixed end
plot(x0,y0,'-s','markeredgecolor','k','markerfacecolor','b','markersize',10);
%plotting of bob
plot(x1,y1,'-o','MarkerEdgeColor','k','markerfacecolor','c','markersize',20);
hold off
axis([-1.4 1.7 -1.5 0.5]);
grid on
M(ct)=getframe(gcf)
ct=ct+1
end
%code to animate the otion
movie(M)
videofile=VideoWriter('odesimplependulum.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
To solve the higher differential equations,it should be deformed into first order equations.Inorder to solve second order differential equations, ti should to transformed into two first order euations by assuming some assigned variables.
First we have taken inputs b,g,l,m values.Considered the intial displacement is zero and the intial velocity is 5.In plotting the figure 1, we have taken the ode functions of t, theta and all the ode func inputs.
In figure 2 we have plotted the movement of the pendulum.It was fixed at one end at(0,0).The other end is fixed with the bob.The end of bob and the length of the pendulum was calculated.All these were assigned with different colours and dimensions.
To caputure the all movements of the pendulum,we have created a videofile M.
https://matlab.mathworks.com/matlab/9.12.0.1896817/window.html?winId=db8e595c-c7bd-49d5-8599-be11572dcb20