Challenge

Week 3 - Solving second order ODEs

https://drive.google.com/file/d/16m38L-X_5ODhISWUAZeUMNMjek8nwG1W/view?usp=sharing

function xdot = ode_func(t,x) 

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

xdot=zeros(2,1)%assigning a null matrix to xdot
xdot(1)= x(2) %first element is angular velocity which is at a position x(2)
xdot(2)= (-b/m)*x(2)-(g/l)*sin(x(l)) % this will obtain after converting second oder ode to first order ode

end

clear all;
close all;
clc;

%g = gravity in m/s2,
%l = length of the pendulum in m,
%m = mass of the ball in kg,
%b=damping coefficient.

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

%initial condition
theta_0= [0;3]; %the elements of the column vector represent the initial angular displacement and the initial angular velocity of the pendulum.

%time points

t_span = linspace(0,20,500); %the time span for which the differential equation is solved

[t, x] = ode45('ode_func',t_span, theta_0);

plot(t, x(:,1));
hold on;
plot(t, x(:,2));
ylabel('plot');
xlabel('time t in sec');

%solving the ODE and generating the animation
x0 =0;
y0 =0;

ct=1 % counter is added to get the current frames for each iteration
for i = 1:length(t_span)
    x1= l*sin(x(i,1));
    y1= -l*cos(x(i,1));
    
    figure(2)
    plot([-1,1],[0,0],'linewidth',3,'color', 'k');
    axis([-2 2 -2 2]);
    line([x0 x1],[y0 y1],'linewidth',3,'color','b');
    hold on;
    plot(x0, y0,'O', 'markersize', 3,'markerfacecolor','y');
    plot(x1, y1,'O', 'markersize', 20,'markerfacecolor','r');
    title('damping of pendulum');
    legend('support rod','length of pendulum', 'fixed point', 'bod of pendulum');
    grid on;
    hold off;
    pause(0.03);
    M(ct)= getframe(gcf);
    ct = ct+1;


end

movie(M)
videofile = VideoWriter('Pendulum.avi', 'Uncompressed 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.