Challenge

Week 3 - Solving second order ODEs

Please find the atatched code for review

https://drive.google.com/file/d/1pWlujwbEfC-rHMCHdAt26oWj0XWlZbLb/view?usp=sharing

clear all
close all

% Simulating the Pendulum motion by solving the 2nd order ode
% Inputs
g = 9.81; %gravity in m/s2,
l = 1;    %length of the pendulum string in m
m = 1;    %mass of the ball in kg
b = 0.05; %damping coefficient

% Initial Condition (when pendulum is at rest) 
theta_rest= [0;3]; % Angular displacement =0, Angular velocity=3 rad/sec (condition when theta is at rest)
% Initial Time Condition (Simulate for 20 sec)
t_x= linspace(0,20,100);

% solve the ODE equation
[t,y]= ode45(@(t,theta)sec_ode_equation(t,theta,b,g,l,m),t_x,theta_rest); % @(t,theta) used because they are dependent variables

% plot the relation

plot(t,y(:,1)) % plotting the value of first column of y
hold on
plot(t,y(:,2)) % plotting the value of first column of y
xlabel('Angular displacement')
ylabel('Angular velocity')
% Motion simulation of pendulum 
ct=1;
for i=1:length(y(:,1)) % considering the values of first column ie displacement
    x0=0; % Position of pendulum when at rest
    y0=0; % Position of pendulum when at rest

    x1= l*sin(y(i,1)); % Position of pendulum when at a particular position y(i,1)
    y1=-l*cos(y(i,1)); % Position of pendulum when at a particular position y(i,1)
 
    % Plot the fixed point for pendulum 
    plot([-1.5 1.5],[0 0]); % Plot the line where pendulum is attached(-1.5 1.5) is the dispacement line in x direction
hold on 
    plot([x0 x1],[y0 y1],'linestyle','--','LineWidth',1,'Color','b') % Plot the line connecting the end and the bob of pendulum
    axis([-1.5 1.5 -1.5 1.5]);
plot(x1,y1,'o','Markersize',20,'markerfacecolor','r','Color','r') % Plot the point when pendulum is in motion
pause(0.05)
M(ct)= getframe(gcf);
hold off
ct=ct+1;
end

% Movie Animation
movie(M)
videofile= VideoWriter('Simple Harmonic motio.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.