Challenge

Week 3 - Solving second order ODEs

-Code to solve the ODE, the ODE to represent the motion of simple Pendulum.

-https://youtu.be/YKWldtRaDlM

clear all
close all
clc

%inputs 

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

%initial condition 
theta_0=[0;3];

%time point 
t_span= linspace (0,20,400);
%solve ODE
[t,results]= ode45(@(t,theta) ode_func(t,theta,b,g,l,m), t_span, theta_0);

plot(results(:,1),results(:,2))
hold on
xlabel('plot')
ylabel('time')

ct=1
for i=1:length(results(:,1))
    
x0= 0
y0= 0

x1= l*sind(results(i,1));
y1= -1*cosd(results(i,1));


%plotting 

plot([1 1],[0 0])
hold on
line([x0 x1],[y0 y1],'LineWidth',3)
hold on
%marker is imported to create shape
plot(x1,y1,'o','markers',25,'MarkerFaceColor','r');

axis([-0.2 0.2 -1.5 0.5])


pause(0.003)
hold off
M(ct)=getframe(gcf);
ct=ct+1;
    
end

movie(M)
videofile = VideoWriter("second_order_ode.m","Uncompressed AVI");
open(videofile)
writeVideo(videofile,M)
close(videofile)

-Function def (dtheta_dt call up):

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

-Explanation of code

%input

-inputs are Damping coeff, mass, gravity and length

%initial condition

-considering theta1 and theta2 as(0 and 5)

%time

-considering time span of 0-20 (with 500 splitup)

%ODE equ

-general ODE equation end up with (t and results)

-this has the function call array

-in the function it has the values if theta 1 and theta 2

-were the dtheta1_dt which represents the equation

d/dt [theta1 ; theta2]

-where the dtheta2_dt which represent the equation

[theta2 ; -(b/m)theta2 - g/l sintheta]

-then t_span and theta_0 are the time points and initial condition in ODE equation

now the displacement velocity graph is plotted with respective to time.

-then the for loop condition is started for increament with new x1 and y1 values with respective to arm length and angle.

-For plotting the axis with range are choosen

-and the maker command is used to create the pendulum ball with string.

- finally the movie is saved as avi file.

Errors faced

-Forget to use Hold on command

-i have mentioned marker command wrong

-Forget to close the for loop

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.