Matlab Notes For Differential Equations: Lia Vas
Matlab Notes For Differential Equations: Lia Vas
Lia Vas
Content
1. M-files
2. Basics of first order differential equations
3. Direction field
4. Numerical solutions using ode45
5. Euler method
Practice problems 1
6. Second and higher order differential equations
Practice problems 2
7. Systems of differential equations
1. M-files
Suppose that you want to perform the same operation many times for different input values.
Matlab allows you to create a function or a script that you can execute repeatedly with
different input values (i.e. to do programming). Such function or a script is called an M-file. An
M-file is an ordinary text file containing Matlab commands. You can create them using any
text editor that can save files as plain (ASCII) text. You can also create an M-file by using
“File” menu and choosing “New M-file” option from your Matlab command window. An
important fact to keep in mind when using M-files is that an M-file can be executed from the
command window just if the "Current Directory" on the top of the command window is set to
be the same directory where the M-file is saved.
Example 1.1. Suppose that you want to solve the equation ax² + bx + c = 0 using the
quadratic formula. We can create a function which will have as input values of a, b and c and
which will give the solution(s) of the quadratic equation. We can enter the following in an M-
file.
function [x1 x2] = quadratic1(a, b, c)
x1 = (-b+sqrt(b^2-4*a*c))/(2*a);
x2 = (-b-sqrt(b^2-4*a*c))/(2*a);
To execute the M-file and find solutions of x² -3x + 2 = 0, you can use
>> [x1, x2] = quadratic(1, -3, 2)
The output is x1 = 2 x2 = 1
Note that the semi-colon symbol after the commands suppresses display of the results of
these commands. The values of x1 and x2 will still be displayed since we listed these
variables in the brackets of the function.
-1-
x1 = (-b+sqrt(b^2-4*a*c))/(2*a)
x2 = (-b-sqrt(b^2-4*a*c))/(2*a)
Here we do not need to list the variables in the first line since we did not use the semi-colon
symbols when computing the values of x1 and x2. Also, you do not need to list x1 and x2 in
the first line since they will be displayed when 2nd and 3rd lines are executed. Also, you can
execute this M-file to solve x² -3x + 2 = 0 simply as quadratic(1, -3, 2) instead of [x1, x2] =
quadratic(1, -3, 2) The output is the same as for the first version.
>> quadratic(1, -3, 2)
This means that you want to solve the equation. Matlab gives you the answers:
x1 = 2 x2 = 1
These M-files produce the complex values if the solutions of a quadratic equation are
complex numbers. For example, to solve x² + 4 = 0, we can use
>> quadratic(1, 0, 4)
x1 = 0 + 2.0000i x2 = 0 - 2.0000i
Example 1.2. Suppose that we need to write a program that calculates the Cartesian
coordinates (x,y) of a point given by polar coordinates (r,θ).
One possible solution is the following M-file.
function [x, y] = polar(r, theta)
x = r*cos(theta);
y = r*sin(theta);
For example, to calculate (x,y) coordinates of for r=3 and θ = π , we can use the command
[x,y]=polar(3, pi) and get the answer
x = -3 y = 3.6739e-016
Note that y is very close to 0. The inaccuracy comes from the fact that using pi for π in Matlab
gives an approximation of π not the exact value. The exact representation of π is obtained by
sym('pi'). The command [x,y]=polar(3, sym('pi')) gives you the expected answer
x = -3 y = 0.
To find symbolic solution of a differential equation, you can use the command dsolve. To use
dsolve, the derivative of the function y is represented by Dy. The command has the following
form:
dsolve('equation', 'independent variable')
If we have the initial condition, we can get the particular solution on the following way:
Example 2.1. Consider the equation x y' - y = 1. a) Find the general solution. b) Find the
particular solution corresponding to the initial condition y(1)=5 and graph it.
a) You can find the general solution by using: >> dsolve('x*Dy-y=1', 'x')
You obtain ans = -1+x*C1 This means that the solution is any function of the form y = -1+
-2-
cx, where c is any constant.
b) To find the solution of x y' - y = 1 with the initial condition y(1)=5, we can use:
>> dsolve('x*Dy-y=1', 'y(1)=5', 'x') ans = -1+6*x
To graph this solution, we can use: >> ezplot(ans)
Example 2.2. Graph the solutions of the equation y' = x+y for y(0) taking integer values
between -2 and 4.
First we can find the solution with the initial condition of the form y(0)=c.
>> s = dsolve('Dy = x+y', 'y(0)=c', 'x')
s = -x-1+exp(x)*(1+c)
Then, we can use the following M-file to produce the graph of the required particular
solutions.
function[ ]=many_solutions(s)
for cval=-2:1:4 (this means that the variable cval is taking values starting at -2 and
ending at 4 with step size of 1. Change this line if you need to use
different values of c)
hold on
ezplot(subs(s, 'c', cval)) (this means that the value of c in s is substituted by values of
cval. Add the domain of the solutions to this command as
explained in Review of Matlab if necessary)
hold off
end
-3-
First we solve the equation naming the general solution s.
>> s = dsolve('Dy = (y-2)*(100-y)', 'y(0)=c', 'x')
Since this is an autonomous equation with the equilibrium solutions at y=2 and y=100, in
order to get a telling graph, we can graph a few solutions with initial conditions less than 2, a
few between 2 and 100 and a few above 100. We can use the same M-file as in the previous
example, but with a few modification. For one, we need to modify the line that specifies the
values of initial condition c. For example we can take the values of c to be 0, 10, 20, ... , 150.
So, we start at 0 and end with 150 making the step size of 10. In Matlab, we can represent
this by 0:10:150. Thus, substitute the line cval=-2:1:4 with cval=0:10:150 and use the M-file
many_solutions again. Secondly, to see the graphs for t values larger than 6, [0, 15] for
example, modify the line with ezplot to be ezplot(subs(s, 'c', cval), [0 15]). Finally, a nice
window for this graph can be axis([0 15 0 150]) for example.
3. Direction Fields
-4-
4. Numerical solutions using ode45
Many differential equations cannot be solved explicitly in terms of elementary functions. for
those equations, approximate solutions can be obtained using numerical methods.
Approximate solutions can be found by using the command ode45. We can illustrate the use
of this command on the following example.
2
Example 4.1. Consider the initial value problem y' = e−x , y(0)=1. Graph the solution on the
interval [0, 2] and display the y-values for x=0, 0.5, 1, 1.5 and 2.
The command ode45 requires that the equation is in the form y'=f(x,y). Thus, if the equation
is not given in this form, you have to solve for y' first. Then you want to represent the function
on the right side of the equation as a vector function. You can use the command:
>> f=inline(vectorize('exp(-x^2)'),'x','y');
If you want the graph without the circles around the points at which ode45 calculates the
solution, you can do the following:
>> [x, y] = ode45(f, [0 2], 1);
>> plot(x, y)
If we do not want to display the entire list of (x,y)-values but just the y-values for x=0, 0.5, 1,
1.5 and 2, we can use the command:
>> [x,y]=ode45(f, [0:.5:2], 1);
>> [x,y]
The output will be:
x= y=
0 1.0000
0.5000 1.4613
1.0000 1.7468
-5-
1.5000 1.8562
2.0000 1.8821
5. Euler Method
The following M-file calculates the numerical values of the solution of an initial value problem
using the Euler method. It approximates the solution of the initial value problem y' = f(x,y),
y(x0) = y0 on the interval [x0, xn] using n steps using the following formulas:
for all i=0,1...,n-1. The input is the inline function f, x0, y0, xn and n. The output is the list of x
and y values of the approximate solution.
-6-
>> [x,y]
To graph this list, use plot(x,y).
The list consists of 21 (x,y) points. The list starts with the initial condition (x,y)=(0,1). The x-
values are all h=(2-0)/20=0.1 units apart. The last x-value is 2 and the corresponding y-value
is 1.9311. From this list you can see the y-values corresponding to x=1 and x=2.
x y
1 1.7778
2 1.9311
Alternatively, if you need to display a specific (x,y)-value, you need to determine the i-value
that corresponds to this point (i.e. you need to count the steps performed until the point has
been calculated). Then you can display the point by typing x(i) and y(i).
For example, the point with x=1 is the 11th point calculated (Note: not 10th point - recall that
x=0 corresponds to first point calculated, not the zeroth point calculated). So, to display this x-
value you can type
>> x(11) and obtain the answer ans = 1.000
To display the corresponding y-value, you can type
>> y(11) and obtain the answer ans = 1.7778
Similarly, the point with x=2 is the 21th point calculated. You can display the x and y values as
follows.
>> x(21) ans = 2.000
>> y(21) ans = 1.9311
Practice problems
2. Graph the solutions of the differential equation y'=0.1y(1-y) for the y-values of the initial
condition y(0) taking values 0.1, 0.3, 0.5 and 0.7.
4. Consider the equation y' = (y+1)(3-y)²(5-y). Using ode45, find the value of the solution
with the initial condition y(0)=4 at x=5. Graph the solution with initial condition y(0)=4
for x-values in [0, 5].
5. Consider the equation y' = (y-1)(5-y). Using the M-file euler, find the value of the
solution with the initial condition y(0)=2 at x=3 for step size of 0.25. Graph the solution
-7-
with initial condition y(0)=2 for x-values in [0, 3].
Solutions.
1. a) General solution: dsolve('Dy-2*y=6*x', 'x') ans= (-6x-3)/2+C1*exp(2x)
b) Particular solution: dsolve('Dy-2*y=6*x', 'y(0)=3', 'x') ans= (-6x-3+9*exp(2x))/2
c) syms x ezplot((-6x-3+9*exp(2x))/2, [0 2])
To find the value at 2: f=inline((-6x-
3+9*exp(2x))/2, x) f(2)
ans=238.19
2. Modify the M-file many_solutions so that
the second line is cval=0.1:0.2:0.7 (this means
that cval is taking values starting at 0.1 and
ending at 0.7 that are 0.2 away from each
other). Thus cval= 0.1, 0.3, 0.5 and 0.7. Also, to
see better the limiting behavior of the solutions,
you may want to graph the solutions on domain
that includes large values of x. To do this
modify the line with ezplot command to
ezplot(subs(s, 'c', cval), [0 100]) for example.
Then execute s = dsolve('Dy=0.1*y*(1-
y)','y(0)=c', 'x') and many_solutions(s) and
obtain the following graph.
3. a) Set the right side of the equation equal to zero. Obtain the
equilibrium solutions y=-1, y=3 and y=5. Analyze the sign. Obtain
that y=-1 is unstable, y=3 is semistable and y=5 is stable.
b) First inline the function f=inline(vectorize('(y+1)*(3-y)^2*(5-
y)'), 'x', 'y')
4.2
-8-
4
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
Execute the M-file euler by [x,y]=euler(f, 0, 2, 3, 12)
To display the answers in two columns, one corresponding to x and the other to y-values use
simply [x,y]. The list starts by x=0 and y=2. The list ends with x=3 and y=5.000. Graph the list
by using plot(x,y). The graph should look similar to the graph from the previous problem.
Second order linear equations can be solved similarly as the first order differential
equations: analytically using dsolve and numerically using ode45. For command dsolve,
recall that we represent the first derivative of the function y with Dy. The second derivative of
y is represented with D2y.
b) For the particular solution with the initial conditions y(0) = 1, y'(0)=-1, use:
>> dsolve('D2y-3*Dy+2*y=sin(x)', 'y(0)=1', 'Dy(0)=-1', 'x')
ans = 3/10*cos(x)+1/10*sin(x)+5/2*exp(x)-9/5*exp(2*x).
For equations that can not be solved in terms of elementary functions, we use
numerical methods. For ode45, the second order differential equation must be converted to
a system of two first order equations using the substitution
(S) y=y1 and y'=y2.
The first equation of the new system reflects the relation between the two new functions: the
second one is the derivative of the first. So, the first equation is
(1) y1'=y2 .
The second equation is obtained by applying the substitution to the original differential
equation and solving it for y''=y2'. For example, the equation y''-3y'+2y = sin x from the
previous example with substitution (S) becomes y2'-3y2 +2 y1 = sin x. Solving for y2' produces
the second equation of the system to be y2'=sin x+3y2-2y1 . Thus, the system is
(1) y1'=y2 and (2) y2'=sin x+3y2-2y1
We illustrate the use of ode45 command for solving second order equations on the following
example.
-9-
First, you need to convert the given second order differential equation into a system of two
first order equations using the substitution (S) y=y1 and y'=y2. The first equation of the new
system is (1) y1'=y2 . The second equation is obtained by using the substitution for the given
equation y'' + xy' + y = 0 to obtain y2' + xy2+ y1 =0 and then solving for y2' to get (2) y2'=-xy2- y1.
Thus, the newly obtained system is
(1) y1'=y2 and (2) y2'=-x y2- y1 .
In Matlab, the two new functions can be denoted by y(1) and y(2). The solution y will be
represented as vector y=[y(1); y(2)]. Keep in mind that y(1) corresponds to the solution y of
the original equation. The function y(2) corresponds to the derivative y'. This problem is not
asking for the derivative y' so you can consider y(2) to be a byproduct.
Inline the right side of the two equations as a vector function f that depends on independent
variable x and dependent variable y.
f=inline('[y(2); -x*y(2)-y(1)]','x','y');
The first entry of f is the right side of the first equation and the second entry of f is the right
side of the second equation.
- 10 -
each other. [1;0] indicates the initial values, just as in part (a). The vector y in the output will
consist of two columns. The first y(1) consists of the values of the solution y at x-values
between 0 and 5 and the second y(2) consists of the values of the derivative y' at these
points. If you want the x and y values to be displayed in columns next to each other (so that it
is easy to see the y-value for a corresponding x-value), you can use [x,y]. Obtain the list
ans =
0 1.0000 0
1.0000 0.6065 -0.6065
2.0000 0.1354 -0.2707
3.0000 0.0111 -0.0333
4.0000 0.0003 -0.0013
5.0000 0.0000 -0.0000
Here the three columns represent x, y and y' values respectively.
Practice problems 2
1. a) Find the general solution of the equation y''-4 y'+4 y= e x +x². b) Find particular
solution of the initial value problem with y(0)=8, y'(0)=3.
2. Using ode45 graph the solution of y'' + x² y' + y = cos 2x, y(0)=1, y'(0)=-1 for t in [0, 4].
Solutions
1. a) dsolve('D2y-4*Dy+4*y=exp(x)+x^2', 'x')
ans = exp(x)+1/4*x^2+1/2*x+3/8+C1*exp(2*x)+C2*exp(2*x)*x
b) dsolve('D2y-4*Dy+4*y=exp(x)+x^2','y(0)=8', 'Dy(0)=3', 'x')
ans = exp(x)+1/4*x^2+1/2*x+3/8+53/8*exp(2*x)-47/4*exp(2*x)*x
2. Convert to a system using y=y1 and y'=y2. The first equation of the system is y1'=y2 and the
second is obtained from y'' + x²y' + y = cos 2x → y2'+ x²y2 + y1 = cos 2x → y2'=cos 2x - x² y2 -
y1. So the system is
(1) y1'=y2 and (2) y2'=cos 2x - x² y2 - y1
Inline the right side of the two equations as a vector function f by
f=inline('[y(2); cos(2*x)-x^2*y(2)-y(1)]','x','y').
To graph the solution on interval [0, 4], you can use ode45(f, [0, 4], [1;-1]). The values [1;-1]
correspond to the y and y' values from the initial conditions.
Symbolic Solutions. You can find the symbolic solutions of a system of differential equations
by using the command dsolve.
- 11 -
d) Plot sufficiently many solutions in the phase plane to determine the type of the equilibrium
point (0,0).
Numeric Solutions. Finding symbolic solutions might be very limiting because many systems
of differential equations cannot be solved explicitly in terms of elementary functions. For those
equations or systems of equations, numerical methods are used in order to get the
approximate solution. To find numeric solutions, you can use the command ode45. In order to
use it, the system needs to be in the form x'=f(x,y,t) and y'=g(x,y,t) and the right sides of the
equations should be represented as a vector function using inline first. The function x can be
represented as y(1) and the function y as y(2). The first entry of the inlined function f is the
right side of the first equation and the second entry of f is the right side of the second
equation.
- 12 -
b) Graph the solutions on interval [0,20].
c) Graph the solution from b) in the phase plane.
d) Plot sufficiently many solutions of this system (without the given initial conditions) in the
phase plane to determine the type of the equilibrium point (1,1).
First, inline the right side of equation as a function of independent variable t. The unknown
functions x and y are represented by y(1) and y(2) respectively.
f = inline('[2*y(1)-y(1)^2-y(1)*y(2); y(1)*y(2)-y(2)]','t','y');
You may need to display just the first or just the second
function. In this case, the command
[t,y]=ode45(f,[0 20],[1;2]); followed by plot(t,y(:,1))
plots just x-values (third graph displayed).
- 13 -
Similarly, the command
[t,y]=ode45(f,[0 20],[1;2]); followed by plot(t,y(:,2)) plots just y-values.
c) The command
[t,y] = ode45(f,[0,20],[1;2]); followed by
plot(y(:,1),y(:,2))
plots the solution in the phase plane: it plots (x,y) as a
parametric curve of parameter t in (x,y)-plane.
d) The following M-file can be used to graph the trajectories in the phase plane for x(0) and
y(0) taking integer initial values between 0 and 5.
- 14 -