You are on page 1of 7

Week 11

11.1 Symbolic algebra with Maple

Sections left over from end of last week.

11.1.1 Manipulating algebra

We can expand expressions (there are options on how you choose to make the expansion),
substitute values into expressions, make Taylor/MacLaurin series, take limits and do sums

[> expand((1-x)^10, x);


1-10*x+45*x^2-120*x^3+210*x^4-252*x^5+210*x^6-120*x^7+45*x^8-10*x^9+x^10

[> subs(x=1,%);
0

[> series(cos(x), x = 0);


1-(1/2)*x^2+(1/24)*x^4+O(x^6)

[> limit(sin(x)/x, x = 0);


1

[> sum(1/j^2,j=1..infinity);
pi^2/6

Note: We use percent to mean the output of the last command issued (not necessarily the
one above on the worksheet)

11.1.2 Calculus

Here we demonstrate how to take derivatives, calculate both indefinite and definite integrals

[> diff(tan(x),x);

1
1+tan(x)^2

[> simplify(%)
1/cos(x)^2

[> int(1/(1+sqrt(p)),p)
-ln(p-1)+2*sqrt(p)+ln(-1+sqrt(p))-ln(1+sqrt(p))

[> int(exp(-t^2),t=0..infinity);
sqrt(Pi)/2

Note: The simplify command can be useful as Maple does not always produce the
symbolic representation of the solution you would expect.

11.2 Restart, Help, Saving work, Printing

If things are not going well, you can clear everything with

[> restart:

If you need help with a command, for example expand, use the question mark; for example:

[> ?expand

There is also a search facilty in the Maple window.


To save your work, click on File and Save as... and select a .mw file (this is a Maple
Worksheet file. This is stored by default in your Documents folder and can be opened in a
new session.
In order to print your work, goto File and select Export as... and save as a .pdf file. This
will be stored in your Documents folder and you can print the PDF from there.

11.3 Solving equations in Maple

Exact solutions

We can use solve in Maple to find analytic roots of equations

[> solve(x^2+x+1 = 0, x);


-1/2+(1/2*I)*sqrt(3), -1/2-(1/2*I)*sqrt(3)

2
The solve command can be extended to solutions of systems of equations, for example:

x + 3y + z = 1, x y + z = 0, y z = 1

is solved using

[> sys := {y-z = -1, x-y+z = 0, x+3*y+z = 1};

[> solve(sys)
{x = -1, y = 1/4, z = 5/4}

11.3.1 Numerical solutions

Maples fsolve command is used to find roots of equations numerically. For example, if
we want roots of
tanh(x) = tan(x)
we use

[> fsolve(tanh(x) = tan(x), x = 3 .. 4);


3.926602312

[> fsolve(tanh(x) = tan(x), x = 5 .. 8);


7.068582746

11.4 ODEs in Maple

Maple is particularly useful in helping to solve and visualise solutions to Ordinary Differ-
ential Equations (ODEs).

11.4.1 1st order ODEs

Lets start with examples we can do by hand this shows how Maples symbolic algebra
capability helps you solve ODEs

[> ode := diff(y(x), x)+y(x) = 0;

[> ic := y(0) = 1;

[> dsolve(ode);
y(x) = _C1*exp(-x)

[> dsolve({ic, ode});


y(x) = exp(-x)

3
Note: The first solution is the general solution, the second example solves the ODE with
the initial condition.

Numerical solutions

Now lets do an example which cannot be solved by hand; i.e. it must be done numerically

[> ode := diff(y(x), x)+sin(y(x)) = sin(x);

[> sol := dsolve({ic, ode}, type = numeric, range = 0 .. 10);


sol:=proc(x_rkf45) ... end proc

Note: This output represents success (it indicates that the solution is solved numerically
by a Runga Kutta 4.5 method.) Note also that the option range sets the interval that the
independent variable (x) that the solution is generated over.
In order to access the solution, we need to load a special set of plotting tools

[> with(plots):
[> odeplot(sol);

11.4.2 2nd order ODEs

Again, we start with something we can do explicity a linear 2nd order ODE with constant
coefficients:
y (x) + 9y(x) = 0, with y(0) = 0, y (0) = 3 (11.1)

[> ode := diff(diff(y(x), x), x)+3^2*y(x) = 0;

[> dsolve(ode);
y(x) = _C1*sin(3*x)+_C2*cos(3*x)

[> ics := y(0) = 0, (D(y))(0) = 3

4
[> dsolve({ics, ode});
y(x) = sin(3*x)

[> sol := dsolve({ics, ode}, numeric, range = 0 .. 4);


sol:=proc(x_rkf45) ... end proc
[> odeplot(sol);

Numerical solutions to 2nd order ODEs

Now lets do a problem which must be solved numerically:


y (x) + 9 sin(y(x)) = 0, with y(0) = 0, y (0) = 3 (11.2)

[> ode := diff(diff(y(x), x), x)+3^2*sin(y(x)) = 0

[> sol2 := dsolve({ics, ode}, numeric, range = 0 .. 4);

[> odeplot(sol2)

[> odeplot(sol2, [y(x), diff(y(x), x)])

The last two command produce the following two plots. The first is, by default, y(x)
against x. The second plot shows the variation of y (x) with y(x) which can be thought of
as speed against displacement, since ...

Remark: Equation (11.2) is the exact description of a pendulum oscillating under grav-
ity (and y(x) represents the angular excursion with time) and (11.1) is the small angle
approximation. You can see small differences between the results in Figs. 11.4.2, 11.4.2.

5
More complicated ODEs

Maple can be useful to find analytic solutions to ODEs which you find hard to do by hand.
For example:  
d dy
x + xy(x) = 0, with y(0) = 1, y (0) = 0
dx dx
(non-constant coefficients... solution not obvious !) In Maple

[> ode := diff(x*(diff(y(x), x)), x)+x*y(x) = 0;

[> dsolve(ode)
y(x) = _C1*BesselJ(0, x)+_C2*BesselY(0, x)

[> ics := y(0) = 1, (D(y))(0) = 0;

[> dsolve({ics, ode});


y(x) = BesselJ(0, x)

[> plot(rhs(%), x = 0 .. 10);

Note: Use of rhs to isolate the right-hand side of an equation.

11.4.3 Coupled systems of ODEs: An example

The Lokta Volterra Equations are represented by a pair of ODEs for two unknown
functions. They are a basic Preditor-Prey model of the evolution with time, t, of a
population of two interacting species, most easily thought of as rabbits, r(t), and foxes,
f (t).
The Lokta Volterra Equations are written:
dr df
= r(t) r(t)f (t), = r(t)f (t) f (t) (11.3)
dt dt
where , , , are model parameters which represent birth rates, death rates and inter-
actions between the two populations. For e.g. if = = 0 so that rabbits and foxes do
not interact then
dr
= r(t), r(t) = C1 et
dt

6
and
df
= f (t), r(t) = C2 et
dt
for two arbitrary constants C1 and C2 to be set by initial conditions. That is, the rabbit
population grows exponentially and the fox population dies exponentially. This is because
foxes are not eating rabbits in this model. So the interaction terms tell you how the fox
population grows with rabbits and how rabbits die due to the fox population.

[> ode1 := diff(r(t), t) = 3*r(t)-2*f(t)*r(t);

[> ode2 := diff(f(t), t) = 2.5*f(t)*r(t)-f(t);

[> ics := r(0) = 1, f(0) = 1;

[> sol := dsolve({ics, ode1, ode2}, numeric, range = 0 .. 10);

[> odeplot(sol, [[t, r(t)], [t, f(t)]]);

[> with(DEtools):

[> phaseportrait([ode1, ode2],[r(t),f(t)],t=0..10,[[ics]],stepsize = 0.05);

and there are two graphical outputs. The first is the plot of the evolution of r(t) and f (t)
with time t. The second requires us to load a special Maple library ([> with(DEtools):
before plotting the so-called phase portrait. This shows r(t) against f (t) and the field
lines (lines with arrows) show the direction the solution evolves in time. The single curve
is the specific trajectory in (r(t), f (t)) space of the solution with initial conditions defined
here by ics (r(0) = 1, f (0) = 1).

Remark: We can easily find the equilibrium point where dr/dt = df /dt = 0 and the
solution remains steady and fixed for all time, by solving the nonlinear equations that result
from imposing this on (11.3). Thus we see easily that these are given by r(t) = f (t) = 0
and r(t) = / and f (t) = /.

You might also like