MATH226 Numerical Methods for Applied Mathematics
Numerical Methods for Applied Mathematics
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
MATH226 Numerical Methods for Applied Mathematics
1 Programming in Maple
Make sure Maple has been configured properly before trying any of the
examples below for yourself. Configuration instructions are in the module
handbook.
1.1 Getting started
• A Maple program is a sequence of statements, each of which tells Maple to
perform some action(s). For example typing
restart
and pressing return causes Maple to clear any currently stored data. It is a
good idea to issue a restart command immediately before starting a new
problem (including at the beginning of the Maple worksheet).
• Pressing shift+return moves the cursor down one line without executing.
This is useful when writing complicated statements.
• The result of a statement that ends with a colon is not displayed. Otherwise,
results will be displayed if they do not generate an excessive amount of
output (e.g. a huge matrix). This behaviour can be altered by changing the
printlevel and rtablesize parameters (see §1.12 and §1.9, respectively).
• The black square brackets to the left of the window indicate the scope of
execution groups. If two statements are inside the same execution group,
then they need to be separated by a colon or a semicolon.
• Maple is case sensitive; for example int and Int are different commands.
• Maple has a comprehensive online help system. To obtain information about
a command, enter a question mark, followed by the command, e.g. ?evalf.
It is usually easiest to scroll down to the examples at the foot of the help
page.
• Maple very rarely crashes, but this does sometimes happen. When you start
a new worksheet, save it immediately after putting restart on the first
line. By default, a saved worksheet will save again automatically every three
minutes. Do not turn off autosave.
1
Numerical Methods for Applied Mathematics MATH226 2021–22
• If a calculation is taking too long, you can try to stop it by pressing the
interrupt button in the toolbar. This may take a few seconds to work, but
it may not work at all. This is one reason why you should never deactivate
autosave.
1.2 Exact vs approximate results
Unless told to do otherwise, Maple tries to produce exact results, which can be
problematic. Suppose we wish to determine whether the inequality∫ 1
0
x
1 + x5 dx > 0.4
holds. The statement
int( x / ( 1 + x^5 ) , x = 0..1 )
tells Maple to evaluate the integral, but the result is rather complicated, and doesn’t
really help. The evalf command tells Maple to calculate an approximate numerical
value, so the result of
evalf( Int( x / ( 1 + x^5 ) , x = 0..1 ) )
is much easier to interpret. Complicated exact results can cause Maple to slow
down and appear to freeze if they are reused in later calculations.
Use evalf to avoid complicated exact results.
Remark: evalf( int( ... ) ) with a lower case ‘i’ tells Maple to evaluate
the integral exactly (if it can) and then convert the result to a decimal. See
problem 2.1.
1.3 Comments
Maple ignores material from a # symbol until the end of the line. This is used to
insert explanatory notes for statements whose effect is not obvious.
# Create a 2 x 2 matrix
Matrix( 2 )
If a comment is inserted after a statement, then a colon or semicolon must be
included in between.
a := 1 ; # Would generate an error without the semicolon
2
MATH226 2021–22 Numerical Methods for Applied Mathematics
1.4 Variables
Variables are used to store data. To assign a value to a variable, use the assignment
operator :=.
a := 27 :
b := 4 :
c := a + b :
c
31
Assignments are not equations. The expression on the right is computed, then
the result is stored in the variable on the left. This means the right-hand side can
reference the variable that is about to be assigned.
a := 27 :
a := a + 1 :
a
28
Maple allows sequences of assignments to be made in the same statement.
a , b := 15 , 21 :
a
15
b
21
This provides a very useful shortcut for swapping variable values.
a := 15 :
b := -6 :
a , b := b , a :
a
-6
b
15
Often, Maple does not need to distinguish between different types of variable such
as integer or complex, but there are situations where this is important.
a := 1 :
b := 1.5 :
type( a , integer )
true
3
Numerical Methods for Applied Mathematics MATH226 2021–22
type( b , integer )
false
Variables in Maple can possess more than one type.
type( a , integer )
true
type( a , numeric )
true
type( a , complex )
true
In this way, Maple differs from languages such as C and Fortran.
See sections 2.11 and 2.12 in Understanding Maple for more about variables.
1.5 Conditional statements
A conditional (if) statement causes Maple to test a condition, then carry out
different operations depending on whether the condition is true or false. The
simplest conditional statement instructs Maple to perform a set of actions only if a
condition is true.
if condition then
statement[s]
end if
Note that this is a single statement (which contains other statements). The line
breaks are produced by pressing shift+return, and the whole structure must be
executed together.
Any statement that evaluates to true or false can be used in an if statement.
cold_today := true :
if cold_today then
"Wear your hat!" :
end if
"Wear your hat!"
In cases where a conditional contains a sequence of statements (as opposed to a
single statement), these must be separated by colons or semicolons. Otherwise
Maple has no way to know where one statement ends and the next begins.
4
MATH226 2021–22 Numerical Methods for Applied Mathematics
a := 1 :
b := a :
increase_vars := true :
if increase_vars then
a := a + 1 : # <--- The colon on this line is crucial!
b := b + 2 :
end if
Boolean operators including and, not and or can be used to construct more
complex conditions.
if a > 0 and frac( a ) = 0 then
"a is a positive integer." :
end if
Here, the message is displayed if a is a positive integer. Note the frac command,
which returns the fractional part of a number (i.e. the digits after the decimal
point). Using else instructs Maple to take different actions (rather than none at
all) if the condition is false.
if condition then
statement[s]
else
alternative statement[s]
end if
Finally, we can check additional conditions if the first turns out to be false.
if condition then
statement[s] # Execute if condition is true
elif second condition then
alternative statement[s] # Execute if first condition is false,
# but second condition is true.
else
more alternative statement[s] # Execute if all above conditions
# are false.
end if
5
Numerical Methods for Applied Mathematics MATH226 2021–22
In the next example, the message "a is negative" will only be printed if both
conditions (a > 0 and a = 0 are false).
if a > 0 then
"a is positive" :
elif a = 0 then
"a is zero" :
else
"a is negative" :
end if
See section 7.1 in Understanding Maple for more about conditional state-
ments.
1.6 do loops
A do loop causes Maple to repeatedly execute the same statements. The next
example causes Maple to display the approximate value of pi five times.
from 1 to 5 do
evalf( Pi ) :
end do
3.141592654
3.141592654
3.141592654
3.141592654
3.141592654
Often we need to use the step number itself during the iteration. This is achieved
using a loop with an index variable.
for index from start to finish do
statement[s]
end do
Here, index , start, and finish are integers. Initially, index is set to equal start.
After each iteration, index is increased by 1. If the result exceeds finish, the loop
terminates. Otherwise another iteration is performed. If start exceeds finish, the
statements inside the do loop are not executed at all. We can compute 10! as
follows.
6
MATH226 2021–22 Numerical Methods for Applied Mathematics
p := 1 :
for j from 2 to 10 do
p := p * j : # Updates the value of p
end do :
p
3628800
As another example, we can compute the sum 1 + 12 +
1
3 + · · ·+ 1n as follows.
n := 100 : # (Or any other natural number)
s := 0 :
for j from 1 to n do
s := s + evalf( 1 / j ) :
end do :
s
5.187377520
Increments other than 1 are possible.
for index from start by increment to finish do
statement[s]
end do
If increment is negative, the loop will terminate when index reaches a value that is
smaller than finish, and will not execute at all if finish exceeds start.
for j from 5 by -1 to 1 do
j ;
end do
5
4
3
2
1
To increment a variable through noninteger values, calculate a step size before the
loop starts. In the next example, x varies from 0 to 3 in steps of size 3/7.
nsteps := 7 : # Number of steps
dx := evalf( 3 / nsteps ) : # Step size
7
Numerical Methods for Applied Mathematics MATH226 2021–22
for j from 0 to nsteps do
x := j * dx ;
end do
x := 0.
x := 0.4285714286
x := 0.8571428572
x := 1.285714286
x := 1.714285714
x := 2.142857143
x := 2.571428572
x := 3.000000000
Using a floating point (decimal) index can lead to a disastrous situation in which
one too few steps is performed because the last value for x exceeds the upper limit
due to rounding error.
dx := evalf( 3 / nsteps ) : # Step size
for x from 0 to 3 by dx do
x ;
end do
x := 0.
x := 0.4285714286
x := 0.8571428572
x := 1.285714286
x := 1.714285714
x := 2.142857144
x := 2.571428573
x
3.000000002
Never use a decimal as a do loop index.
Remark: In Maple, it is possible to use an exact fraction as a loop index (e.g.
try the above example without evalf). However, very few other programming
languages allow this, so we will always use integers.
8
MATH226 2021–22 Numerical Methods for Applied Mathematics
1.7 Break statements
Sometimes it is not possible to predict how many iterations will be needed for a
particular task. For these cases we can use a do loop with no final value for the
index (or no index at all). In such cases the loop must be terminated by a break
statement.