Challenge

Week 3 - Solving second order ODEs

% solving Second order ODE and Pendulum animation

clear all
close all
clc

% Inputs
l=1;
m=1;
b=0.05;
g=9.81;

%initial condition
theta_0= [0;3];

% Time points
t_span = linspace(0,20,500);

% solve ode
[t, results] = ode45(@(t,theta) ode_func(t,theta,l,m,b,g),t_span, theta_0);

% graph
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')
grid on

%animation
X0=0;
Y0=0;

% Looping 
ct=1;
for i=1 : length(t)
    X1= l*sin(results(i,1));
    Y1= -l*cos(results(i,1));

    figure(2);
    plot([-1 1],[0 0],'linewidth',5,'Color','b');
    axis([-2 2 -2 2]);
    line([X0 X1],[Y0 Y1],'linewidth',2,'color','g');
    hold on;
    plot(X0,Y0,'O','MarkerSize',3,'MarkerFaceColor','y');
    plot(X1,Y1,'O','MarkerSize',20,'MarkerFaceColor','r');
    title('damping of simple pendulum');
    legend('support rod','length of pendulum','fixed point','bod of pendulum');
    grid on;
    hold off;
    M(ct)=getframe(gcf);
    ct=ct+1;
end

% Smooth animation and making movie
movie (M)
videofile=VideoWriter('simple_pendulum.avi','uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)


https://youtu.be/-cFXAXCkcYo

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.