Challenge

Week 3 - Solving second order ODEs

https://drive.google.com/file/d/1VFzmz0VOD9b7o3tCkkvSDvJ_S2sG9qV1/view?usp=sharingAim:To write progam to solve second order ODE that represents the motion of asimple pendulum and to animate its motion

Input from the question :

g = 9.81 m/s2,gravity

L = 1meter ,length of the pendulum

m = 1 kg,mass of the ball

b = 0.05, damping coefficient

Function program:

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

dtheta_dt = [dtheta1_dt;dtheta2_dt];

end

MAIN PROGRAM
clear all
close all
clc

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

% initial condition 
theta_0 = [0;5];

%time points
t_span = linspace(0,20,100);

% solve ODE
[t,results] = ode45(@(t,theta) ode_fun(t,theta,b,g,l,m),t_span,theta_0);
figure(1)
plot(t,results(:,1))
hold on
grid on
plot(t,results(:,2))
axis([0 30 -2 2])
ylabel('angular velocity and angular displacement')
xlabel('time')




x0=1;
y0=1;
ct=1;
coord = results(:,1);
for i = 1:length(coord)
    figure(2)
    x1 = 1-l*sin(coord(i));
    y1 = 1-l*cos(coord(i));
    plot([0 2],[1 1],'LineWidth',3)
    hold on 
    plot([1,x1],[1 y1],[x0 x1],[y0 y1],'o','MarkerFaceColor','r','LineWidth',2)
    hold off
    axis([0 2 -0.5 1.5])
    grid on
    pause(0.2)

M(ct) = getframe(gcf); 
ct=ct+1;

end
movie(M)
videofile = VideoWriter('pendulum.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
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.