Title: 5th lecture
1Elementary Programming using Mathcad (INF-20303)
2No lecture coming Wednesday
- Because of software problems many people run
behind in the practicals - We skip the material from last lecture
practical
3- Series
- Example Fibonacci series
- 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,
89, - Prepare an efficient function that produces this
series (or outcome) - Therefore you need to detect the relation
between successive terms - The first two terms are 1
- Each next term is the sum of the two previous
terms - So the recursive relation is
- (Preparation of the function is an assignment in
one of the - coming practical sessions)
no vector needed
4- Another example
- Two issues are important
- Design an efficient function
- How many terms are needed for an accurate result?
5 - numerator so
- a successive numerator is previous
numerator - denominator which
means -
- So
Denominator of next term is n times denominator of
previous term.
term n of series
6term n of series
Algorithm Make Repeat as long as
needed
value of series
value of a term
term number
7repeat
8repeat
for does that job
Algorithm ok However, specifying number of terms
isnot very attractive
9When to stop adding terms?
- Series to compute power of e
-
- Every successive term is smaller than its
predecessor - As of a certain term, the terms will not
contribute to the - value of the series (they are simply too
small), for example - Suppose r 2.5 and t 1.235 10-10
- To be able to add powers of 10 should be equal,
so - r 2.5 and t 0.0000000001235
- Suppose computer can store up to 7 decimal
places, then - 0.0000000001235 0
10- We may stop computing terms when t becomes 0
relative to r - At that time
- So repeat as long as
11A slightly different algorithm (using while)
t is second term
Repeat as long as
Make sure that from the first time holds.
12Try out (test) r 1 t x n 1
r 1 x n 1 1 2 t x x/2 x2/2
r 1 x x2/2 n 2 1 3 t (x2/2) x/3
x3/3!
r 1 x x2/2 x3/3! n 3 1 4 t
(x3/3! ) x/4 x4/4!
test at least 3 terms!
13Results
Results are incorrect for
14Another alternative
Not efficient and therefore not a correct
function! (dont do this during exam).
For term 3 the computer computes Overflow
when n is large inaccurate inefficient
15Why does that not work? At a certain point, we
have as numerator and denominator
- Remarks
- n! becomes large for small values of n, see 40!
and 41! - power also becomes large
- n! may become larger than the largest possible
number - n! increases faster than the power (terms
decrease) - outcome will be unreliable.
- the function given before is the correct function
16More series sinus
- numerator is previous numerator
- denominator
- denominator term 2 is denominator
term 1 - denominator term 3 is denominator
term 2 - so in general (in terms of n), denominator is
times denominator previous term, with - every term has opposite sign
17Algorithm
- numerator previous numerator
- denominator times previous
denominator - multiply every term by -1
- gives the algorithm
18More series
- numerator previous numerator
- denominator successively 1, 3, 5, 7, ..
- different situation denominator not by means of
multiplication - ? we need an additional variable
19Translate algorithm to Mathcad
Make
Repeat
20An example with several nested for statements
Which numbers lt 600 have this property?
- Solution
- Assume the number is abc
- the number consists of digits a, b, c
- the value of the number then is
- this value should be equal to
- which numbers of 3 digits lt 600 fulfill this
condition? - we check all possible numbers
- a varies from 1 up to and including 5
- b varies from 0 up to and including 9
- c varies from 0 up to and including 9
21Important element of algorithm
- We have to test the specific criterion
We have to check this for every possible
combination of a, b and c. Therefore we need
three nested for statements
22Structure of algorithm
if a number fulfills the requirement, we insert
the number in a vector.
23Algorithm
Because we store the numbers in a vector
We can do without h
sumThird has no argument, so it is not a function!
24Result is
25Preparation section 14.3, assignment 4
mod(m,n) returns remainder on dividing m by n We
may determine this by repeated subtractionfor
example mod(14,3) 14
3 11
3
8 3
5 3
2
Repeat subtraction of n until .? Apply a
statement to repeat the subtraction.
26Answer section 13.1, assignment 2
(write function to calculate area of polygon)
x1,y1
x2,y2
x0,y0
x4,y4
x3,y3
x5,y5
27Algorithm
What here?
last(x) can be an index
28Also possible factoring out factor 1/2
Or
29Can we expect any problems?
30Here we assume that the vectors x and y in the
call have the same number of elements. What if
we assume wrong?
31Function area can only return a correct result if
the number of elements of x and y are equal.
32Other solution The area equals to
x1,y1
x2,y2
x0,y0
x4,y4
x6,y6
x3,y3
x5,y5
Complexity of function reduces by introducing an
additional vertex
33(Extra vertex (point) is added to vectors
holding x and y values)
The area then can be computed with
Function then becomes
34(No Transcript)
35Answer section 13.1, assignment 3
Find the largest element in a matrix
Test the general case (max is largest element, x
is matrix)
We should test all elements of matrix x
rows(x) is number of rows of x. Because the
first row has number 0 rows(x)-1
36Recall maximum value in a vector. We started
there with 1. Is that correct here?