Main Content

odeToVectorField

Reduce order of differential equations to first-order

Support for character vector or string inputs will be removed in a future release. Instead, usesymsto declare variables, and replace inputs such asodeToVectorField('D2y = x')withsyms y(x), odeToVectorField(diff(y,x,2) == x).

Description

example

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

example

[V,S] = odeToVectorField(eqn1,...,eqnN)convertseqn1,...,eqnNand returns two symbolic vectors. The first vectorVis the same as the output of the previous syntax. The second vectorSshows the substitutions made to obtainV.

Examples

collapse all

Define a second-order differential equation:

d 2 y dt 2 + y 2 t = 3 t .

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

symsy(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 first-order differential equations, whereV[i]= Y i and Y 1 = y . Here, the outputVrepresents these equations:

d Y 1 dt = Y 2

dY 2 dt = 3 t - t Y 1 2 .

For details on the relation between the input and output, seeAlgorithms.

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

symsf(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 )

The elements ofVrepresent the system of first-order differential equations, whereV[i]= Y i . The output S shows the substitutions being made, S[1] = Y 1 = f , S[2] = Y 2 =diff(f), and S[3] = Y 3 = g .

Solve a higher-order differential equation numerically by reducing the order of the equation, generating a MATLAB® function handle, and then finding the numerical solution using theode45function.

Convert the following second-order differential equation to a system of first-order differential equations by usingodeToVectorField.

d 2 y d t 2 = ( 1 - y 2 ) dy dt - y .

symsy(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 fromVby usingmatlabFunction.

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

Specify the solution interval to be[0 20]and the initial conditions to be y ( 0 ) = 2 and y ( 0 ) = 0 . Solve the system of first-order differential equations by usingode45.

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

Next, plot the solution y ( t ) within the interval t =[0 20]. Generate the values oftby usinglinspace. Evaluate the solution for y ( t ) , which is the first index inySol, by calling thedevalfunction with an index of 1. Plot the solution usingplot.

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

Figure contains an axes object. The axes object contains an object of type line.

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

symsy(x)aeqn = 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 of symbolic differential equations. Use the==operator to create an equation. Use thedifffunction to indicate differentiation. For example, representd2y(t)/dt2=ty(t)by entering the following command.

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 equationY[i]′ =V[i].

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

Tips

  • To solve the resulting system of first-order differential equations, generate a MATLAB®function handle usingmatlabFunctionwithVas an input. Then, use the generated MATLAB function handle as an input for the MATLAB numerical solverode23orode45.

  • odeToVectorFieldcan convert only quasi-linear differential equations. That is, the highest-order derivatives must appear linearly. For example,odeToVectorFieldcan converty*y″(t) = –t2because it can be rewritten asy″(t) = –t2/y. However, it cannot converty″(t)2= –t2orsin(y″(t)) = –t2.

Algorithms

To convert annth-order differential equation

a n ( t ) y ( n ) + a n 1 ( t ) y ( n 1 ) + + a 1 ( t ) y + a 0 ( t ) y + r ( t ) = 0

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

Y 1 = y Y 2 = y Y 3 = y Y n 1 = y ( n 2 ) Y n = y ( n 1 )

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

Y 1 = y = Y 2 Y 2 = y = Y 3 Y n 1 = y ( n 1 ) = Y n Y n = a n 1 ( t ) a n ( t ) Y n a n 2 ( t ) a n ( t ) Y n 1 ... a 1 ( t ) a n ( t ) Y 2 a 0 ( t ) a n ( t ) Y 1 + r ( t ) a n ( t )

odeToVectorFieldreturns the right sides of these equations as the elements of vectorVand the substitutions made as the second outputS.

Version History

Introduced in R2012a

expand all

Warns starting in R2019b