Challenge

Week 4.1 - Solving second order ODEs

close
clear
clc
% inputs 
l = 1;
m = 1;
b = 0.05;
g = 9.81;
%initial conditions 

t_span = linspace(0,20,200);
% at time t=0 angular displacement and velocity is 0 and 3 respectively.
theta_0 = [0;3];

% use of ode45 solver
[t,results] = ode45(@(t,theta) ode_fun(t,theta,b,g,l,m),t_span,theta_0);

%generation of plot 
plot(t,results,'LineWidth',3);

%labeling
xlabel('angular displacement');
ylabel('angular velocity')


%creation of animation for pendulum motion

theta2 = results(:,1); % angular displacement array from 'results'

ct = 1;                 %inition of animation frames


% generation of for loop for pendulum motion
for i = 1:length(theta2)    % length of loop
    theta = theta2(i);      % indexing for theta

    x0 = 0;                  % initial values 
    y0 = 0;                  % initial values 
    x1 = l*sin(theta);       % final values 
    y1 = l*cos(theta);       % final values 

    figure(2)    % generation of seprate plot window 

    plot([-1 1],[0 0],'Color','k','LineWidth',3)
    hold on 
    plot([x0 x1],[y0 y1],'Color','m','LineWidth',3)

    plot(x1,y1,'o','markers',10,"MarkerFaceColor","b")
    hold off
    grid on
    axis([-2 2 -2 2]);
    pause(0.03)
    P(ct) = getframe(gcf); % storing of frames in 'p'
    ct = ct+1;



end

% VIDEO CREATION STEPS
movie(P)    % creation of movie of array frames' p'
videofile = VideoWriter('pendulum motion.avi','Uncompressed AVI')
open(videofile)   
writeVideo(videofile,P)
close(videofile)

outputs

workspace outputs

Links of video

https://youtu.be/nB8I0jNm79Q

function code

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.