Challenge

Week 3 - Solving second order ODEs

clear all
close all
clc


b=0.15; % Damping co efficient
g=9.81; %gravitational force
l=1;       %lenght 
m=0.1; % mass


%initial condition
theta_0=[0;5];

%time space
t_span=linspace(0,10,500);

% solving the  ODE


[t,results]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0)

% ploting the graph

plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')

%function programm used for this perticular ODE

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

                  % result by increasing damping co-efficient

            %ODE first result

%for Annimation

clear all
close all
clc


b=0.1;
g=9.81;
L=0.5;
m=0.3;

%for simplification
r=g/L;
k=b/(m*L);

%the pendulum ODE
f=@(t,x)[x(2);-k*x(2)-r*sin(x(1))];

%initial conditions
init=[pi/2;0];

%solve ODE the time intervel set from 0 to 20 sec

[t,x]=ode45(f,[0 20],init);

%for annimation

%origin
O=[0,0];
axis(gca,'equal');
axis([-0.7 0.7 -0.7 0.2]); % the limits of the plot
grid on; % unable the grid


% loop for the annimation

for i=1:length(t)

%mass point
P=L*[sin(x(i,1))-cos(x(i,1))];
%circle in origin
O_circ=viscircles(O,0.01);

%pendulum
pend=line([O(1) P(1)],[O(2) P(2)]);
%ball
ball=viscircles(P,0.05);
%time interval to update the plot
pause(0.001);
%delete the previous length
if i delete(pend);
delete(ball);
delete(O_circ);
end


end

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.