Challenge

Week 3 - Solving second order ODEs

Errors:

1) Faced the first error in ploting the initial graphs to ensure the calculations and method used were correct.but made mistakes in calculating and saving the right theata values which caused in generation of errors followed by generating a plot that rotates the pendulum 360 degres.

2)struggled with the hold command generating wrong plots which i could fix by trial and error.

3) made a mistake in assignment of values and saving folder for the function made to calculate the differential equations.

4) struggled with the for loop saving the write figures and returning the ct values for the animation.

Description:

ode45 function is a pre defined function in mathlab that helps to solve integral equations for defferentials. this command mainly needs 3 values and returns 2 results (time,results) = ode45(fun,time,variable).

The below code is eplained with each line of code including how and why the plots generated are used and what they represent.

youtube link

https://youtu.be/NqLJ7xdgw2o

%% ODE function 

function [dtheta_dt] = ODE_fun(t, theta, b, g, l, m)  
    theta1 = theta(1); % first order of differential
    theta2 = theta(2); % second order for differential
    dtheta1_dt = theta2;
    dtheta2_dt = -(b/m)*theta2 - (g/l*sin(theta1)); % the following the the differential equations required to solve the pendulum problem
    dtheta_dt  = [dtheta1_dt; dtheta2_dt];
    return ;
end

% the function requires the following variables to be defined defines the time period, the amplitude of pendulum, the
% dampening coefficient , gravity , length and mass. 



Program for Pendulum using 2nd order ODE
clear all
close all
clc
clf


Input Variables
b = .02;            % Damping co-efficient 
g = 9.81;           % gravity
l = 1;              % length of pendulum 
m = 0.5;            % Mass of bob
theta = [0;5];      % angle of pendulum in radians initial position to final angular velocity 
t = linspace(0,20,1000); % time period


calculation for ODE
[t, results] = ode45(@(t,theta) ODE_fun(t, theta, b, g, l, m),t,theta);
% function made that calculated the 2nd order differential for the equation
% of pendulum . it calculates the displacement.

figure(1)                         % generates a screenshot of the figure and saves it 
plot(t,results(:,1));             % plots time period with change in displacement of pendulum 
hold on                           % holds the plot to generate the velocity and displacement
plot(t,results(:,2),'-o');        % plots the velocity change with time.
xlabel('Time');
ylabel('velocity');
legend('displacemet','velocity');   % adds text and titels to define the plots for proper understanding and representation
hold off                          % stops overlaping plots from being generated 

figure(2)                        % creates a new figure to save for a different plot to be generated
plot(results(:,1),results(:,2)); % plots the results between velocity and displacement
xlabel('displacement');          
ylabel('velocity');

% NOTE:---- the above generated plote shows how the degreadition in the
% energy of the pendulum occurs as it reaches a position of rest. the
% spiriling of the graph is generates as the displacement and velocity
% change directions and degrade over time with each pass through the
% equilibrium position

Animate
x0 = 0; % initial position of X
y0 = 0; % initial position of Y
ct = 1; % counter point to help generate the animation frames and increaments it with a value of 1 with each loop

theta2 = results(:,1); % displacement results are saved to theata 2 taking the first column of results

for i = 1:length(theta2)
      theta3 = theta2(i);
     % the following loop increaments the value of result file displacement
     % in each pass of loop and calculates the values of position as
     % pendulum moves
   
    x1 = l*sind(theta3);
    y1 = -l*cosd(theta3);
    
    % the above x1 and y1 are the mathematical formulaes to define the
    % positions for the movement of bob   
    
    figure(3); % generates figures for animation
    
    plot([-0.5 0 0.5 0],'linewidth',5); %
    hold on
    plot([0 x1],[0 y1],'linewidth',3); % Generates the length element holding the bob
    hold on
    plot (x1,y1,'-o','MarkerSize',20,'MarkerFaceColor','b'); % generates the bob
    hold off
    axis([-0.5 0.5 -1 0]) % fixes the scale of plot generated
    
    grid on
    pause(0.02); % an interval of .2 second is given to run the calcualtion loops.
    M(ct)= getframe(gcf); % saves the generated figues.
    ct = ct+1; % increaments the value for generating the figures
end

 movie(M) % calls the function to create a movie file 
 vediofile = VideoWriter('ODE_pendulum.avi','uncompressed AVI'); % creates a file that writes the data and save the movie file in the specified format
 open(vediofile) % opens the created writable file 
 writeVideo(vediofile,M) % writes the data or images saved as a vedio files frame by grame generating the animation
 close(vediofile) % closes the files and saves the newly added data.


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.