Week 3 - Solving second order ODEs
b=0.05
g=9.81
l=1
m=0.1
%intial condtn
theta_0=[0,5]
%time points
t_span=linspace(0,10,50)
%solve ODE
[t , results] = ode45(@(t, theta) ode_func(t,theta,b,g,l,m),t_span,theta_0)
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('time')
ylabel('angular-velocity_and_angular-displacement')
legend('angular displacement' , 'angular velocity')
t=1
for i = 1:length(results(:,1))
x0=0
y0=0
x1=l*sin(results(i,1));
y1=-l*cos(results(i,1));
%showing simple pendulum via plotting
figure (2);
plot([1 -1] ,[0 0],'color', 'r','linewidth',3)
hold on
plot([x0 x1],[y0 y1],'color','b','linewidth',5)
hold on
plot(x1,y1 ,'-o', 'markeredgecolor','b','markerfacecolor','r','markersize',30)
pause(0.01);
axis([-2 2 -2 0.25])
hold off
s(t)=getframe(gcf)
t=t+1
end
movie(s)
videofile=VideoWriter('simplependulum.avi','uncompressed AVI')
open(videofile)
writeVideo(videofile,s)
close(videofile)
https://youtu.be/LHgWcUILaE4 (youtube animation video)
coding explanation
1.Declaring the input values i.e., b,m,g,l
2. Initially providing t_span (integration limits) and initial values of displacement and velocity
3.Building the function of ode
4.Using Matlab solver ode45 syntax as shown below
[t,y]=ode45(odefun,tspan,y0)
5.In figure(1):plotting displacament and velocity with respect to time
6.In figure(2):plotting pendulum with marker 'o' using robort arm technique
7.next animation plot using same technique that done in robort arm i.e., with the help of movie,getframe(gcf) and VideoWriter
Errors noticed
1.wrong using of ode45 syntax(sol:used it properly by proper analysing the ode45 syntax)
2.invalid expressions in several areas like,
I used just sin(theta) instead of sin(theta1)
3.WriteVideo in correct form (spell mistake)