MATLAB Answers

Is there more information available on solving Ordinary Differential Equations in MATLAB?

29 views (last 30 days)
For example, how do I reduce the order of a differential equation? Are there more examples of how to solve differential equations in MATLAB? What options can I specify when using the ODE solvers?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 22 Jan 2010
There is a Technical Note on our web site answering these questions and more.
Specifically, Section 4 of the attached Tech Note 1510 describes how to reduce the order of a Differential Equation in detail.
Another example is shown below.
ODE Reduction Example
***************************
Ths is an example of how to reduce a second-order differential equation into two first-order equations.
This is acheived through:
1. Introducing a new variable that is equal to the first derivative of the free variable in the second-order equation. In the case below, (z = y'). This introduces a new differential equation (y' = z).
2. To finish the reduction, notice that taking the derivatives of both sides of ( y' = z ) gives ( y'' = z' ).
3. Next, substitute z' for y'' in your second-order equation, and now you have a first-order equation.
As an example, start with the differential equations
x' = -y * exp(-t/5) + y' * exp(-t/5) + 1
and
y'' = -2*sin(t)
To reduce the order of the second equation, let z = y'. This implies that z' = y'', and that y' = z. Making this substitution for y'' in the second equation gives
x' = -y * exp(-t/5) + y' * exp(-t/5) + 1
and
z' = -2*sin(t)
Combine this with y' = z, and you have three first-order differential equations.
x' = -y * exp(-t/5) + y' * exp(-t/5) + 1
y' = z
and
z' = -2*sin(t)
MATLAB wants all the derivatives to be on the left hand side of the equations. For this example, this can be done by substituting z for y' in the first equation. This gives
x' = -y * exp(-t/5) + z * exp(-t/5) + 1
y' = z
and
z' = -2*sin(t)
MATLAB uses one vector as the variable list. Let x(1) = x, x(2) = y and x(3) = z. MATLAB also wants a function that has one return argument that is the derivative of the second input, i.e.,
xprime = fun( x, t )
The MATLAB code for this function would be
function xprime = fun(t, x)
xprime = [ -x(2) * exp(-t/5) + x(3) * exp(-t/5) + 1;
x(3);
-2*sin(t)];
To run this in MATLAB, save the above code in a file called odetest.m. Then, refer to this function as the first input argument to the ODE23 function using the command:
[t,x] = ode23(@odetest,[t0 tf],x0);
The variable "t0" is the start time, "tf" is the stop time (solve the diff_eq from "t0" to "tf"), "x0" is the initial condition of the state vector "x" at time "t0".
x0(1) = x at t0, x0(2) = y at t0 and x0(3) = z at t0
To run this, use the MATLAB commands:
x0 = [1 -1 3];
t0 = 5;
tf = 20;
[t, x] = ode23(@odetest, [t0 tf], x0);
To look at the data use
plot(t, x)

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!