Challenge

Week 3 - Solving second order ODEs

Function used in program to animate motion of pendulum

function [dtheta_dt] = ode_func(t,theta,b,g,L,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

Program used to simulate motion of pendulum and animate it

clear all
close all
clc

b = 0.05;
g = 9.81;
L = 1;
m = 1;

% initial condition
theta_0 = [0;3];

% time points
t_span = linspace(0,30,500)

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


% Plot to animate pendulum
figure(1)
ct=1;
for i=1:length(results(:,1))
    x0=0;
    y0=0;
    plot([-1 1],[0 0],'linewidth',3,'color','c');
    hold on
    % position of bob
    x1=L*sin(results(i,1));
    y1=-L*cos(results(i,1));
    % plotting of length of pendulum
    plot([x0,x1],[y0,y1],'linewidth',2,'Color','b');
    %plotting of fixed end
    plot(x0,y0,'-s','MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',10);
    %plotting of bob
    plot(x1,y1,'-o','MarkerEdgeColor','k','MarkerFaceColor','m','MarkerSize',20);
    hold off
    axis ([-1.4 1.7 -1.5 0.5]);
    grid on
    titlestring=['Pendulum motion at time t=',num2str(t(i)),'seconds'];
    title(titlestring,'fontsize',10);

    M(ct)=getframe(gcf);
    ct=ct+1;

end

%code to animate the motion
movie(M)
videofile=VideoWriter('odesimplependulum.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Youtube link of motion video

https://youtu.be/ohH4-iEpQ-E

First function is created to solve ode using formulas and values required to solve ode for motion of pendulum

Then program is created for motion of pendulum

First the values of initials given in the challenge are defined

then theta and time span points are defined

after that ode is solved using pre defined matlab command 'ode45' and our created function

then for loop is used to get multiple values and repeating the program for getting different points

then for plotting we used plot command and defined the required values and markers for bob of pendulum, length of pendulum, fixed point

now for creating a movie we first created a frame using getframe command then using movie command and videowriter command we created a video

the link of the video is given above and figure is attached with the report

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.