Search for question
Question

2. An interesting function is shown below, and this pattern continues for all x. For x between 0 and

4, the function takes on these values (pattern repeats itself indefinitely):

f(x)

20

10

f(x)=0 for 0 < x < 1

f(x) = 10 for 1 ≤ x < 2

f(x) = 20 for 2

f(x) = 10 for 3 ≤ x < 4 ... and so on.

1 2 3 4 5 6 7 8

X

Pattern

continues

Write a VBA function called Periodic that returns the value f(x) of the function above for a given

value of x. If a negative value for x is input, the function should just be the same (mirror image

about the y-axis). Importantly, x should not only be constrained to integers, but x can take on any

real value.

Strategy. When faced with a repeating pattern, as is in this problem, it can be valuable to utilize

the Mod operator in VBA, which yields the remainder when two numbers are divided./nCHEN 1310

Homework #6

Page 3

result = number1 Mod number2

The modulus, or remainder, operator divides number1 by number2 (rounding floating-point

numbers to integers) and returns only the remainder as result.

In this problem, there is a pattern that repeats itself every 4 units of x. In other words,

f(x)=f(x+4.n), where n is a positive integer. So, a statement of the form x Mod 4 will convert

any x > 4 to a "transformed" 0

The only other caveat is the fact that the Mod operator first converts number1 to an integer

(rounding to the nearest integer); therefore, 7.5 Mod 4 is not equal to 3.5 nor 3, but rather is equal

to 0 (the 3.5 is first rounded up to 4 so when divided by 4 there is no remainder). This problem can

be circumvented by prudent use of the Int function, which returns the integer value of a number,

e.g., Int(3.5) = 3.

Create the function and verify that it is working properly. What is f(2873)?

Fig: 1

Fig: 2