Project

second order ode in matlab

In this challenge the code is written to solve second order differential equation using matlab. This folowing code simulates an animation for a simple pendulum with damped oscillations. This equation involves mass of ball, length of the pendulum, gravity and damping coefficient.

The differential equation will be

\"\"

clear all
close all
clc

%inputs
l = 1;
m = 1;
g = 9.81;
b = 0.5;
x0 = 0;
y0 = 0;

%initial value of theta
theta -0 = [0;3];

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

%ode
[t,results] = ode(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
ct = 1;
for i = 1:length(t_span)
r = results(i) - pi/2
x1 = -l*cos(r);
y1 = 1*sin(r);
plot([x0 x1],[y0 y1],\'-o\' ,\'linewidth\',3,\'color\',\'r\')
axis([-1 1 -1 0]);
M(ct) = getframe(gcf);
ct = ct+1;
end
movie(M)
videofile = VideoWriter(\'ode_main.avi\',\'uncompressed AVI\');
open(videofile)
writevideo(videofile,M)
clsoe(videofile)

Function code for the main code

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

plot diagram

\"\"

the link for animation of the pendulum is given below

https://drive.google.com/open?id=1x8a9LgAE_7qLkTswoRW9W6PdlQNQoCU6

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.