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.
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.
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.
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.
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.