Challenge

Week 3 - Solving second order ODEs

2nd ORDER ODE

ODE is used to describe the transient behavior of a system. A simple example is a pendulum.

The way the pendulum moves depends on the Newtons second law. When this law is written down, we get a second order Ordinary Differential Equation that describes the position of the "ball" w.r.t time.

ODE represents the equation of motion of a simple pendulum with damping.

where,

b = damping coefficient

m = mass of the load in kg

g = gravity in m/s^2

l = length of the pendulum in meter

Solving the θ and also the rate at which the θ value changing with respect to time which is nothing but the velocity.

Program 1: Function code

%ode_fun is a function code to simple the main code

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

Program 2: Main code with function

clear all
close all
clc

%input

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

%intial condition

theta_Q = [0;3];

%time point

t_span = linspace(0,20,500);

%solve ODE

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

%plotting

figure(1)

plot(t,result(:,1));
hold on
plot(t,result(:,2));
xlabel('time');
ylabel('angular displacement and velocity');

figure(2)

for i = 1:length(t_span);
    x0 = 0;
    y0 = 0;
    ct = 0;
    x1 = 1*sind(result(i));
    y1 = -1*cosd(result(i));

    plot([x0,x1],[y0,y1],'linewidth',3);
    hold on;
    
plot(x1,y1,'Marker','o','MarkerIndices','20','MarkerEdgeColor','r','MarkerFaceColor','r')
    hold on
    axis(gca,'equal');
    grid on;
    pause(0.001);
    hold off;

    
  movie(t_span);
  videofile = VideoWriter('ODE.avi','Uncompressed AVI');
  open(videofile);
  writeVideo(videofile,ct);
  close(videofile);
end

Output ways are:

Error Faced:

  • just missed ',' in between the cordinator, so it shows error as 'Check for missing multiplication operator, missing or unbalanced delimiters'.

  • When execute above program it shows error,

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.