Solving ODEs with MATLAB
Course Title: MATLAB Programming: Applications in Engineering, Data Science, and Simulation
Section Title: Solving Differential Equations with MATLAB
Topic: Solving ordinary differential equations (ODEs) using ode45
, ode23
, etc.
Overview
Ordinary differential equations (ODEs) are a crucial part of mathematical modeling in various fields, including physics, engineering, biology, and economics. In this topic, we will explore how to solve ODEs using MATLAB's built-in ODE solvers, specifically ode45
, ode23
, and others.
Introduction to ODE Solvers
MATLAB provides a suite of ODE solvers that can be used to solve a wide range of problems. The most commonly used ODE solvers are:
ode45
: A variable-step solver that uses the Runge-Kutta method to solve non-stiff problems.ode23
: A fixed-step solver that uses the Bogacki-Shampine method to solve non-stiff problems.ode113
: A variable-step solver that uses the Adams-Bashforth-Moulton method to solve stiff problems.ode23s
: A fixed-step solver that uses the modified Rosenbrock method to solve stiff problems.
Solving a Simple ODE using ode45
Let's consider a simple example of a harmonic oscillator:
dy/dt = -y
To solve this ODE using ode45
, we need to define the ODE function and then call the ode45
function.
% Define the ODE function
function dydt = harmonic_oscillator(t, y)
dydt = -y;
end
% Define the time span and initial condition
tspan = [0 10];
y0 = 1;
% Call the ode45 function
[t, y] = ode45(@harmonic_oscillator, tspan, y0);
% Plot the solution
plot(t, y);
xlabel('Time (s)');
ylabel('Amplitude (m)');
title('Harmonic Oscillator');
This will solve the ODE and plot the solution over the specified time span.
Solving an ODE with ode23
Let's consider an example of an ODE that describes the growth of a population:
dy/dt = ry
To solve this ODE using ode23
, we need to define the ODE function and then call the ode23
function.
% Define the ODE function
function dydt = population_growth(t, y)
r = 0.1; % growth rate
dydt = r*y;
end
% Define the time span and initial condition
tspan = [0 10];
y0 = 100;
% Call the ode23 function
[t, y] = ode23(@population_growth, tspan, y0);
% Plot the solution
plot(t, y);
xlabel('Time (s)');
ylabel('Population (m)');
title('Population Growth');
This will solve the ODE and plot the solution over the specified time span.
Choosing the Right ODE Solver
The choice of ODE solver depends on the specific problem you are trying to solve. Here are some guidelines to help you choose the right solver:
ode45
: Good for most non-stiff problems. It is a variable-step solver, which means it adapts the step size to achieve a specified level of accuracy.ode23
: Good for non-stiff problems that require a fixed-step solver.ode113
: Good for stiff problems. It is a variable-step solver, which means it adapts the step size to achieve a specified level of accuracy.ode23s
: Good for stiff problems that require a fixed-step solver.
Practical Takeaways
- Use
ode45
for non-stiff problems when a variable-step solver is desired. - Use
ode23
for non-stiff problems when a fixed-step solver is desired. - Use
ode113
for stiff problems when a variable-step solver is desired. - Use
ode23s
for stiff problems when a fixed-step solver is desired. - Always check the accuracy of the solution by comparing it to an analytical solution or by using a different solver.
Further Reading
For more information on ODE solvers in MATLAB, see the following resources:
What's Next?
In the next topic, we will explore how to solve systems of ODEs and initial value problems (IVPs).
Exercise
Try solving the following ODE using ode45
:
dy/dt = y^2
Define the ODE function and call the ode45
function to solve the ODE. Plot the solution over a suitable time span.
Comment or Ask for Help
If you have any questions or need help with the material, please leave a comment below.
Images

Comments