Challenge

Week 3 - Solving second order ODEs

in this assignment, I am going to solve the second-order equation of the pendulum and used the calculated values to make graph plotting and animation for the same using MATLAB.

Firstly I have created the function code file for the second-order equation using Input parameters like (time, theta, damping, co-eff, gravity, mass). using the function code file in the main code for calculating values.

Note:- Function code file name and function name should be the same to run the code.

function [dtheta_dt]=ode_func(t,theta,b,l,g,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
  1. For the main coding, I have started the usual basic commands like close all, clear all, clc for avoiding overlapping the values of the variables. then I have considered the assumed values for my basic parameters of the pendulum.
  2. In the next step consider the initial values of [0;3] Angular displacement and angular velocity respectively using column matrix symbol.
  3. Then provide the time Points using linspace command for the pendulum oscillations from 0to 20 seconds with 500 iterations.
  4. For solving the ODE function applied Matlab ode 45 solvers using upper function code.
  5. Graph plotting executed the subplot commands and done the required graph-related labelings (X-Y label, the grid on, title, legends, etc.)
  6. for making an animation of the pendulum using for loop in build command to assign the initial and incremental final values with the help of loop counter.
  7. describes the coordinates of the string and assigned the range of the axis. The pause command is used to stop for a certain time
  8. Making Animation part starts with the Movie command video file command gives the video name and format of the file the open used for opening media player, write video for video frames, close(video file ) for closing the video media player.

https://youtu.be/Z4jt3-0VhL8

clear all
close all
clc

b=0.05; % damping Coefficient
g=9.81; % Gravity
l=1; % Length of Pendulum
m=0.1; % Mass

% Initial Conditions
theta_0=[0;3];

% Time Points
t_span=linspace(0,20,500);

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

% Graph Plotting
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('Time')
ylabel('Plot')
title('Damping analysis of a pendulum')
legend('Angular Displacement','Angular Velocity')
grid on

% Pendulum Animation using For loop
ct=1
for i=1:length(t)
    x=l*sin(results(i,1));
    y=-l*cos(results(i,1));

    figure(2)
    grid on
    plot([-1,1],[0,0],'LineWidth',10)
    hold on
    plot([0,x],[0,y])
    hold on
    plot(x,y,'o','linewidth',20,'color','cyan')
    axis([-1.5,1.5,-1.5,1])
    pause(0.05)
    hold off
    M(ct)=getframe(gcf);
    ct=ct+1;
end

% Animation of Pendulum Creation 

movie(M)
videofile=VideoWriter('Pendulum Animation.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.