Week 3 - Solving second order ODEs
second order differenceal equation to solve the simple pendulum simulation.
d^2theta/dt^2+b/m.dtheta/dt+g/lsin(theta) = 0
create two ODE
dtheta1/dt = theta2 and dtheta2/dtheta = -(b/m(theta2)+g/lsin(theta1))
codes for calling a function :-the function which we have define with some parameter are going to use in the other programe the function is going to solve the second order equation or we can say that it going to integrate the value
function[dtheta_dt] = ode_func(t,theta,b,g,l,m);
theta1 = theta(1);
theta2 = theta(2);
dtheta_dt = theta2;
dtheta_dt = -(b/m)*theta2-(g/l)*sin(theta1);
dtheta_dt = [dtheta1_dt;dtheta2_dt);end
code simple pendulum
ODE system are two firest order velocity and other displacement and initial condition for displacement and velocity
ode 45 inbuilt solver in matlab and octave
time theta and other parameter
b = 0.5,
g = 9.81m/s
l = 0.9 in m
m = 2kg
clear all
close all
clc
%data
b = 0.05;
g = 9.81;
l = 1;
m = 1;
x0 = 0;
y0 = 0;
ct = 1;
%initial condition
theta_0 = [0,3]
% tmie_plot
t_span = linspace(0,20,500);
%solve_ode
[t,result]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
plot(t,result(:,1))
xlable('plot')
ylable('plot')
%pendulum simulation using ode
for i = 1:length(result(i,1);
d = result(i,1);
%defining x and y coordinates
x1 = -l*sin(d);
y1 = -l*cos(d);
hold on
plot([x0 x1],[y0 y1],'linewidth',4);
plot(x1,y1,'-o','markersize',30,'markeredgecolor','g','markerfacecolor',[0.5 0.5 0.5]);
plot(x1,y1,'-o','markersize',3,'markeredecolor','g','markerfacecolor',[0.5 0.5 0.5]);
axisz9[-2 2 -2 2]);
pause(0.01);
m(ct) = getframe(gcf)
ct = ct+1;
clf()
end
%animation