ODE Matlabb
ODE Matlabb
Table of Contents
First-Order Linear ODE.............................................................................................................................................1
Solve Differential Equation with Condition................................................................................................................1
Second-Order ODE with Initial Conditions................................................................................................................2
Nonlinear Differential Equation with Initial Condition................................................................................................ 2
Third-Order ODE with Initial Conditions....................................................................................................................3
Solve System of Differential Equations.....................................................................................................................3
Solve Differential Equations in Matrix Form..............................................................................................................5
Solve a Second-Order Differential Equation Numerically......................................................................................... 8
Approximating Functions using Taylor Polynomials................................................................................................10
clearvars
syms y(t)
Define the equation using == and represent differentiation using the diff function.
ode(t) =
ySol(t) = dsolve(ode)
ySol(t) =
cond = y(0) == 2
cond =
ySol(t) = dsolve(ode,cond)
1
ySol(t) =
Define the equation and conditions. The second initial condition involves the first derivative of y. Represent the
derivative by creating the symbolic function Dy = diff(y) and then define the condition using Dy(0)==0.
syms y(x)
Dy = diff(y);
ode = diff(y,x,2) == cos(2*x)-y;
cond1 = y(0) == 1;
cond2 = Dy(0) == 0;
Solve ode for y. Simplify the solution using the simplify function.
ySol(x) =
ySol = simplify(ySol)
ySol(x) =
syms y(t)
ode = (diff(y,t)+y)^2 == 1;
2
ic = y(0) == 0;
ySol(t) = dsolve(ode,ic)
ySol(t) =
If dsolve cannot solve your equation, then try solving the equation numerically.
syms u(x)
Du = diff(u,x);
D2u = diff(u,x,2);
ode = diff(u,x,3) == u;
cond1 = u(0) == 1;
cond2 = Du(0) == -1;
cond3 = D2u(0) == pi;
conds = [cond1 cond2 cond3];
uSol(x) = dsolve(ode,conds)
uSol(x) =
First, represent u and v by using syms to create the symbolic functions u(t) and v(t).
3
Define the equations using == and represent differentiation using the diff function.
odes(t) =
Solve the system using the dsolve function which returns the solutions as elements of a structure.
S = dsolve(odes)
If dsolve cannot solve your equation, then try solving the equation numerically.
uSol(t) = S.u
uSol(t) =
vSol(t) = S.v
vSol(t) =
Alternatively, store u(t) and v(t) directly by providing multiple output arguments.
uSol(t) =
vSol(t) =
The constants C1 and C2 appear because no conditions are specified. Solve the system with the initial
conditions u(0) == 0 and v(0) == 1. The dsolve function finds values for the constants that satisfy these
conditions.
cond1 = u(0) == 0;
cond2 = v(0) == 1;
conds = [cond1; cond2];
[uSol(t), vSol(t)] = dsolve(odes,conds)
uSol(t) =
vSol(t) =
4
Visualize the solution using fplot.
fplot(uSol)
hold on
fplot(vSol)
grid on
legend('uSol','vSol','Location','southwest')
Let
5
The system is now .
odes(t) =
Solve the matrix equation using dsolve. Simplify the solution by using the simplify function.
xSol(t) =
ySol(t) =
xSol(t) = simplify(xSol(t))
xSol(t) =
ySol(t) = simplify(ySol(t))
ySol(t) =
6
The constants C1 and C2 appear because no conditions are specified.
Solve the system with the initial conditions u(0) = 2 and v(0) = –1. When specifying equations in matrix form,
you must specify initial conditions in matrix form too. dsolve finds values for the constants that satisfy these
conditions.
xSol(t) =
ySol(t) =
xSol(t)=simplify(xSol(t))
xSol(t) =
ySol(t)=simplify(ySol(t))
ySol(t) =
clf
fplot(ySol)
hold on
7
fplot(xSol)
grid on
legend('ySol','xSol','Location','best')
systems of first-order differential equations, and then solve those systems. The example uses Symbolic Math
Toolbox™ to convert a second-order ODE to a system of first-order ODEs. Then it uses the MATLAB solver
ode45 to solve the system.
using a change of variables. Let y(t) = Y1and = Y2 such that differentiating both equations we obtain a
8
syms y(t)
[V] = odeToVectorField(diff(y, 2) == (1 - y^2)*diff(y) - y)
V =
The MATLAB ODE solvers do not accept symbolic expressions as an input. Therefore, before you can use a
MATLAB ODE solver to solve the system, you must convert that system to a MATLAB function. Generate a
MATLAB function from this system of first-order differential equations using matlabFunction with V as an input.
M = matlabFunction(V,'vars', {'t','Y'})
To solve this system, call the MATLAB ode45 numerical solver using the generated MATLAB function as an
input.
Plot the solution using linspace to generate 100 points in the interval [0,20] and deval to evaluate the solution
for each point.
9
Approximating Functions using Taylor Polynomials
A Taylor polynomial approximates a function near a known value. You can evaluate the polynomial at nearby
points to get an approximate value for the function.
For a function f(t), you can use the taylor function to find the Taylor polynomial for f near the point t = 3.
tpoly = taylor(f,t,3)
The function g is defined below. Find the Taylor polynomial for g near the point x = 1. Store the result as p.
syms x
g(x) = exp(x-1)
g(x) =
The taylor function returned a 5th degree polynomial, but you can return a higher or lower degree polynomial
by specifying the truncation order.
tpoly = taylor(f,t,3,'Order',n)
The truncation order n is one more than the highest polynomial degree you want. So if you want a polynomial of
degree ≤ 7, use a truncation order of 8.
Find the Taylor polynomial for g near the point x = 1 with a truncation order of 10. Store the result as p9.
10
tp5=taylor(g,x,1,'Order',6)
tp5(x) =
p9=taylor(g,x,1,'Order',10)
p9(x) =
syms x
f = 1/(5 + 4*cos(x));
T = taylor(f, 'Order', 8)
T =
The output T is a series of all the terms up to, but not including, order eight in the Taylor series for f(x):
11