odeToVectorField

Reduce order of differential equations to 1

Character vector inputs will be removed in a future release. Instead, use syms to declare variables, and replace inputs such as odeToVectorField('D2y = x') with syms y(x), odeToVectorField(diff(y,x,2) == x).

Syntax

V = odeToVectorField(eqn1,...,eqnN)
[V,S] = odeToVectorField(eqn1,...,eqnN)

Description

example

V = odeToVectorField(eqn1,...,eqnN) converts higher-order differential equations eqn1,...,eqnN to a system of first-order differential equations, returned as a symbolic vector.

example

[V,S] = odeToVectorField(eqn1,...,eqnN) converts eqn1,...,eqnN and returns two symbolic vectors. The first vector V is the same as the output of the previous syntax. The second vector S shows the substitutions made to obtain V.

Examples

Convert Higher-Order Equation to First-Order System

Convert this second-order differential equation to a system of first-order differential equations.

d2ydt2+y2t=3t.

syms y(t)
eqn = diff(y,2) + y^2*t == 3*t;
V = odeToVectorField(eqn)
V =
           Y[2]
 3*t - t*Y[1]^2

The elements of V represent the system of differential equations because y = Y[1] and Y[i]′ = V[i]. Here, this particular output represents these equations:

  • diff(Y[1],t) = Y[2]

  • diff(Y[2],t) = 3*t - t*Y[1]^2

For details on the relation between the input and output, see Algorithms.

Return Substitutions Made When Reducing Order

When reducing the order of differential equations, return the substitutions that odeToVectorField makes by specifying a second output argument.

syms f(t) g(t)
eqn1 = diff(g) == g-f;
eqn2 = diff(f,2) == g+f;
eqns = [eqn1 eqn2];
[V,S] = odeToVectorField(eqns)
V =
        Y[2]
 Y[1] + Y[3]
 Y[3] - Y[1]

S =
  f
 Df
  g

From S, we have S[1] = Y[1] = f, S[2] = Y[2] = diff(f), and S[3] = Y[3] = g.

Numerically Solve Higher-Order Differential Equation

Numerically solve a higher-order differential equation by reducing the order of the equation, generating a MATLAB® function handle, and then finding the numerical solution using the ode45 function.

Convert this second-order differential equation to a system of first-order differential equations.

dy2dx2=(1y2)dydxy.

syms y(t)
eqn = diff(y,2) == (1-y^2)*diff(y)-y;
V = odeToVectorField(eqn)
V =
                       Y[2]
 - (Y[1]^2 - 1)*Y[2] - Y[1]

Generate a MATLAB function handle from V by using matlabFunction.

M = matlabFunction(V,'vars', {'t','Y'})
M = 
  function_handle with value:
    @(t,Y)[Y(2);-(Y(1).^2-1.0).*Y(2)-Y(1)]

Solve this system over the interval [0 20] with initial conditions y’(0) = 2 and y’’(0) = 0 by using the ode45 function.

interval = [0 20];
y0 = [2 0];
ySol = ode45(M,interval,y0);

Generate values of t in the interval by using the linspace function. For these values, evaluate the solution for y, which is the first index in ySol, by calling the deval function with an index of 1. Plot the solution.

tValues = linspace(0,20,100);
yValues = deval(ySol,tValues,1);
plot(tValues,yValues)

Convert Higher-Order System with Initial Condition

Convert the second-order differential equation y″(x) = x with the initial condition y(0) = a to a first-order system.

syms y(x) a
eqn = diff(y,x,2) == x;
cond = y(0) == a;
V = odeToVectorField(eqn,cond)
V =
 Y[2]
    x

Input Arguments

collapse all

Higher-order differential equations, specified as a symbolic differential equation or an array or comma-separated list of symbolic differential equations. Use the == operator to create an equation. Use the diff function to indicate differentiation. For example, represent d2y(t)/dt2 = t*y(t).

syms y(t)
eqn = diff(y,2) == t*y;

Output Arguments

collapse all

First-order differential equations, returned as a symbolic expression or a vector of symbolic expressions. Each element of this vector is the right side of the first-order differential equation Y[i]′ = V[i].

Substitutions in first-order equations, returned as a vector of symbolic expressions. The elements of the vector represent the substitutions, such that S(1) = Y[1], S(2) = Y[2],….

Tips

  • To solve the resulting system of first-order differential equations, generate a MATLAB function handle using matlabFunction with V as an input. Then, use the generated MATLAB function handle as an input for the MATLAB numerical solver ode23 or ode45.

  • odeToVectorField can convert only quasi-linear differential equations. That is, the highest-order derivatives must appear linearly. For example, odeToVectorField can convert y*y″(t) = –t2 because it can be rewritten as y″(t) = –t2/y. However, it cannot convert y″(t)2 = –t2 or sin(y″(t)) = –t2.

Algorithms

To convert an nth-order differential equation

an(t)y(n)+an1(t)y(n1)++a1(t)y+a0(t)y+r(t)=0

into a system of first-order differential equations, odetovectorfield makes these substitutions.

Y1=yY2=yY3=yYn1=y(n2)Yn=y(n1)

Using the new variables, it rewrites the equation as a system of n first-order differential equations:

Y1=y=Y2Y2=y=Y3Yn1=y(n1)=YnYn=an1(t)an(t)Ynan2(t)an(t)Yn1...a1(t)an(t)Y2a0(t)an(t)Y1+r(t)an(t)

odeToVectorField returns the right sides of these equations as the elements of vector V and the substitutions made as the second output S.

Introduced in R2012a