Week 3 - Solving second order ODEs
- Upload an animation on You tube and provide the You tube link for an animation in your report.
In Engineering, ODE is used to describe the transient behavior of a system. A simple example is a pendulum
The way the pendulum moves depends on the Newtons second law. When this law is written down, we get a second order Ordinary Differential Equation that describes the position of the "ball" w.r.t time.
Similarly, there are hundreds of ODEs that describe key phenomena and if you have the ability to solve these equations then you will be able to simulate any of theses systems.
Your objective is to write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping
In the above equation,
g = gravity in m/s2,
L = length of the pendulum in m,
m = mass of the ball in kg,
b=damping coefficient.
Write a program in Matlab\Octave that will simulate the pendulum motion, just like the one shown in the start of this challenge.
use,
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2.
Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.
When you do this you will get the position of the pendulum as a function of time. Use this information to animate the motion of the pendulum,
- For each value in time
- Create an image that represents the position of the pendulum
- You can assume the length of the string and the size of the ball
- Use the marker command to create a sphere
- One end of the pendulum is fixed
- The other end moves according to the solution of the ODE you had solved.
- If you are using using Matlab, use the movie command create an animation
2. Submit a report that has the following content:
- Step wise explanation of your code.
- Errors faced while programming.
- How have you overcome those errors.
- Screenshots showing errors thrown in command window.
The presentation of your code will be taken into consideration while grading. Labelling sections of your code using comments is appreciated.
Upload the code using "INSERT SAMPLE CODE" option and then post screenshot of the plots generated.
In case if you are not able to finish the challenge, upload until what you have done and explain the procedure and plot. Deserved marks will be allotted.
GENERAL INSTRUCTIONS:
- The codes, plots, images, GIFs, and videos should be submitted only throug
https://drive.google.com/file/d/1JL2Rlv95GV0TodQJ4QTpQiRrOJirmjqq/view?usp=sharing
clear all
close all
clc
%given paramatets
g = 9.81;
l = 1;
m = 1;
b = 0.05;
theta_0 = [0,3]
%we need to represent initial condition to the problum theeta_0 = [0 3]
%given time interval or time spac
time_given = linspace(0,20,500)
[t,results] = ode45(@ (t,theta) second_order_odependulum(t, theta, b,g,l,m), time_given,theta_0)
figure(1)
plot(t, results(:,1))
hold on
plot(t, results(:,2))
xlabel('Time')
ylabel('Angular displacement vs Angular velocity')
legend('Angular displacement','Angular velocity')
grid on
theta2 = results(:,1)
ct = 1
for i = 1:length(theta2)
theta3 = theta2(i)
%for fixed bar and movung bar of a pendulum
x0 = 0;
y0 = 0;
x1 = 1*sin(theta3)
y1 = -1*cos(theta3)
%giveng aaxis, color for the pendulum bob, end.,
figure(3)
plot([-1.1 1.1],[0,0],'linewidth',3)
hold on
plot([x0,x1],[y0,y1])
plot(x1,y1,'o','Markersize',20)
xlabel('x-axis')
ylabel('y-axis')
hold off
grid on
axis([-1.5 1.5 -1.5 1.5])
%time frame to get the the video
pause(0.003)
M(ct) = getframe(gcf)
ct = ct+1
end
movie(M)
Videofile = VideoWriter('simple pendulum.avi','Uncompressed AVI');
open(Videofile)
writeVideo(Videofile,M)
close(Videofile)
function [dtheta_dt] = second_order_odependulum(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