Challenge

Week 3 - Solving second order ODEs

clear all
close all
clc

b=0.05;
g=9.81;
l=1;
m=1;
theta_0=[0;3];
t_span=linspace(0,20,1000);
[t,results]=ode45(@(t,theta) ode_fun(t,theta,b,g,l,m),t_span,theta_0);
subplot(5,4,[13,14,17,18]);%Placing plot in the left corner
plot(t,results(:,1),'color','b')
hold on
plot(t,results(:,2),'color','r')
xlabel('Time');
hold off
subplot(5,4,[15,16,19,20]);%Placing plot in the right corner
plot(results(:,1),results(:,2))
xlabel('Position-Radians');
ylabel('Angular Velocity');

ct=1;
for i=1:length(results(:,1))
    %initial points of arm
    x0=0;
    y0=0;
    %end points of arm and place where weight will be
    x1=1*sin(results(i,1));
    y1=-1*cos(results(i,1));
    subplot(5,4,[1,2,3,4,5,6,7,8,9,10,11,12]);
    plot([-1 1],[0 0],'Linewidth',4,'color','k');%Support/Base line
    hold on
    line([x0 x1],[y0 y1],'Linewidth',2,'color','b');%arm of the pendulum
    hold on
    plot(x1,y1,'-o','Markersize',20,'Markerfacecolor','r');%weight at the end of pendulum
    grid on
    axis([-1.5 1.5 -1.5 0.5])
    pause(0.0003);
    hold off
    q(ct)=getframe(gcf);%To make animation video
    ct=ct+1;
end

movie(q)
videofile=VideoWriter('Pendulum.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,q)
close(videofile)

Ode Function:-

function[dtheta_dt]=ode_fun(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

Youtube link:-

https://youtu.be/YgUPbHGLUHw

Google Link:-

https://drive.google.com/drive/folders/1b0WqmYP6VP5zrfl7uN9xeOvHypvaj4yG?usp=sharing

Not many or major errors are faced. Some of them are syntax-related like marker size and sizing subplots.

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.