0% found this document useful (0 votes)
37 views11 pages

ODE Matlabb

The document discusses solving differential equations symbolically and numerically in MATLAB. It covers first, second, and third order ordinary differential equations as well as systems of differential equations. It also discusses approximating functions using Taylor polynomials.

Uploaded by

Shashidhara H R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views11 pages

ODE Matlabb

The document discusses solving differential equations symbolically and numerically in MATLAB. It covers first, second, and third order ordinary differential equations as well as systems of differential equations. It also discusses approximating functions using Taylor polynomials.

Uploaded by

Shashidhara H R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Working with Differential Equations, Taylor Series

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

First-Order Linear ODE


Solve this differential equation.

First, represent y by using syms to create the symbolic function y(t).

clearvars
syms y(t)

Define the equation using == and represent differentiation using the diff function.

ode = diff(y,t) == t*y

ode(t) =

Solve the equation using dsolve.

ySol(t) = dsolve(ode)

ySol(t) =

Solve Differential Equation with Condition


In the previous solution, the constant C1 appears because no condition was specified. Solve the equation with
the initial condition y(0) == 2. The dsolve function finds a value of C1 that satisfies the condition.

cond = y(0) == 2

cond =

ySol(t) = dsolve(ode,cond)

1
ySol(t) =

Second-Order ODE with Initial Conditions


Solve this second-order differential equation with two initial conditions.

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.

conds = [cond1 cond2];


ySol(x) = dsolve(ode,conds)

ySol(x) =

ySol = simplify(ySol)

ySol(x) =

Nonlinear Differential Equation with Initial Condition


Solve this nonlinear differential equation with an initial condition. The equation has multiple solutions.

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.

Third-Order ODE with Initial Conditions


Solve this third-order differential equation with three initial conditions.

syms u(x)
Du = diff(u,x);
D2u = diff(u,x,2);

Create the equation and initial conditions, and solve it.

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) =

Solve System of Differential Equations


Solve this system of linear first-order differential equations.

First, represent u and v by using syms to create the symbolic functions u(t) and v(t).

syms u(t) v(t)

3
Define the equations using == and represent differentiation using the diff function.

ode1 = diff(u) == 3*u + 4*v;


ode2 = diff(v) == -4*u + 3*v;
odes = [ode1; ode2]

odes(t) =

Solve the system using the dsolve function which returns the solutions as elements of a structure.

S = dsolve(odes)

S = struct with fields:


v: C1*cos(4*t)*exp(3*t) - C2*sin(4*t)*exp(3*t)
u: C2*cos(4*t)*exp(3*t) + C1*sin(4*t)*exp(3*t)

If dsolve cannot solve your equation, then try solving the equation numerically.

To access u(t) and v(t), index into the structure S.

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)] = dsolve(odes)

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')

Solve Differential Equations in Matrix Form


Consider this system of differential equations.

The matrix form of the system is

Let

5
The system is now .

Define these matrices and the matrix equation.

syms x(t) y(t)


A = [1 2; -1 1];
B = [1; t];
Y = [x; y];
odes = diff(Y) == A*Y + B

odes(t) =

Solve the matrix equation using dsolve. Simplify the solution by using the simplify function.

[xSol(t), ySol(t)] = dsolve(odes)

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.

C = Y(0) == [2; -1];


[xSol(t), ySol(t)] = dsolve(odes,C)

xSol(t) =

ySol(t) =

xSol(t)=simplify(xSol(t))

xSol(t) =

ySol(t)=simplify(ySol(t))

ySol(t) =

Visualize the solution using fplot.

clf
fplot(ySol)
hold on

7
fplot(xSol)
grid on
legend('ySol','xSol','Location','best')

Solve a Second-Order Differential Equation Numerically


This example shows you how to convert a second-order differential equation into a system of differential
equations that can be solved using the numerical solver ode45 of MATLAB.

A typical approach to solving higher-order ordinary differential equations is to convert them to

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.

Use odeToVectorField to rewrite this second-order differential equation.

using a change of variables. Let y(t) = Y1and = Y2 such that differentiating both equations we obtain a

system of first-order differential equations.

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'})

M = function_handle with value:


@(t,Y)[Y(2);-(Y(1).^2-1.0).*Y(2)-Y(1)]

To solve this system, call the MATLAB ode45 numerical solver using the generated MATLAB function as an
input.

sol = ode45(M,[0 20],[2 0]);

Plot the solution using linspace to generate 100 points in the interval [0,20] and deval to evaluate the solution
for each point.

fplot(@(x)deval(sol,x,1), [0, 20])

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.

The default truncation order is 6, returning a polynomial of degree ≤ 5.

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):

Technically, T is a Maclaurin series, since its expansion point is a = 0.

11

You might also like