Challenge

Week 3 - Solving second order ODEs

%%Program to simulate motion of simple pendulum
clear all; close all; clc;
%Inputs
b=0.05;%damping coeff
g=9.81 %gravity
l=1%Length of pendulum
m=1%mass of ball
%INITIAL CONDITIONS of angular displacemet & Velocity at t=0
Y0 = [0;3]; 
%TIME SPAN
t_span = linspace(0,20,500);
%SOLVING ODE
[t,Y0] = ode45(@(t,Y0) ode_func(t,Y0,b,g,l,m), t_span,Y0);
%PLOT
figure(1);
plot(t,Y0(:,1));
hold on
plot(t,Y0(:,2));
xlabel('Time');
ylabel('Angular displacement & Velocity');
legend('Angular Displacement','Angular Velocity');

%Pendulum Initial condition
x0 =0;
y0 =0;
ct=1;
for i = 1:length(t_span)
    x1=l*sin(Y0(i,1));
    y1=-l*cos(Y0(i,2));
figure(2);

plot([-1 1],[0 0],'LineWidth',6,'Color','b');
axis([-2 2 -2 2]);
hold on
line([x0 x1],[y0 y1],'LineWidth',3,'color','g');
plot(x0,y0,'o','MarkerSize',3,'MarkerFaceColor','y');

plot(x1,y1,'o','MarkerSize',20,'MarkerFaceColor','r');
grid on
hold off
M(ct)=getframe(gcf);
ct=ct+1;
end

%Create Videofile
movie(M)
videofile = VideoWriter('ODE_Pendulum_Animation.avi');
open(videofile);
writeVideo(videofile,M);
close(videofile)

EXPLANATION -

The Pendulum details are given like b,g,l,m at initial conditions of distance and velocity as 0 and 3 respectively at t=0.

As we need to calculate values of angular displacement and velocity for 20 seconds hence used linspace(0,20,500) where we get 500 values of time t.

After giving inputs, apply ode45 function where we will get 500 values of both displacement and velocity with respect to time.

plot figure of Time vs Angular displacement & Angular velocity.

TO CAPTURE ANIMATION OF SIMPLE PENDULUM WE APPLIED for loop with the help of geometrical formulas for x1 and y1 where x0 and y0 are pivot points hence are equals to 0

ERROR

Put yo instead of y0

In Marker command used 0 instead of o

https://drive.google.com/file/d/1EFpXyerhj94zSsH9zQTAvu8RgcKqkV6U/view?usp=sharing

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.