Challenge

Week 3 - Solving second order ODEs

ode pendulum oscillation

clear all
close all
clc

%parameters for calculate ode
b=0.2;
g=9.81;
l=2;
m=1.5;

%initial status
%to solve First order ode intial dispalcement and indtial velocity 0,3m/s
%is given
theta_0=[0;3];

%time pionts
%for 0 to 20 sec we provide 100 time steps
t_span=linspace(0,50,200);

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

%damping_graph 
figure(1)
plot(t,result(:,1))
hold  on
plot(t,result(:,2))
ylabel('dispacement')
xlabel('time')
grid on
title('Ode Function','color','r')
legend('displacement/time','velocity/time')

theta1=result(:,1);

c=1;
for i=1:length(theta1)
    r=theta1(i);
    figure(2)
    %origin ponit of spring
    x0=0;
    y0=0;
    
    %maximum end position of spring during oscilation
    x1=l*sin(r);
    y1=-l*cos(r);
    
    %plotting the graph
    %spring line represent in graph
     plot([x0 x1],[y0 y1], 'linewidth',3)
     hold on
     %pendulam represent in graph
     plot(x1,y1,'.','markersize',100)
     hold on
     
     %attached roof represent in graph
     plot([-3,3],[0,0],'linewidth',8)
     hold off
     %graph anotations
     grid on
     title('Ode Function','color','r')
     legend('spring','pendulam')
     ylabel('Y AXIS->')
     xlabel('X AXIS->')
     axis([-4,4,-3,3])
     pause(0.03)
     M(c)=getframe(gcf);
     c=c+1;
end

%animation video writing 
movie(M)
videofile=VideoWriter("ode pendulam","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.