Project

Solving second order ODE

Explanation of code:-

1) first I create \"ode_func\" function file. which contain second order differential equation of pendulum simple harmonic motion which is converted into two first order differantial equation.

2) As this two differential equation stored in dtheta1_dt and dtheta2_dt variables.

3)After that I start for main program.

4)I take some value as mentioned in question b=0.05; g=9.81; l=1; m=1 .

5) I take initial value for displacement and angular velocity are 0 and 3 respectively that stored in array theta_0 .

6) after that I take 500 values of time interval between 0-20 sec.

7) after that I call the \"ode_func\" and solution for that stored into array [t,results].

8) I get value of displacement and accelration for each time value.

9) after that I plot graph for displacement and accelration with respect to time that graph is shown below.

  • code for \"ode_func\" function :-
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
  • main program:-
clear all
close all
clc

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

%initial condition
theta_0=[0;3];

%time points
t_span=linspace(0,20,500);

%solve ode
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);

plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel(\'plot\')
xlabel(\'time\')
  • Graph

Error faced during programming :-

1)error in second differential equation \'*\' miss :-

Error eliminated by taking \'*\' sign

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.