12th lecture - PowerPoint PPT Presentation

1 / 51
About This Presentation
Title:

12th lecture

Description:

d of worksheet! test 13 9 5 = 14 yes: p = 13 5 = 18. z = 5 1 = 4. test 18 ... although possible: do not by multiplication. 24. 25. Assume the series is: ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 52
Provided by: Infor374
Category:

less

Transcript and Presenter's Notes

Title: 12th lecture


1
Elementary Programming using Mathcad (INF-20303)
  • 12th lecture

2
Answers section 20.1, assignment 1 Multiplying by
a recursive function
  • multiply m by n
  • stopping case is n 1, result m
  • other cases m result of m times n-1

Here, we assume m and n 1. If n lt 1 the a
problem recursion does not stop.
3
Answer section 20.1, assignment 2
Compute with a recursive function
4
Answer assignment 3 of 20.1 Compute sum of digits
of a (integer positive) value. Example sum of
digits of 3412 Sum of 3412 2 sum of
341 sum of 341 1 sum of 34
sum of 34 4 sum of 3 sum of 3 3
sum of 0 sum of 0 0 (stopping
case!) How we get lowest significant digit?
mod(3412,10) How we come from 3412 to 341?
floor(3412/10)
5
Answer section 20.1, assignment 4 Recursive
version of Rule of false position (Regula Falsi)
Like in the bisection the interval will be
reduced . Apply on the reduced interval Rule of
false position again.
Geen means No roots on given interval!
6
Is it necessary to test whether f(a) 0 and/or
f(b) 0?
Mathcad no problem however it is an error!
7
Extra matrix exercise
  • A matrix is square if number of columns equals
    number of rows.
  • The main diagonal of a 4?4 matrix M
  • are the elements
  • A square matrix can be symmetrical
  • An element mirrored against the main diagonal
    have the same value
  • For example

8
  • Design a function that detects
  • whether a given matrix is symmetrical
  • Result
  • 0 if matrix not symmetrical
  • 1 if matrix is symmetrical

9
Look at element mi,j
Then should hold
This should hold for every i and j
Algorithm 1
Is this OK?
10
Assume the matrix is
Matrix is not symmetrical. Assume the last test
concerns the elements 3,2 and 2,3.
Because m3,2 m2,3 becomes r 1 and The
conclusion is that the matrix is symmetrical!
So, if (only) the last element is ok then result
1!
11
A way out is to suppose the opposite We assume
the matrix is symmetrical and if the condition
does not hold for one element and then we make r
0.
Remark as soon as r becomes 0 we know the matrix
is not symmetrical, so we can stop the function.
Therefore, a while! We return to this.
12
Prerequisite is matrix is square we must test
that
13
  • Here we test
  • all elements twice, and
  • main diagonal elements

14
(No Transcript)
15
  • Here we test the elements, assume 4?4 matrix
  • i0 NOT
  • i1 1,0
  • i2 2,0 2,1
  • i3 3,0 3,1 3,2

So the lower triangle below the main diagonal
16
Not correct is (however you will get points for
it)
This is an incorrect solution because if the last
element is the only element that is symmetrical
the whole matrix will be detected as symmetrical.
17
Function that stops if the first non symmetrical
element has been found
Inner for-loop
Outer for-loop
Give this answer only if the question was stop
searching when it is clear the matrix is not
symmetrical.
18
Answers exam October 23, 2000 question 1
f(a,b) p value of a 13 c value of b
9 c lt 10 so a d 5 d of
worksheet! test 13 lt 9 5 14 yes p 13
5 18 z 5 1 4 test 18 lt 14 no result
p 18
In current version you will not succeed in
entering p !
19
z z in function is local so, z is from
worksheet and 7
a a gets a value inside the function,
however a is local. Here a of worksheet and
value is 13
p On worksheet no p, so p does not exists!
20
f(a,c) p value of a 13 c value of c
11 c gt 10, so a d -5 test 13 lt 11 5 16
yes p 13 (-5) 8 z 5 1 4 test 8 lt
16 yes p 8 (-5) 3 z 5 1 4 test 3 lt
16 yes ........ Does not stop! Infinite loop!

21
Answer exam October 23, 2000 question 2
Write a function that translates a decimal number
into a binary equivalent, for example
Assume
one digit follows from
divide by 2
remainder
result
next digit
n becomes
22
Repeat until n0
To store first digit
Also n gt 0
23
Answer exam October 23, 2000 question 3
Numerator multiply by
  • Denominator
  • increase by 2
  • although possible do not by multiplication

24
(No Transcript)
25
Assume the series is
Numerator multiply by x
Denominator
Multiply by 2, 4, 7, 11
So we need a variable nAdd that increases by 1
for the next term And add that to n (denominator)
26
nAdd 0 n 1 nAdd 1 n 11 2
nAdd 3 n 4 3 7
nAdd 2 n 2 2 4
27
(No Transcript)
28
Answer exam October 23, 2000, question 4
13 6 7 21 1 2 3 4 5 6 21 6 7
8 21 10 11
Consist of two parts!
  • Sum of successive numbers
  • Find all combinations for a given N
  • At first write function checkSum
  • Determines whether a given number is a start of
    a series
  • with sum N
  • If success returns vector with the successive
    numbers.
  • Otherwise returns 0
  • Second, write a function that finds all series.
  • Result for 21 is

29
Part a
Heading function is
Start with s and make a series
We need
Successive numbers
In vector
We need
30
Check whether sum equals to N
31
Alternative subtract s from N, we do not need
sum
32
  • Part b Find all possibilities
  • return 0 if there are no numbers satisfying the
    condition
  • return a matrix if there are some series
  • heading

Assume we like to test number i to be a start
number
If r ? 0, store in vector
Copy all elements of r into w
nr needs to be initialized!
33
Repeat for i in 1 to N-1
34
Exam October 22, 2001, question 1
f(a,c) p value of a 15 c value
of c 17 execute instructions a 15
(local variable!) test 15 lt 17 yes
a 15 - 5 10 z 5 - 1 4
test 10 lt 17 yes a 10 - 5
5 z 5 - 1 4 test 5 lt 17
yes etc. Result infinite loop
35
z We are outside of the function, so z is from
the worksheet z of function cannot be
accessed. So, z 5
a We mean here a of worksheet So, a 15
p current version does not do, p is not known
outside of function
36
f(a,b) p value of a 15 c value
of b 10 executing instructions a 15
(local variable!) test 15 lt 10 no
a 15 is result.
37
Exam October 22, 2001, question 2 Write a
function that returns all numbers ? N (given)
that are divisible by 5 or 7. Example
N is given so it will be an argument of the
function.
38
Assume we like to know whether i can be divided
by 5 or 7. What we have to check?
What with i that satisfies the condition? Save i
in a vector
Note m should be initialized!
39
Repeat this for every i ? N, so
Numbers lt 5 will not satisfy the condition. If
you like, you may do.......
40
Gives
For N lt 5 a problem?
For example N3 Then for range 5..3 5 will be
tested, so error message!
41
Alternative
Problem numbers divisible by 5 and 7 will occur
twice! See 35.
42
A better solution is
For cosmetic reasons only
43
Exam October 22, 2001 question 3 Write function
for the following series
For
  • Numerator
  • multiply by x, and
  • multiply by factor, that decreases by 1 in next
    term.
  • Denominator multiply by n, where n increases by
    1 in next term.

44
already first term
a is factor the numerator should be multiplied
second term
Decreases factor by 1
45
Alternative
46
Exam October 22, 2001, question 4 The numbers
  • Form a so-called magic square.
  • A magic square has the following properties
  • sum of all elements per row are equal
  • sum of all elements per column are equal
  • sum of both main diagonal elements are equal
  • all mentioned sums are equal, here 15
  • all numbers appear only once
  • Write function(s) to test whether a square matrix
    is
  • so-called additive (we drop the last property)
    magical square.

47
  • The following functions have to be available
  • sumRow(M,n), computes sum of elements in row n
    (given)
  • sumCol(M,n), computes sum of elements in column
    n (given)
  • sumDiagU(M), computes sum of elements on the
  • diagonal that starts in the upper left corner
    (given)
  • sumDiagL(M), computes the sum of the elements on
    the
  • diagonal that starts in the lower left corner
    (required)
  • Question a write sumDiagL
  • M is matrix with n1 rows and columns.
  • We start in element
  • We need to add the following elements

48
Gives
We add the following elements
49
  • Question b
  • Write a function that test whether the given
    matrix is a
  • additive magical matrix.
  • Write function such that no additional sums are
    computed
  • if already known that the matrix is nt additive
    magical.
  • This means a while!
  • Our function test in the given order (it is your
    choice)
  • rows
  • columns
  • diagonals

50
testen rijen
To be able to compare other sums
magic needed to test whether we may continue
alternative
51
given before
test columns
diagonals
new
Write a Comment
User Comments (0)
About PowerShow.com