For any differential equation in normal form
\[
y' = f(x,y) \qquad \mbox{or} \qquad \frac{{\text d}y}{{\text d}x} = f(x,y)
\]
where
f(
x,y) is a well defined in some domain slope function, it is possible to obtain a graphical information about general behavior of the solution curves (call trajectories) from the differential equation itself without actual solution formula.
In the previous section, we show that
direction fields or slope fields are very important features of differential
equations because they provide a qualitative behavior of solutions. However, more precise information results from including in the plot some typical solution curves or trajectories.
A plot that shows representative sample of trajectories for a given first order differential equation is called phase portrait.
This section shows how to include sample trajectories into tangent field to obtain a phase portrait for a given differential equation.
The main matlab command for plotting direction fields is quiver, used in conjuction with meshgrid. To plot the slope field of a differential equation \( y' = f(x,y) \) on the rectangle 𝑎 ≤ x ≤ b, c ≤ y ≤ d, type the following sequence of commands:
The first command sets up a 26 by 16 grid of uniformly spaced points in the rectangular domain [-2,3] × [-1,2]. The second command evaluates the right-hand side of the differential equation
\( y' = 1- x\,y^2 \) at the grid points, thereby producing the slopes,
s =
f(
x,y), at these points. The vectors (1,
s) have the desired slopes but have different lengths at the different grid points. The
quiver command, used for plotting vector fields, requires four inputs: the array
x of
x-values, the array
y of
y-values, and arrays consisting of the two components of the direction field vectors. Since all of these arrays must have the same size, the code
ones(size(s)) conveniently creates an array of ones of the same size as
s. In the last command,
axis tight eliminates white space at the edges of the direction field. The result looks like this:
While this picture is correct, it is somewhat hard to read because of the fact that many of the vectors are quite small. You can get a better picture by rescaling the arrows so that they do not vary in magnitude---for example, by dividing each vector (1,s) by its length
\( \| (1,s)\| = \sqrt{1 + s^2} . \) In order to achieve it in matlab, we modify the preceeding sequence of commands to:
|
 
|
The fifth entry, 0.5, in the quiver command reduces the length of the vectors by half and prevents the arrow heads from swallowing up the tails of nearby vectors. The result is presented at the left.
The picture strongly suggests that some solutions approach a specific curve, but others tend to minus infinity. This specific curve is called the separatrix.
|
Unless the special purposed field program is used, plotting slope fields in matlab requiries a bit of work. The following program utilizes the built-in command quiver(x,y,dx,dy) that plots an n × n array of vectors with x- and y- components specified by the n×n matrices dx and dy, based at the xy-points in the plane whose x- and y-coordinates are specified by the n×n matrices x and y.
Another Octave code:
Another code with numerical solution
% portrait.m
[x,y] = meshgrid(-3:0.2:3,-3:0.2:3);
n = size(x);
u = ones(n);
v = 1 - x.*y.^2;
for i = 1:numel(x)
rac = 2*sqrt(u(i)^2 + v(i)^2);
u(i) = u(i)/rac;
v(i) = v(i)/rac;
end
xspan = [-2,3];
x0 = -2;
[xx,yy] = ode45(@(x,y) 1-x*y^2, xspan, x0);
figure
hold on
quiver(x,y,u,v)
plot(xx,yy,'-')
We use
Chebfun
to plot phase portrait:
??????
Error using chebop/solveivp (line 240)
The number of initial/final conditions does not match the
differential order(s) appearing in the problem.
Error in \ (line 49)
[varargout{1:nargout}] = solveivp(N, rhs, pref, varargin{:});
|
|
N = chebop(0,100);
N.op = @(t,u) diff(u, 1) - 3*(1-u^2) + t.*u;
% Then we call quiver with N as an argument
quiver(N, [-2.75 2.75 -5.5 5.5],'xpts', 40, 'ypts', 40, 'scale', .5, ...
'normalize', true)
hold on
for init = 0.2:0.4:0.2
N.lbc = init;
u = N\0;
plot(1, u)
end
title('Phase portrait of the first-order ODE','interpreter','latex', ...
'fontsize',11)
xlabel('$u$','interpreter','latex', 'fontsize',10)
ylabel('$u''$','interpreter','latex', 'fontsize',10)
hold off
|
To plot direction fileds along with some solutions, use the command
streamline
% define arrays x, y, u, and v:
[x,y] = meshgrid(0:0.1:1,0:0.1:1);
u = x;
v = -y;
% Create a quiver plot of the data. Plot streamlines that start at different points along the line y=1.
figure
quiver(x,y,u,v)
startx = 0.1:0.1:1;
starty = ones(size(startx));
streamline(x,y,u,v,startx,starty)
The best way to plot direction fields is to use existing m-files, credited to John Polking from Rice University
(http://math.rice.edu/~polking/). He is the author of two special
matlab routines:
dfield8 plots direction fields for single, first order ordinary
differential equations, and allows the user to plot solution curves;
pplane8 plots vector fields for planar autonomous systems.
It allows the user to plot solution curves in the phase plane, and it also enables a variety of time plots of the
solution. It will also find equilibrium points and plot separatrices. The latest versions of dfield8 and pplane8
m-functions are not compatible with the latest matlab version.
You can grab its modification that works from dfield.m
Since matlab has no friendly subroutine to plot direction fields
for ODEs, we present several codes that allow to plot these fields directly. Many others can found on the Internet.
The following Octave code shows how to plot the direction field for the linear differential equation \( y' = 5\,y - 3\,x . \)
The following code presents the function version to plot direction field for first order ODE y' = f(t,y)
% plot direction field for first order ODE y' = f(t,y)
function dirfield
f = @(t,y) -y^2+t % f = @(independent_var, dependent_var)
% mathematical expression y in terms of t
figure;
dirplotter(f, -1:.2:3, -2:.2:2) % dirfield(f, t_min:t_spacing:t_max, y_min:y_spacing:y_min)
% using t-values from t_min to t_max with spacing of t_spacing
% using y-values from y_min to t_max with spacing of y_spacing
end
Another function:
function dirplotter(f,tval,yval) % plots direction field using aboved defined (f,tval,yval)
[tm,ym]=meshgrid(tval,yval); % creates a grid of points stored in matrices in the (t,y)-plane
dt = tval(2) - tval(1); % determines t_spacing and y_spacing based on tval
dy = yval(2) - yval(1); % determines t_spacing and y_spacing based on yval
fv = vectorize(f); % prevents matrix algebra; if f is a function handle fv is a string
if isa(f,'function_handle') % if f is function handle
fv = eval(fv); % turn fv into a function handle from a string
end
yp=feval(fv,tm,ym); % evaluates differential equation fv from tm and ym from meshgrid
s = 1./max(1/dt,abs(yp)./dy)*0.35;
h = ishold; % define h as ishold which returns 1 if hold is on, and 0 if it is off
quiver(tval,yval,s,s.*yp,0,'.r'); hold on; % plots vectors at each point w direction based on righthand side of differential equation
quiver(tval,yval,-s,-s.*yp,0,'.r');
if h % h above defined as ishold
hold on % retain current plot
else % refers to all not h
hold off % resets axes properties to default
end
axis([tval(1)-dt/2,tval(end)+dt/2,yval(1)-dy/2,yval(end)+dy/2]) % sets the scaling for axes on current graph to ends of tval and yval
end
% Simple direction field plotter
clear;
[T Y]=meshgrid(-2:0.2:2,-2:0.2:2);
dY=cos(3*T)+Y./T;
dT=ones(size(dY));
scale=sqrt(1+dY.^2);
figure;
quiver(T, Y, dT./scale, dY./scale,.5,'Color',[1 0 0]);
xlabel('t');
ylabel('y');
title('Direction Field for dy/dt');
axis tight;
hold on;
% change color
% do multiple examples, x.^2-y.^2; use T, etc
% Simple direction field plotter
clear;
[T Y]=meshgrid(-2:0.2:2,-2:0.2:2);
dY=T.^2-Y.^2;
dT=ones(size(dY));
scale=sqrt(1+dY.^2);
figure;
quiver(T, Y, dT./scale, dY./scale,.5,'Color',[0 1 0]);
xlabel('t');
ylabel('y');
title('Direction Field for dy/dt');
axis tight;
hold on;
Example:
**DESCRIPTION OF PROBLEM GOES HERE**
This is a description for some MATLAB code. MATLAB is an extremely useful tool for many different areas in engineering, applied mathematics, computer science, biology, chemistry, and so much more. It is quite amazing at handling matrices, but has lots of competition with other programs such as Mathematica and Maple. Here is a code snippet plotting two lines (
y vs. x and
z vs. x) on the same graph. Click to view the code!
figure(1)
plot(x, y, 'Color', [1 0 0]) %blue line
hold on
plot(x, z, 'Color', [0 1 0]) %green line
-
Trefethen, L.N., Birkisson, A., Driscoll, T.A., Exploring ODEs, SIAM, Philadelphia, 2018.
-