Challenge

Solving second order ODEs

% Solving 2nd Order ODE
clear all
close all
clc

%inputs
l = 1;
m = 1;
b = 0.5;
g = 9.81;

theta_0 = [0;3];

%time plots
t_span = linspace(0,10,250);

%Solution for ODE
[t,results] = ode45(@(t,theta) ode_function(t,theta,l,m,b,g),t_span,theta_0);

%plotting
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('time');
ylabel('plot');
legend('angular displacement','angular velocity');

% for loop
ct = 1;
figure(2)

for i=1:length(results(:,1))
theta0 = results(i,1);

x0 = 0;
y0 = 0;

x1 = 1*sin(theta0);
y1 = -1*cos(theta0);

%plotting
plot([-1,1],[0,0],'linewidth',10,'Color','black');
line([x0,x1],[y0,y1],'linewidth',3, 'color', 'r');
hold on
plot(x1,y1,'O','markers',20,'markerfacecolor','b');
axis([-2 2 -2 2]);
pause(0.01);
M(ct) = getframe(gcf);
hold off
ct = ct+1;
end

%Annimation
movie(M)
videofile=VideoWriter('simple_pendulum.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

function [dtheta_dt] = ode_function(t,theta,l,m,b,g)

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

Video Link is as follows

https://drive.google.com/file/d/13M25e4mXWbXsNkeSTBTPYRvOzq1YGtvi/view?usp=sharing

Explanation

1) Providing input variables

2) Giving values of theta

3) Solving ode by use of ode formula

4) Plotting the graph

5) Using 'for' command

6) Providing equation

7) Plotting all coordinates

8) Using pause command

9) End 'for' loop

10) Using movie command for annimation

11) Saving movie in .avi format

Errors faced & steps taken are as follows

As usual I made silly spelling mistakes which resulted in some errors which I rectified it.

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.