MATLAB Differential Equations

This chapter introduces Differential Equations in MATLAB, a cornerstone of modeling dynamic systems and natural processes. MATLAB offers powerful tools for solving ordinary differential equations (ODEs), partial differential equations (PDEs), and boundary value problems (BVPs), making it indispensable for engineers and scientists.

Chapter Goal

The goal of this chapter is to provide a comprehensive understanding of solving differential equations in MATLAB, focusing on:

  1. Learning how to define and solve ordinary differential equations.
  2. Exploring MATLAB’s solvers for initial value problems and boundary value problems.
  3. Understanding numerical methods for solving differential equations.
  4. Implementing best practices for efficient and accurate solutions.

Key Characteristics for MATLAB Differential Equations

  1. Wide Range of Solvers: MATLAB provides solvers for ODEs (e.g., ode45, ode23, ode15s), PDEs, and BVPs.
  2. Customizable Options: Easily modify solver parameters like tolerances and step sizes for better control.
  3. Symbolic and Numerical Integration: Solve differential equations symbolically or numerically.
  4. Integration with Plotting Tools: Visualize solutions with built-in plotting functions.
  5. Support for Stiff Equations: Dedicated solvers for stiff systems ensure robust handling.

Basic Rules for MATLAB Differential Equations

  1. Define the Function: Write the differential equation as a function or anonymous function.
  2. Select a Solver: Choose the solver that matches your equation type (e.g., stiff, non-stiff).
  3. Provide Initial Conditions: Specify initial conditions for ODEs or boundary conditions for BVPs.
  4. Specify the Time Span: Define the time range over which the solution is computed.
  5. Verify Results: Check solution accuracy using residuals or alternative solvers.

Best Practices

  1. Simplify Equations: Rewrite equations to reduce complexity for solvers.
  2. Choose Appropriate Tolerances: Use appropriate absolute and relative tolerances for numerical stability.
  3. Visualize Solutions: Plot results to understand the solution’s behavior over time or space.
  4. Use Symbolic Solvers for Validation: Compare numerical solutions with symbolic results where possible.
  5. Document Code: Clearly label functions, solvers, and parameters for maintainability.

This chapter equips you with the tools to solve a variety of differential equations using MATLAB, enabling accurate and efficient modeling of dynamic systems. Subsequent sections will delve into syntax, examples, and real-world applications.

Syntax Table

Serial No Component Syntax Example Description
1 Solve ODE (Non-Stiff) [t, y] = ode45(@func, tspan, y0); Solves non-stiff ODEs using the ode45 solver over the time span tspan with initial values y0.
2 Solve ODE (Stiff) [t, y] = ode15s(@func, tspan, y0); Solves stiff ODEs using the ode15s solver.
3 Define Anonymous Function f = @(t, y) -y + t; Defines the ODE function dy/dt = -y + t.
4 Plot Results plot(t, y); Plots the solution y versus time t.
5 Solve BVP sol = bvp4c(@func, @bc, solinit); Solves boundary value problems using the bvp4c solver.

Syntax Explanation

1. Solve ODE (Non-Stiff)

What is Solving a Non-Stiff ODE?

Non-stiff ODEs are equations that do not require small step sizes to maintain numerical stability. MATLAB’s ode45 solver is commonly used for such equations.

Syntax:

[t, y] = ode45(@func, tspan, y0);

 

Detailed Explanation:

  • @func: A function handle that defines the ODE.
  • tspan: A vector specifying the start and end times for the solution.
  • y0: Initial value of the solution at the start of tspan.

Example:

f = @(t, y) -y + t;

tspan = [0, 5];

y0 = 1;

[t, y] = ode45(f, tspan, y0);

plot(t, y);

 

Output:

A plot of the solution y over the time interval [0, 5].

2. Solve ODE (Stiff)

What is Solving a Stiff ODE?

Stiff ODEs require solvers with adaptive step sizes to handle rapid changes in the solution efficiently. MATLAB’s ode15s solver is suitable for such equations.

Syntax:

[t, y] = ode15s(@func, tspan, y0);

 

Detailed Explanation:

  • ode15s is designed for stiff problems and adjusts step sizes dynamically.

Example:

f = @(t, y) -1000*y + sin(t);

tspan = [0, 1];

y0 = 0;

[t, y] = ode15s(f, tspan, y0);

plot(t, y);

 

Output:

A plot of the solution y over the time interval [0, 1].

3. Define Anonymous Function

What is Defining an Anonymous Function?

An anonymous function is a simple, one-line function defined in MATLAB without creating a separate file.

Syntax:

f = @(t, y) -y + t;

 

Detailed Explanation:

  • f defines the ODE as dy/dt = -y + t.
  • Can be used directly in solvers like ode45 or ode15s.

Example:

f = @(t, y) -y + t;

 

Output:

No direct output; the function f is stored in memory for use in solvers.

4. Plot Results

What is Plotting Results?

Plotting visualizes the solution of the differential equation over the specified time interval.

Syntax:

plot(t, y);

 

Detailed Explanation:

  • t: Time vector from the solver output.
  • y: Solution vector from the solver output.

Example:

plot(t, y);

title(‘Solution of the ODE’);

xlabel(‘Time (t)’);

ylabel(‘Solution (y)’);

 

Output:

A labeled plot of y versus t.

5. Solve BVP

What is Solving a Boundary Value Problem?

A boundary value problem (BVP) involves solving differential equations with conditions specified at both ends of the interval.

Syntax:

sol = bvp4c(@func, @bc, solinit);

 

Detailed Explanation:

  • @func: Function defining the differential equation.
  • @bc: Function defining the boundary conditions.
  • solinit: Initial guess structure for the solution.

Example:

func = @(x, y) [y(2); -y(1)];

bc = @(ya, yb) [ya(1) – 1; yb(1)];

solinit = bvpinit(linspace(0, 1, 5), [0, 0]);

sol = bvp4c(func, bc, solinit);

plot(sol.x, sol.y(1, :));

 

Output:

A plot of the solution to the boundary value problem over the interval [0, 1].

Notes

  • Always verify solver output against analytical solutions when possible.
  • Use appropriate solvers for stiff and non-stiff problems to save computation time.

Warnings

  • Poorly defined functions or initial guesses may lead to solver errors or incorrect results.
  • Ensure compatibility between time spans, initial conditions, and solver requirements.