## Math 315 Computer Tools

There are some Java Applets that allow interactive work with differential equations at the textbook website. For example, the applet program "DFIELD" plots direction fields and selected solution curves.

The computer tools Maple and Matlab are available online from the Math Department MyMath Math Portal; after login, use "Goto" to follow the "Software" link and select Matlab or Maple. Online information about Matlab is given at the Matlab information website. Some tutorial information about Maple is given at this Maple tutorial website, with more details given at this second Maple tutorial website. Here are some Maple and Matlab example commands for working with differential equations.

• GRAPH EXAMPLES: to plot 2exp(-t/2)cos(2t-3/2), 2exp(-t/2), -2exp(-t/2), on the same graph use
• in MATLAB:
t = [0:.1:6];
plot( t,2*exp(-t/2).*cos(2*t-3/2), t,2*exp(-t/2), t,-2*exp(-t/2) )
• in MAPLE:
g1 := plot( 2*exp(-t/2)*cos(2*t-3/2), t=0..6 ):
g2 := plot( 2*exp(-t/2), t=0..6, color=green ):
g3 := plot(-2*exp(-t/2), t=0..6, color=blue ):
with(plots): display(g1,g2,g3);
• FIRST ORDER EQUATIONS: Assume you want to solve y' = exp(-t) - 2y, given y(0) = 1.
• MAPLE
• To plot a direction field use:
with(DEtools):
DEplot({diff(y(t),t) = exp(-t)-2*y(t)},y(t),t=-2..3,y =-1..2);
• To plot a direction field that includes a solution plot use:
with(DEtools):
DEplot({diff(y(t),t) = exp(-t)- 2*y(t)},y(t),t=0..3,[y(0)=1]);
• To plot a direction field that includes solution plots for 2 initial values use:
with(DEtools):
DEplot({diff(y(t),t) = exp(-t)-2*y(t)},y(t),t=0..3,[[y(0)=1],[y(0)=2]]);
• To solve the equation symbolically use:
dsolve( { diff(y(t),t) = exp(-t) - 2*y(t), y(0) = 1 }, y(t) );
• To solve the equation numerically and plot a solution use:
sol:=dsolve( { diff(y(t),t) = exp(-t) - 2*y(t), y(0) = 1 },y(t),numeric);
with(plots): odeplot(sol,[t,y(t)],0..4);
• MATLAB
• To solve the equation numerically and plot the solution use:
ode45(@(t,y)exp(-t)-2.*y, [0, 3], 1 )
• To solve the equation numerically for 3 different initial values (y(0)=1,2,3) and plot the solutions use:
ode45(@(t,y)exp(-t)-2.*y, [0, 3], [1,2,3] )
• To plot a direction field use:
[t,y] = meshgrid( [-2:.2:3], [-1:.2:2] ); % set T and Y limits
f = exp(-t) - 2*y; % this line defines the ODE
L = sqrt(1+f.^2); quiver( t, y, 1./L, f./L, 0.5 );
axis equal tight, grid on, xlabel('t'), ylabel('y')
title('y'' = exp(-t) - 2*y(t) Direction Field')
• To solve the equation symbolically and plot the solution use:
sol = dsolve('Dy = exp(-t) - 2*y, y(0) = 1','t'); pretty(sol)
ezplot( sol, [-2, 3] )
• SECOND ORDER EQUATIONS:
Assume you want to solve y''+2y'+y = 3cos(2t), given y(0) = 1, y'(0)=1.
• To solve the equation symbolically in MAPLE use:
dsolve({diff(y(t),t\$2)+2*diff(y(t),t)+y(t)=3*cos(2*t),y(0)=1,D(y)(0)=1},y(t));
• To solve the equation numerically in MATLAB, switch to a system of two first order eqautions with
u_1 = y, u_2 = y', so that u'_1= u_2, u'_2 = 3cos(2t)-u_1 - 2u_2; then use:
ode45(@(t,u)[ u(2); 3*cos(2*t)-u(1)-2*u(2) ], [0, 6], [ 1; 1 ] );
to compute and plot y and y' on the same graph.
To solve the equation numerically in MATLAB with several
starting points (e.g. (-1,1), (0,1), (1,1), (2,1) ) points
and plot all solutions on the same graph use
for s = -1:2
[t,u] = ode45(@(t,u)[ u(2); 3*cos(2*t)-u(1)-2*u(2) ], [0,6], [s;1] );
plot( t, u(:,1) ), hold on
end
• SERIES solutions in MAPLE: to compute the first 9 terms of the
series solution for cos(x)y''+xy'-2y=0, near x = 0, with y(0) = 1, y'(0) = 2, use:
Order := 9;
eqn := cos(x)*diff(y(x),x\$2) + x*diff(y(x),x) - 2*y(x) = 0;
dsolve({eqn, y(0) = 1, D(y)(0) = 2}, y(x), type = series );
• EULERS's METHOD:
Assume you want to solve y'=2xy, given y(1) = 2, for x in [1,2], with Euler's method, for step sizes h = .1,.05,.025,
and plot the numerical solution with the actual solution.
• In MATLAB, use
for N = [10 20 40]; h = 1/N; y(1) = 2; x = 1+h*[0:N];
for n = 1:N, y(n+1) = y(n) + h*2*x(n)*y(n); end
plot(x,y), hold on
end
For the same problem to compute final solution and compare with actual solution for N = 16, 32, 64, 128, 256 steps use
for N = [16 32 64 128 256]; h = 1/N; y(1) = 2; x = 1+h*[0:N];
for n = 1:N, y(n+1) = y(n)+h*2*x(n)*y(n); end
z = 2*exp(x(N+1)^2-1); e = y(N+1)-z; disp([N h y(N+1) z e])
end
• SYSTEMS:
If you want a components plot and phase-plane plot of the vector function (cos(t),sin(t)) for t in [0,3]:
• In MATLAB, use
t = [0:.25:3]; x = cos(t); y = sin(t); plot(t,x,t,y); plot(x,y)
• In MAPLE, use
plot([cos(t), sin(t)], t = 0..3 );
plot([cos(t), sin(t), t = 0..3] );
Consider the system R'= (.4-.1F)R, F'= (-.3+.1R)F, with initial values R(0)=10, F(0)=20, for t in [0,40].
Suppose you want to solve the system numerically, and plot R(t) and F(t) together, with a separate phase plane plot.
• In MATLAB, use
up = @(t,u)[(.4-.1*u(2)).*u(1); (-.3+.1*u(1)).*u(2)];
[t u] = ode45( up, [0,40], [10;20] );
plot(t,u); plot(u(:,1),u(:,2))
To add a composite graph, use
z=zeros(length(t),1); plot3(u(:,1),u(:,2),t,u(:,1),z,t,z,u(:,2),t);
• In MAPLE, use
sl := dsolve({ diff(R(t),t)=(.4-.1*F(t))*R(t), diff(F(t),t)=(-.3+.1*R(t))*F(t),
R(0)=10, F(0)=20 },{R(t),F(t)}, numeric }; with(plots):
odeplot( sl, [[t,R(t)],[t,F(t)]], 0..40 );
odeplot( sl, [R(t),F(t)], 0..40 );
Consider the system u'= 8u+20v, v'= -4u-8v. Suppose you want to solve the system numerically for t in [0,2],
and plot two solutions (starting at (1,1) and (-1,1)) in the phase plane, along with a direction field.
• In MATLAB, use
yp = @(t,y)[8 20;-4 -8]*y;
[t y] = ode23( yp, [0,2], [1;1] );
plot(y(:,1),y(:,2))
[t y] = ode23( yp, [0,2], [-1;1] );
hold on, plot(y(:,1),y(:,2))
[y1,y2]=meshgrid(-8:1:8,-4:.5:4);
yp1 = 8*y1+20*y2; yp2 =-4*y1-8*y2;
L = sqrt(yp1.^2+yp2.^2);
hold on, quiver(y1,y2,yp1./L,yp2./L,.5)
axis tight
Note: you need to use the scale information from the first two plots
to pick reasonable parameters from the meshgrid command.