Week 3 - Solving second order ODEs
Step wise Expalntion of the code
- strarted by clc,close all,clear all command
- Givien inputs required i.e gravity ,mass,length,and acceration
- Provide time intervals for the pedulum
- wrote the function programmand second order code
- plot the tme and displacement
- for pendulam operation used the for loop command to repeat the motion of pendulum
- plot the pendulum motion
- animated the viedofile .
code for pendulum motion
% close all
% clear all
% clc
% input
damp = 0.05;
acc = 9.81;
leng = 1;
mass = 1;
% input condition
theta0 = [0:3];
% time of the points
tspan = linspace(0,20,500);
% sloveing the second order OD
[t,reults] = ode45(@(t,theta) odefunc(t,theta,damp,acc,mass,leng),tsapn,theta0);
% plotting
figure (1)
plot ([t t ],[reults(:,1) reults(;,2)])
xlabel('time')
ylabel('displacement')
legend('angular displacement','angular velocity')
hold off
% for animation loop
for j = 1:length(t)
x=0
y=0
angle = reults (j,1);
x1 = -leng * cos(angle);
y1 = leng * sin(angle)
figure (2)
plot(x1,y1,'marker','o' ,'markerfacecolor','r,'markersize',30)
hold on
plot ([-.5 5],[0 0],'linewidth',5,'color','c')
plot([x x1],[y y1],'inewidth',2)
hold off
axis([-1.3 1.3 -1.3 0.25])
pause (0.01)
M(j) = getframe(gcf)
end
moive (M)
videofile = viedowriter('ODEpendulum.avi','uncompressed AVI')
open(viedofile,M)
close(viedofile)


ODE function
function [dtheatadt] = odefunc(t,theta,damp,acc,mass,leng)
theta1 = theta(1);
theta2 = theta(2);
dtheta1dt = theta2;
dtheta2dt = -(damp/mass)* theta2 - (acc/leng)*sin(theta1);
dthetadt = [dtheta1dt;dtheta2dt];
end
Error obtained and steps to solve ;

here the result i wrote reult in program crorect by checking it

while plotting this error obtained by recheck and rewrite the plot command

here the plot exicuting but error obtaine din command window id rectified by checking the speling i wrote

here i missed one input variable in function by correcting in rectified the problem