Challenge

Week 3 - Solving second order ODEs

>> sloving 2nd order differential equation;;


close all
clear all
clc

%inputs requried;;
b=0.2;
g=9.81;
l=1;
m=0.1;

%intial conditions

>> (0,5) are displacement and velocity of the equation;;

theta_0=[0;5];

>> command error by entering the (thea_0) which is an inproper code and it get rectified by entering (theta_0);;

%time points

>>t_span is an time array varies from 0,10 to 500;;

t_span =linspace(0,10,500);

>> ODE solving

>>[t,result] is an output of the function;;

>> ode45 is an syntax to the function,which is an inbuilt function of matlab;;

>>(@(t,theta)) is an function code and (t,theta) are dependent and independent varibles;;

>> (ode_function) is which defines the function and provided by input variables(t,theta,b,g,l,m);;

>> theta_0 is an intial condition of the function;;


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

plot(t,result(:,1))
hold on
plot(t,result(:,2))

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

>> CREATING A FUNCTION AND INSERTING THE FUNCTION;;

g = gravity in m/s2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient.

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

>> animation creation;;

% ode_animation
close all

%ode_main
%inputs
b=0.05;
g=9.81;
l=1;
m=0.1;
angular_displacement=0;
angular_velocity=3;
t=20;
d=10;
r=d/2;
%intial conditions
theta_0=[0;5];
%time points
t_span =linspace(0,10,500);
%sphere making
makeresampler=r;
for i=1:length(t)

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

plot(t,result(:,1))
hold on
plot(t,result(:,2))
xlabel('time')
ylabel('plot')
pause(0.05);
frame=getframe(gcf);
end
movie(frame)
videofile=VideoWriter('ode_animation.avi');
open(videofile);
writeVideo(videofile,frame);
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.