Challenge

Week 3 - Solving second order ODEs

https://skill-lync.com/student-projects/creating-simple-pendulum-motion

The challenged has been attached as project.please do check the above link leads to the challange on motion of simple pendulum.

maincode

% Solving second order ODEs 
close all
clear all
clc


% few given parameters:
%length of pendulum (L) in meter
L=1;
%mass of ball (M) in kg
m=1;
%damping coefficient (b) 
b=0.05;
%accelaration due to gravity (g) in m/s^2
g=9.81;
%vaiable to cpunt frames
ct=1;
%time span of ossilaton (t_span) 
t_span=linspace(0,20,500);
%angular displacement at time t=0 from timespan of occilation
ang_disp0=0;
%angular velocity at time t=0 from timespan of occilation in rad/sec
ang_vel0=3;
%create an ode vector theta_0[displacement,velocity] at initial conditon
theta_0=[ang_disp0;ang_vel0];

%using an inbuilt function ode45 to get value from function
[t,results]= ode45(@(t,theta) ode_fun(t,theta,b,g,L,m),t_span,theta_0);

%plot graph for angular displacement
plot(t,results(:,1)) ;

%hold values on graph
hold on

%plot graph for angular velocity
plot(t,results(:,2));

%label y-axis as plot
ylabel('plot')

%label x-axis as time
xlabel('time')


%to create animation of pendulum
%let us take the value from 1stode 
thetax=results(:,1);

%creating forloop for the plotswit i value from 1 to length(thetax)
for i=1:length(thetax)
    theta_x=thetax(i);
%   fixed point of pendulum (x0,y0)
    x0=0;
    y0=0;

%   posission of the other end of pendulum(x1,y1)at angle theta_x
    x1=L*sin(theta_x);
    y1=-L*cos(theta_x);

%   creating a second plot
    figure(2)

%   ploting a base line which represent a celing or stucture where one end
%   of pendulum is attached
    plot([-1 1],[0 0], 'Color','r','LineWidth',4);
    hold on

%   ploting aline from atached point of sting to length of pendulumat an angle    
    plot([x0 x1],[y0 y1], 'Color','g','LineWidth',4);
    hold on
%   ploting a o-shaped marker to repesent weight of pendulum
    plot(x1,y1,'o','Markers',15,'MarkerFaceColor','b');
    hold off

    %label y-axis as plot
    ylabel('y axis')

%   label x-axis as time
    xlabel('x axis')
    grid on
%   fix the axis value to the graph
    axis([-1.5 1.5 -1.5 0.5]);
%   pausethe proseses for a brief period   
    pause(0.001);

%   adding frames
    M(ct)=getframe(gcf);
    ct=ct+1;


end

% to create a video file of the animation

%use function movie() to process the data from M(ct)
movie (M)

%create new video file with video name and format uncompressed avi 
videofile=VideoWriter("ode2challenge1.avi","Uncompressed AVI")

%command to open the video file
open(videofile);

%write the plots/frames to video file
writeVideo(videofile,M);

%close the videofile and save
close(videofile);

functioncode

% creating a function ode2_function with input time,theta,damping
% coefficient,gravity,length,mass
%save value to dtheta_dt
function [dtheta_dt]=ode_function(t,theta,b,g,L,m)
%   assign 1st oder differntial equation
    theta1=theta(1);
%   assign 2nd oder differential equation
    theta2=theta(2);
%   so we can solve  1st oder  
    dtheta1_dt=theta2;
%   so we can solve  2st oder
    dtheta2_dt=-(b/m)*theta2-(g/L)*sin(theta1);
%represent value of 1st and 2nd oder in a colum vecrtor
    dtheta_dt=[dtheta1_dt;dtheta2_dt];
end

output

https://www.youtube.com/watch?v=dhPgFOfW5aA

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.