Week 3 - Solving second order ODEs
https://drive.google.com/file/d/1lOdIaY6CVOsjnS1iuysQkWJWfGedV09i/view?usp=sharing
SOLVING SECOND ORDER ODEs
AIM:
To write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping.
INTRODUCTION:
In Engineering, ODE is used to describe the transient behaviour 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 these systems.
DISCRIPTION:
MATLAB code is used to solve the second order ODEs for a simple pendulum. First we adding input variables then we creating a function for the main code that function used to solve second order equation then the function code replaced in the main code. The next step is to create animation of pendulum, after solving the equation in MATLAB we getting the angle and displacement values that values used to plot the result of angle and displacement. Then that values are used to create an animation. Then we find the length of pendulum leg and ‘marker’ command used to draw a bob and fix the axis of pendulum and the values applied to for loop and creating an animation then the created animation change to video file using movie command.
FUNCTION CODE FOR SOLVING 2ND ORDER ODEs
function [dtheta_dt] = ode_fun(l,m,b,g,theta,t);
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
CODE FOR 2ND ORDER ODEs
clear all
close all
clc
%inputs
%length l= 1m
l =1;
%mass m = 1kg
m =1;
%damping coefficient b
b = .05;
%gravity g=9.81m/s^2
g = 9.81;
theta_0 =[0,3];
t_span = linspace(0,20,500);
[t,result] = ode45(@(t,theta)ode_fun(l,m,b,g,theta,t),t_span,theta_0);
%plotting result
figure(1)
plot(t,result(:,1));
hold on
plot(t,result(:,2));
%to create an animation of 2nd order ODE
ct =1;
x0 = 0;
y0 = 0;
for i =1:length(t_span)
x1 = l*sin(result(i,1));
y1 = -l*cos(result(i,1));
figure(2)
plot([x0 x1],[y0 y1],'LineWidth',4,'Color','g')
axis([-2 2 -2 2])
hold on
plot([-1 1],[0 0],'linewidth',10,'color','b')
plot( x1,y1,'marker','o','MarkerSize',15,'Color','r','MarkerFaceColor','r','MarkerEdgeColor','b')
grid on
hold off
M(ct)=getframe(gcf);
ct = ct+1;
end
movie(M)
videofile = VideoWriter('animated2ndODE.avi')
open(videofile)
writeVideo(videofile,M)
close(videofile)
COMMANDEXPLANATION:
Clear all : Clear all is used to clear the variables run in MATLAB.
Close all : Close all open figures
Linspace : this command used to create matrix .
Clc : clear all commands in MATLAB screen.
Hold on: All plot hod on the current figure.it do not delete the existing plot
Plot: to plot calculated values and to construct a graph
Function : input and output variable can add in this function for main program
Then add this function name to the main program
Grid on : this command is used to turn on grid lines in graph
Movie(M) : it is used to create a movie frame of output plot
getframe : it is used to captures the current movie frame
VideoWriter : it is used to create a video file and write to the function
CODE EXPLANATION
g = gravity in m/s2,
l = length of the pendulum in m,
m = mass of the ball in kg,
b=damping coefficient.
theta_0: we adding angular velocity value
t_span : adding time interval using linspace command to create matrix
ode45 : it is a MATLAB inbuilt function to solve the function
ode_fun : this is the function which already done in another program and save as ode_fun.m
plot(t,result(:,1)):it is the command which used to plot t and first column values of
result
plot(t,result(:,2)):it is the command which used to plot t and second column values
of result
for i =1:length(t_span): it is loop which is used to assign each value of time
intervals and run each values of time
x1 = l*sin(result(i,1)):it is the position of string in x direction when angle changes
x1 also changes
y1 = -l*cos(result(i,1)): it is the position of string in y direction when angle changes
y1 also changes
marker ‘o’ :it is command which used to add a circle
MarkerSize':it is used to define size of circle
MarkerFaceColor : it is used to fill the color in circle
MarkerEdgeColor: it is used to add color in edge of circle
ERROR WHEN PROGRAMME RUN
When function code run I didn’t get graph because dtheta2_dt and dtheta1_dt are interchanged so plot get wrong
Error snap attached with answer box