Challenge

Week 3 - Solving second order ODEs

aim

to write a program that solves the ode and to animate the motion of pendulum.

procedure

1.take the input values and conditions

2.convertion of 2nd order to first order ode

3.create a function ode

4.create a simple pendulum progtram using the created ode function

5.with a time span of 20 seconds with 500 steps.

6.solving function by using ode45



clear all
close all
clc
l=1;
m=1;
g=9.81;
b=0.05;

dis_angvelo=[0;3];
t_span=linspace(0,20,500);
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,dis_angvelo);
figure(1)

plot(t,results(:,1),'linewidth',1)
hold on
plot(t,results(:,2),'linewidth',1)
axis([0 25 -3 3])
xlabel('Time - sec')
ylabel('Angular Velocity&Displacement')
legend('change in dispacement','change in angular velocity')
hold off

figure(2)
plot(results(:,1),results(:,2))
xlabel('Position-rad')
ylabel('Angular Velocity-rad/sec')
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],'linewidth',2);
hold on
plot([x0 x1],[y0 y1],'color','r','linewidth',2)
hold on
plot(x1,y1,'o','markers',20,'markerfacecolor','b')
hold off
grid on
axis([-1.5 1.5 -1.5 1]);
Angular_Displacement=results(i,1)
Angular_Velocity=results(i,2);
txt1=['Angular Displacement : ' num2str(Angular_Displacement) 'rad'];
txt2=['Angular Velocity: ' num2str(Angular_Velocity) 'rad/s'];
text(-0.5,0.5,3,txt1,'color','r')
text(-0.5,0.2,3,txt2,'color','b')
pause(0.003)
M(ct)=getframe(gcf);
ct=ct+1;
end

movie(M)
videofile=VideoWriter('pendulum.avi','MPEG-4');
open(videofile)
writeVideo(videofile,M)
close(videofile)

function program

function [dtheta_dt]= ode_func(t,theta,b,g,l,m)
theta1=theta(1)
   theta2=theta(2)
  
    
    dtheta1_dt=theta2;
 dtheta2_dt=(-b/m)*theta(2)-(g/1)*sin(theta(1));
 dtheta_dt=[dtheta1_dt; dtheta2_dt];
 
    
    end

plot

video link for the pendulum simulation

https://drive.google.com/file/d/1Y2_t-zEaPZFMdddU_UEeCXQRz0UjTMcw/view?usp=sharing

conclusion

plot is shown of how the displacement and angular velocity change with respect to time.

errors faced

removed the inverted commas for the number after the line width

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.