• No results found

Generalization

In document Physical Modeling in MATLAB (Page 40-45)

Exercise 3.4 This example computes the terms of the series directly; as an exercise, write a script named series that computes the same sum by computing the elements recurrently. You will have to be careful about where you start and stop the loop.

3.8 Generalization

As written, the previous example always adds up the first 10 elements of the sequence, but we might be curious to know what happens to total as we increase the number of terms in the sequence. If you have studied geometric series, you might know that this series converges on 2; that is, as the number of terms goes to infinity, the sum approaches 2 asymptotically.

To see if that’s true for our program, we could replace the constant, 10, with a variable named n:

A1 = 1;

total = 0;

for i=1:n

a = A1 * 0.5^(i-1);

total = total + a;

end

ans = total

Now the script can compute any number of terms, with the precondition that you have to set n before you execute the script. Here’s how you could run it with different values of n:

>> n=10; series

total = 1.99804687500000

>> n=20; series

total = 1.99999809265137

>> n=30; series

total = 1.99999999813735

>> n=40; series

total = 1.99999999999818

It sure looks like it’s converging on 2.

Replacing a constant with a variable is called generalization. Instead of com-puting a fixed, specific number of terms, the new script is more general; it can compute any number of terms.

This is an important idea we will come back to when we talk about functions.

3.9 Glossary

absolute error: The difference between an approximation and an exact an-swer.

relative error: The difference between an approximation and an exact answer, expressed as a fraction or percentage of the exact answer.

loop: A part of a program that runs repeatedly.

loop variable: A variable, defined in a for statement, that gets assigned a different value each time through the loop.

range: The set of values assigned to the loop variable, often specified with the colon operator; for example 1:5.

body: The statements inside the for loop that are run repeatedly.

sequence: In mathematics, a set of numbers that correspond to the positive integers.

element: A member of the set of numbers in a sequence.

recurrently: A way of computing the next element of a sequence based on previous elements.

directly: A way of computing an element in a sequence without using previous elements.

series: The sum of the elements in a sequence.

accumulator: A variable that is used to accumulate a result a little bit at a time.

generalization: A way to make a program more versatile, for example by replacing a specific value with a variable that can have any value.

3.10 Exercises 31

3.10 Exercises

Exercise 3.5 We have already seen the Fibonacci sequence, F , which is defined recurrently as

Fi = Fi−1+ Fi−2

In order to get started, you have to specify the first two elements, but once you have those, you can compute the rest. The most common Fibonacci sequence starts with F1= 1 and F2= 1.

Write a script called fibonacci2 that uses a for loop to compute the first 10 elements of this Fibonacci sequence. As a postcondition, your script should assign the 10th element to ans.

Now generalize your script so that it computes the nth element for any value of n, with the precondition that you have to set n before you run the script. To keep things simple for now, you can assume that n is greater than 2.

Hint: you will have to use two variables to keep track of the previous two ele-ments of the sequence. You might want to call them prev1 and prev2. Initially, prev1 = F1 and prev2 = F2. At the end of the loop, you will have to update prev1 and prev2; think carefully about the order of the updates!

Exercise 3.6 Write a script named fib plot that loops i through a range from 1 to 20, uses fibonacci2 to compute Fibonacci numbers, and plots Fi for each i with a series of red circles.

Chapter 4

Vectors

4.1 Checking preconditions

Some of the loops in the previous chapter don’t work if the value of n isn’t set correctly before the loop runs. For example, this loop computes the sum of the first n elements of a geometric sequence:

A1 = 1;

total = 0;

for i=1:n

a = A1 * 0.5^(i-1);

total = total + a;

end

ans = total

It works for any positive value of n, but what if n is negative? In that case, you get:

total = 0

Why? Because the expression 1:-1 means “all the numbers from 1 to -1, count-ing up by 1.” It’s not immediately obvious what that should mean, but MAT-LAB’s interpretation is that there aren’t any numbers that fit that description, so the result is

>> 1:-1

ans = Empty matrix: 1-by-0

If the matrix is empty, you might expect it to be “0-by-0,” but there you have it. In any case, if you loop over an empty range, the loop never runs at all, which is why in this example the value of total is zero for any negative value of n.

If you are sure that you will never make a mistake, and that the preconditions of your functions will always be satisfied, then you don’t have to check. But for the rest of us, it is dangerous to write a script, like this one, that quietly produces the wrong answer (or at least a meaningless answer) if the input value is negative. A better alternative is to use an if statement.

In document Physical Modeling in MATLAB (Page 40-45)

Related documents