Hi @John Terry
You can follow a few examples on this link and apply the [V, S] = odeToVectorField(eqns) command.
help odeToVectorField
odeToVectorField Converting scalar differential equations to coupled
first order systems (vector fields).
odeToVectorField will not accept equations as strings in a future release.
Use symbolic expressions or sym objects instead.
V = odeToVectorField('eqn1','eqn2', ...) accepts symbolic equations
representing ordinary differential equations given in terms of
expressions.
The differential equations must be linear in the highest
derivatives of the unknown functions involved.
The output 'V' is a SYM object containing the components of the
coupled first order system associated with 'eqn1','eqn2', ...
The output 'V' can be used as input for matlabFunction to generate
a MATLAB function. Such a function then can be used to define input
for a numerical ODE solver in MATLAB.
V = odeToVectorField('eqn1','eqn2', ...) returns a SYM object
containing the components of the coupled first order system
associated with 'eqn1','eqn2', ...
[V,Y] = odeToVectorField('eqn1','eqn2', ...) additionally returns
the SYM object 'Y' listing the substitutions that have been made
for converting the input equations 'eqn1','eqn2', ... into the
components of 'V'. In 'Y', derivatives are written as variables
whose names are formed as detailed above.
Note that the result for 'V' can directly be used as input for
matlabFunction in order to create a MATLAB function that can passed
to one of MATLAB's numerical ODE solvers. See the examples below.
Example 1:
syms x(t)
V = odeToVectorField(diff(x, 2) == -3*t) returns
V =
Y[2]
-3*t
[V,Y] = odeToVectorField(diff(x, 2) + t*diff(x) + x == -3*t)
V =
Y[2]
- 3*t - t*Y[2] - Y[1]
Y =
x
Dx
The result contained in 'V' can be used as input for
MATLABFUNCTION:
M = matlabFunction(V,'vars', {'t', 'Y'})
M = @(t,Y)[Y(2),t.*-3.0-t.*Y(2)-Y(1)]
Example 2:
You can also apply odeToVectorField on systems of scalar
differential equations:
syms f(t) g(t)
[V,Y] = odeToVectorField(diff(f, 2) == f + g, diff(g) == -f + g)
V =
Y[1] - Y[2]
Y[3]
Y[1] + Y[2]
Y =
g
f
Df
We compute the state space representation for y''=(1-y^2)*y'-y
syms y(t)
[V,Y] = odeToVectorField(diff(y, 2) == (1-y^2)*diff(y) - y)
V =
Y[2]
- (Y[1]^2 - 1)*Y[2] - Y[1]
Y =
y
Dy
use it as input for MATLABFUNCTION
M = matlabFunction(V,'vars', {'t', 'Y'})
M = @(t,Y)[Y(2);-(Y(1).^2-1.0).*Y(2)-Y(1)]
and then use MATLAB's numerical ODE solver ODE45 to create a
plot of the solution:
sol = ode45(M,[0 20],[2 0]);
x = linspace(0,20,100);
y = deval(sol,x,1);
plot(x,y);
See also DSOLVE, matlabFunction.
Documentation for odeToVectorField
doc odeToVectorField