Matlab

How to Use ode45 in MATLAB for Second Order ODE

Differential equations are the building blocks of any physical phenomenon used in engineering applications. They include two types: ordinary differential equations (ODEs) as well as partial differential equations (PDEs). The solutions of these equations can be found analytically as well as numerically. There are limited methods for solving a differential equation analytically. So, most of the time we opt for numerical methods to solve them.

The numerical approach to solving a differential equation is not an easy task but MATLAB makes it easy by introducing built-in functions like ode23, ode45, and many others.

The main focus of this blog is to explain how to solve 2nd-order ordinary differential equations in MATLAB using the ode45() function.

What is an Ordinary Differential Equation?

An ordinary differential equation (ODE) has the dependent variable, an independent variable, as well as the dependent variable’s derivatives with respect to the independent variable. The form of this equation is given below:

Here y behaves the dependent variable and x behaves like the independent variable. The f(x,y) is a differential equation that represents the relation between the dependent variable y and the independent variable x.

How to Find Solution of a 2nd Order Ordinary Differential Equation Using ode45() Function?

An ordinary differential equation having 2nd-order derivatives is called the 2nd-order ordinary differential equation. These equations can be solved analytically as well as numerically. However, to get an approximation we have to use numerical methods.

MATLAB includes built-in functions to solve 2nd-order ordinary differential equations numerically. One such function is ode45(). This function works the same as the Runge-Kutta method, an accurate numerical method used for solving 2nd-order ODEs.

Syntax

The ode45() function uses a simple syntax in MATLAB.

[t,y] = ode45(odefun,tspan,y0)

Here,

The function [t,y] = ode45(odefun,tspan,y0) calculates and returns the numerical solutions of the given ODE where tspan integrates the given function from t0 to tf on the given initial condition y0.

Return Value

The return value of the ode45() function is a two-dimensional vector, where the first dimension of the vector contains the time values, while the second dimension contains the solution values.

Examples

Consider some examples to learn how to use the ode45() function to find numerical solutions of 2nd-order ODE in MATLAB.

Example 1: Use ode45 in MATLAB to Solve 2nd order ODE

In this example, first, we create a user-defined function vdp1 that includes the 2nd order ODE. It takes two arguments, time t, and solution y, and returns the derivative of y with respect to t.

function dydt = vdp1(t,y)
dydt = [y(2); (y(1)^2)*y(2)-y(1)]

After that, we use the ode45() function to evaluate the given ODE on the specified points.

[t,y] = ode45(@vdp1,[0:0.5:1],[-1;0])

Example 2: Use ode45 in MATLAB to Solve 2nd order ODE and Plot the Solution

This example defines the 2nd-order ODE. After that, it uses the ode45() function to evaluate the given ODE on the specified points and plots the solutions.

f = @(t,y) [(y(2)^2)*y(1); y(1)];
t0 = [1:0.5:2]; y0 = [1;0];
[ts,ys] = ode45(f,t0,y0)
plot(ts,ys(:,1),'b')
title('Solution y(t) of IVP')
xlabel('t');
ylabel('y(t)')
grid on

Conclusion

Differential equations are classified into two types: partial differential equations (PDEs) and ordinary differential equations (ODEs). These are used to solved analytical or numerical methods. Numerical methods are more accurate than analytical methods. MATLAB provides built-in functions to solve 2nd order ODEs and ode45() function in among those functions that uses the Runge-Kutta (RK) method to solve these equations. This tutorial has discussed the approach to solve 2nd order ODEs using the ode45() function in MATLAB.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.