Week 3 - Solving second order ODEs
%ODE_MAIN
clear all
close all
clc
b=0.05;
g=9.81;
l=1;
m=1;
A_d=0;
A_v=3;
t=0;
% Initial condition;
theta_0 = [0;3];
% time points
t_span = linspace(0,10,500);
% solve ODE
[t,results] = ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
figure(1)
plot(t,results(:,1))
hold on
plot (t,results(:,2))
ylabel('plot')
xlabel('plot')
x0=0;
y0=0;
ct=1;
for i=1:length(t_span)
x1=1*sin(results(i,1));
y1=-1*cos(results(i,1));
figure(2)
plot([x0 x1],[y0 y1]);
hold on
plot(x1,y1,'.','MarkerSize',40)
axis([-2 2 -2 2])
hold off
M(ct)=getframe(gcf);
ct=ct+1;
end
movie (M)
videofile = VideoWriter('fwi.avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)
%ODE_FUNC
function [dtheta_dt] = ode_func(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
https://drive.google.com/file/d/1xG2EIGsMmNOSemJUq5fsJ_Gv8pG29X2K/view?usp=sharing
Explanation:
Errors faced while programming.
Error 1: Forgot to inert the @ symbol in ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
Error 2:Forgot to mention the * symbol for x1 and y1. Here * is used for multiplication.
x1=1*sin(results(i,1));
y1=-1*cos(results(i,1));