Challenge

Week 3 - Solving second order ODEs

clear all
close all
clc

% Defining Fix Values.

b=0.05;
g=9.81;
l=1;
m=1;
ct=1;

% Initial Condtion Defines the Displacement,Velocity

theta_0 = [0;5];

% Time span

t_span = linspace(0,10,500);

% Solving and Calling the ODE function

[t, Results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);

plot(t,Results(:,1))

hold on

plot(t,Results(:,2))

ylabel('plot')
xlabel('time')

%Pendulum Animation Reffering to Listed Data.

figure(2)

theta2 = Results(:,1);

for i=1:length(theta2)

    theta2(i)

    x0=0;
    y0=0;

    x1=l*sin(theta2(i));
    y1=-l*cos(theta2(i));

    % Actual Plotting

    figure(2)
    plot([-0.5 0.5],[0 0],'color','b','linewidth',5);
    hold on
    plot([x0 x1],[y0 y1],'color','r','linewidth',2);
    
    plot(x1,y1,'o','markers',15,'markerfacecolor','y');
    hold off
    grid on
    axis([-1.5 1.5 -1.5 0.5])
    
    M(ct)= getframe(gcf);
    ct = ct+1
end
movie(M)
videofile = VideoWriter('pendulum_animation.avi')
open(videofile)
writeVideo(videofile,M)
close(videofile)

    



Calling ODE function Program




% Calling Function ODE Program

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

Link for pendulum animation video

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

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.