Solving Differential Equation Using MATLAB
Solving Differential Equation Using MATLAB
syms x
f = sin(5*x);
The command
ans = 5*cos(5*x)
To take the second derivative of g
g = exp(x)*cos(x);
diff(g,2)
c = sym('5');
diff(c)
Derivatives of Expressions with
Several Variables
To differentiate an expression that contains more than one
symbolic variable, specify the variable that you want to
differentiate with respect to. The diff command then
calculates the partial derivative of the expression with respect
to that variable. For example, given the symbolic expression
syms s t
f = sin(s*t);
the command
diff(f,t)
calculates the partial derivative ∂f/∂t.
Integration in MATLAB
If f is a symbolic expression, then
int(f)
syms x
int(x^n) or int(x^n,x)
syms x
f = exp(-x^2);
int(f)
ans = (pi^(1/2)*erf(x))/2
ySol(t) = dsolve(ode)
Example
Solve the first-order differential equation
dy/dt=ay
syms y(t) a
eqn = diff(y,t) == a*y;
S = dsolve(eqn)
Solve Differential Equation
with Condition
In the previous solution, the constant C appears because
no condition was specified.
Solve the equation with the initial condition y(0) == 2.
The dsolve function finds a value of C that satisfies the
condition.
cond = y(0) == 2;
ySol(t) = dsolve(ode,cond)
Example
dy/dt+4y(t)=e−t,
y(0)=1.
syms y(t)
ode = diff(y)+4*y == exp(-t);
cond = y(0) == 1;
ySol(t) = dsolve(ode,cond)
Nonlinear Differential Equation
with Initial Condition
Solve this nonlinear differential equation with an initial
condition. The equation has multiple solutions.
(dy/dt+y)2=1,
y(0)=0.
syms y(t)
ode = (diff(y,t)+y)^2 == 1;
cond = y(0) == 0;
ySol(t) = dsolve(ode,cond)
Second-Order ODE with Initial
Conditions
Solve this second-order differential equation with two
initial conditions.
d2y/dx2=cos(2x)-y,
y(0)=1,
y′(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;
conds = [cond1 cond2];
ySol(x) = dsolve(ode,conds);
ySol = simplify(ySol)
Example
Solve the following differential Equation
2x2d2y/dx2+3xdy/dx−y=0.
syms y(x)
ode = 2*x^2*diff(y,x,2)+3*x*diff(y,x)-y == 0;
ySol(x) = dsolve(ode)
Example
syms y(t)
eqn = diff(y,t)==1+t^2+y^2+t^2*y^2;
cond=y(0)==1;
dsolve(eqn,cond)
Third-Order ODE with Initial
Conditions
Solve this third-order differential equation with three
initial conditions.
d3u/dx3=u,
u(0)=1,
u′(0)=−1,
u′′(0)=π.
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)
Solve a System of Differential
Equations
Solve this system of linear first-order differential
equations.
du/dt=3u+4v,
dv/dt=−4u+3v.
Example
First, represent u and v by using syms to create the
symbolic functions u(t) and v(t).
syms u(t) v(t)
Define the equations using == and represent
differentiation using the diff function.
uSol(t) = S.u
vSol(t) = S.v
cond1 = u(0) == 0;
cond2 = v(0) == 1;
conds = [cond1; cond2];
[uSol(t), vSol(t)] = dsolve(odes,conds)
Visualize the solution using fplot.
fplot(uSol)
hold on
fplot(vSol)
grid on
legend('uSol','vSol','Location','best')
Find Explicit and Implicit Solutions
of Differential Equation
Solve the differential equation
∂/∂t y(t)=e−y(t)+y(t).
dsolve returns an explicit solution in terms of a Lambert
W function that has a constant value.
syms y(t)
eqn = diff(y) == y+exp(-y)
sol = dsolve(eqn)
To return implicit solutions of the differential equation,
set the 'Implicit' option to true.
sol = dsolve(eqn,'Implicit',true)
Find Implicit Solution When No
Explicit Solution Is Found
If dsolve cannot find an explicit solution of a differential
equation analytically, then it returns an empty symbolic
array. You can solve the differential equation by using
MATLAB numerical solver, such as ode45.
syms y(x)
eqn = diff(y) == (x-exp(-x))/(y(x)+exp(y(x)));
S = dsolve(eqn)
S = dsolve(eqn,'Implicit',true)
Numerical Solutions of ODEs in
MATLAB Using ODE45
Sometimes it is not possible to find the exact solution of
certain differential equations, then in this case we are forced
to compute their numerical solutions.
[t,y] = ode45(odefun,tspan,y0)
y′=2t.
ode45(f,[0,2],0.5:0.2:3)
Solve ODE with Multiple Initial
Conditions
y'(x) =−2y+2 cos(t) sin(2t)
Solve the equation for each initial condition over the time interval [0,3] using ode45.
plot(t,y)