Challenge

Week 3 - Solving second order ODEs

% animation for  the second order of a simple pendulum with damping

% Clear all variables from mengry
% close all charts and figures
% clear command windows

clear all 
close all
clc


%inputs

b = 0.05 ; % b = damping coefficient , 
g = 9.81 ; % g = gravitational force   , m/s^2
l = 1 ; % l = length of pendulam , m
m = 1 ; % m =- mass , kg
 
% initial condition at time=o

theta_0 = [0;3] ; %initial displacement = o , initial angular velocity = 3


%time points 

t_span = linspace(0,20,300) ;  %time array for intrgrating ode solution

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

% ode45(@(t,theta) = find out Angular Displacement with respect to time
% ode_func(t,theta,b,g,l,m) = call ode function
% t_span time array total 100 from 0 to 20


ct = 1;

% initial value for the plot number; set it 1

for i=1:length(results(:,1))

% i is the range of first column of results

x0 = 0; % fixed point of link in x-axis

y0 = -0.05; % Fixed point of link in y-axis

x1 = l*sin(results(i,1)); % angular movement in x-axix


y1 = -l*cos(results(i,1)); % angular movement in y-axix



x2 = (l/2)*sin(results(i,1));


y2= -(l/2)*cos(results(i,1));


% Plot pendulum support line

plot([-0.6 0.6],[0 0], 'linewidth',8, 'color', 'k');

hold on

% hold on use to put next plot in same chart

% Plot pendulum support dot

plot (0,-0.03,'o','MarkerSize', 12, 'MarkerEdgecolor', 'k', 'MarkerFacecolor', 'k');

% Provid Link with length= 1

line([x0 x1],[y0 y1],% the second order ODE for a simple pendulum with damping

% Clear all variables from mengry
% close all charts and figures
% clear command windows

clear all 
close all
clc


%inputs

b = 0.05 ; % b = damping coefficient , 
g = 9.81 ; % g = gravitational force   , m/s^2
l = 1 ; % l = length of pendulam , m
m = 1 ; % m =- mass , kg
 
% initial condition at time=o

theta_0 = [0;3] ; %initial displacement = o , initial angular velocity = 3


%time points 


t_span = linspace(0,20,500) ;  %time array for intrgrating ode solution

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

% ode45(@(t,theta) = find out Angular Displacement with respect to time
% ode_func(t,theta,b,g,l,m) = call ode function
% t_span time array total 100 from 0 to 20


% subplot To plot multiple plot in single figure
% (18,18,[61,70,91,100]) 10,10 is 10x10 matrix

% [61,70,91,100] position of plot in 10x1e matrix

subplot (10,10, [61,70,91,100]); % plot pendulum position by using results matrix first column with respect to time 
 plot(t, results(:,1), 'linewidth',2) % linewidth use to define width of line

hold on    % hold on use to put next plot in same chart

% plot pendulum position by using results matrix second column with respect to time

plot(t,results(:,2), 'linewidth',2) % linewidth use to define width of line

hold off

% hold off use for turn of the hold on command

title('Pendulum position')

% title command use for provide title to plot

xlabel('Time')

ylabel('Angular Displacement') 

grid on

subplot(10,10,[8,10,38,40])
plot(results(:,1),results(:,2), 'LineWidth',2)
title('angular velocity')
xlabel('position radious')
ylabel('angular velocity')
grid on


ct = 1;

% initial value for the plot number; set it 1

for i=1:length(results(:,1))

% i is the range of first column of results

x0 = 0;

% fixed point of link in x-axis

y0 = -0.04;

% Fixed point of link in y-axis

x1 = l*sin(results(i,1)); % angular movement in x-axix


y1 = -l*cos(results(i,1)); % angular movement in y-axix


% movemet for text 'Link' in x-axis

x2 = (l/2)*sin(results(i,1));

% movemet for text Link in y-axis

y2= -(l/2)*cos(results(i,1));

% title command use for provide title to plot

% subplot To plot multiple plot in single figure

% (10,10,[1,6,41,46]) 10,10 is 10x10 matrix

 % [1,6,41,46] position of plot in 10x10 matrix

subplot (10,10,[1,6,41,46])

% Plot pendulum support line

plot([-0.6 0.6],[0 0], 'linewidth',8, 'color', 'k');

hold on

% hold on use to put next plot in same chart

% Plot pendulum support dot

plot (0,-0.03,'o','MarkerSize', 12, 'MarkerEdgecolor', 'k', 'MarkerFacecolor', 'k');

% Provid Link with length= 1

line([x0 x1],[y0 y1], 'linewidth',3,'color','b')

% Name the link with L=1
 
t = text((x2+0.04),y2, 'L=1');

% give font size

s = t.FontSize;

% Mention font size of 12

t.FontSize = 12;

hold on;

% hold on use to put next plot in same chart



% plot pendulum circle

% with arkers give size to pendulum

% with markerfacecolor give face color to the pendulum circle 

plot (x1,y1,'-o','markers', 20, 'markerfacecolor', 'r') 

% Text command to provide 'm' as a name to pendulum circle

t = text((x1-0.04), (y1), 'm'); % give font size

s = t.FontSize;

% Mention font size of 12

t.FontSize = 12;



title('Pendulum Damped Animation')

xlabel('x-axis') 

ylabel('y-axis')

grid on

% labeling of y-axis

% enable grid in graph

axis ([-1.5 1.5 -1.5 0.5])

% Fix axis by setting x and y limit

pause(0.003)

hold off

% Pause interval between each frame

% hold off use for turn of the hold on command

% Set frame position and size

a0=20; % Initial x-axis

b0=20; % Initial y-axis

width=900; % width of the frame

height=655; % hight of the frame


set(gcf, 'position', [a0, b0,width, height]) 

% set poition and side using set command

% save all frame in M named array

% in M(ct), ct represents each and every frame to store in M

% use getframe command to get current frame window in each loop)

M(ct) = getframe(gcf);

% increment of number of frame in each loop

ct=ct+1;

end

% after the end of  for loop , we use a Movie command to geneate a movie realte to
% above program
movie(M);

%create an avi file to store the movie
% we have to give file name and profile 

videofile = VideoWriter('simple pendulam.avi' , 'Uncompressed AVI') ;
open(videofile)
writeVideo(videofile,M)
close(videofile)


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.