16 Chapter 1.Computers and Programs print x x=3.9*X*(1-x) print x x=3.9*X*(1-x) print x x=3.9*X*(1-x) print x x=3.9*x*(1-x) print x x=3.9*X*(1-x) print x x=3.9*X*(1-x) print x x=3.9*X*(1-x) print x Obviously,using the loop instead saves the programmer a lot of trouble But what exactly do these statements do?The first one performs a calcula- tion. x=3.9*X*(1-x) This is called an assignment statement.The part on the right side of the =is a mathematical expression.Python uses the character to indicate multiplication. Recall that the value of x is 0.25 (from the input statement).The computed value is 3.9(0.25)(1-0.25)or 0.73125.Once the value on the righthand side is computed,it is stored back (or assigned)into the variable that appears on the lefthand side of the =in this case x.The new value of x (0.73125)replaces the old value(0.25). The second line in the loop body is a type of statement we have encountered before,a print statement. print x When Python executes this statement the current value of x is displayed on the screen.So,the first number of output is 0.73125. Remember the loop executes 10 times.After printing the value of x,the two statements of the loop are executed again X=3.9*x*(1-x) print x
16 Chapter 1. Computers and Programs print X X = 3.9 * X * (1 - x) print X X = 3.9 * X * (1 - x) print X X = 3.9 * X * (1 - x) print X X = 3.9 * X * (1 - x) print X X = 3.9 * X * (1 - x) print X X = 3.9 * X * (1 - x) print X X = 3.9 * X * (1 - x) print X Obviously, using the loop instead saves the programmer a lot of trouble. But what exactly do these statements do? The first one performs a calculation. X = 3.9 * X * (1 - x) This is called an assignment statement. The part on the right side of the = is a mathematical expression. Python uses the * character to indicate multiplication. Recall that the value of x is 0.25 (from the input statement). The computed value is 3.9(0. 25)(1 - 0.25) or 0.73125. Once the value on the righthand side is computed, it is stored back (or assigned) into the variable that appears on the lefthand side of the =, in this case x. The new value of x (0.73125) replaces the old value (0.25). The second line in the loop body is a type of statement we have encountered before, a print statement. print X When Python executes this statement the current value of x is displayed on the screen. So, the first number of output is 0.73125. Remember the loop executes 10 times. After printing the value of x, the two statements of the loop are executed again. X = 3.9 X * (1 - x) print X
1.8.Chaos and Computers 17 Of course,now x has the value 0.73125,so the formula computes a new value of xas3.9(0.73125)(1-0.73125),which is0.76644140625. Can you see how the current value of x is used to compute a new value each time around the loop?That's where the numbers in the example run came from. You might try working through the steps of the program yourself for a different input value(say 0.5).Then run the program using Python and see how well you did impersonating a computer. 1.8 Chaos and Computers I said above that the chaos program illustrates an interesting phenomenon. What could be interesting about a screen full of numbers?If you try out the program for yourself,you'll find that,no matter what number you start with, the results are always similar:the program spits back 10 seemingly random numbers between 0 and 1.As the program runs,the value of x seems to jump around,well,chaotically. The function computed by this program has the general form:k()(1-z), where k in this case is 3.9.This is called a logistic function.It models cer- tain kinds of unstable electronic circuits and is also sometimes used to predict population under limiting conditions.Repeated application of the logistic func- tion can produce chaos.Although our program has a well defined underlying behavior,the output seems unpredictable. An interesting property of chaotic functions is that very small differences in the initial value can lead to large differences in the result as the formula is repeatedly applied.You can see this in the chaos program by entering numbers that differ by only a small amount.Here is the output from a modified program that shows the results for initial values of 0.25 and 0.26 side by side: input 0.25 0.26 0.731250 0.750360 0.766441 0.730547 0.698135 0.767707 0.821896 0.695499 0.570894 0.825942 0.955399 0.560671 0.166187 0.960644 0.540418 0.147447
1.8. Chaos and Computers 17 Of course, now x has the value 0.73125, so the formula computes a new value of X as 3.9(0.73125)(1 - 0.73125), which is 0.76644140625. Can you see how the current value of x is used to compute a new value each time around the loop? That's where the numbers in the example run came from. You might try working through the steps of the program yourself for a different input value (say 0.5). Then run the program using Python and see how well you did impersonating a computer. 1.8| Chaos and Computers I said above that the chaos program illustrates an interesting phenomenon. What could be interesting about a screen full of numbers? If you try out the program for yourself, you'll find that, no matter what number you start with, the results are always similar: the program spits back 10 seemingly random numbers between and 1. As the program runs, the value of x seems to jump around, well, chaotically. The function computed by this program has the general form: k{x){l - x), where k in this case is 3.9. This is called a logistic function. It models cer- tain kinds of unstable electronic circuits and is also sometimes used to predict population under limiting conditions. Repeated application of the logistic function can produce chaos. Although our program has a well defined underlying behavior, the output seems unpredictable. An interesting property of chaotic functions is that very small differences in the initial value can lead to large differences in the result as the formula is repeatedly applied. You can see this in the chaos program by entering numbers that differ by only a small amount. Here is the output from a modified program that shows the results for initial values of 0.25 and 0.26 side by side: input 0.25 0. 26 0.731250 0.750360 0.766441 0.730547 0.698135 0.767707 0.821896 0.695499 0.570894 0.825942 0.955399 0.560671 0.166187 0.960644 0.540418 0.147447
18 Chapter 1.Computers and Programs 0.968629 0.490255 0.118509 0.974630 With very similar starting values,the outputs stay similar for a few iterations, but then differ markedly.By about the fifth iteration,there no longer seems to be any relationship between the two models. These two features of our chaos program,apparent unpredictability and ex- treme sensitivity to initial values,are the hallmarks of chaotic behavior.Chaos has important implications for computer science.It turns out that many phe- nomena in the real world that we might like to model and predict with our computers exhibit just this kind of chaotic behavior.You may have heard of the so-called butterfly effect.Computer models that are used to simulate and predict weather patterns are so sensitive that the effect of a single butterfly flap- ping its wings in New Jersey might make the difference of whether or not rain is predicted in Peoria. It's very possible that even with perfect computer modeling,we might never be able to measure existing weather conditions accurately enough to predict weather more than a few days in advance.The measurements simply can't be precise enough to make the predictions accurate over a longer time frame. As you can see,this small program has a valuable lesson to teach users of computers.As amazing as computers are,the results that they give us are only as useful as the mathematical models on which the programs are based.Com- puters can give incorrect results because of errors in programs,but even correct programs may produce erroneous results if the models are wrong or the initial inputs are not accurate enough. 1.9 Chapter Summary This chapter has introduced computers,computer science,and programming. Here is a summary of some of the key concepts: A computer is a universal information-processing machine.It can carry out any process that can be described in sufficient detail.A description of the sequence of steps for solving a particular problem is called an algorithm. Algorithms can be turned into software(programs)that determines what the hardware (physical machine)can and does accomplish.The process of creating software is called programming
18 Chapter 1. Computers and Programs 0.968629 0.490255 0.118509 0.974630 With very similar starting values, the outputs stay similar for a few iterations, but then differ markedly. By about the fifth iteration, there no longer seems to be any relationship between the two models. These two features of our chaos program, apparent unpredictability and ex- treme sensitivity to initial values, are the hallmarks of chaotic behavior. Chaos has important implications for computer science. It turns out that many phenomena in the real world that we might like to model and predict with our computers exhibit just this kind of chaotic behavior. You may have heard of the so-called butterfly effect. Computer models that are used to simulate and predict weather patterns are so sensitive that the effect of a single butterfly flapping its wings in New Jersey might make the difference of whether or not rain is predicted in Peoria. It's very possible that even with perfect computer modeling, we might never be able to measure existing weather conditions accurately enough to predict weather more than a few days in advance. The measurements simply can't be precise enough to make the predictions accurate over a longer time frame. As you can see, this small program has a valuable lesson to teach users of computers. As amazing as computers are, the results that they give us are only as useful as the mathematical models on which the programs are based. Computers can give incorrect results because of errors in programs, but even correct programs may produce erroneous results if the models are wrong or the initial inputs are not accurate enough. 1.9| Chapter Summary This chapter has introduced computers, computer science, and programming. Here is a summary of some of the key concepts: • A computer is a universal information-processing machine. It can carry out any process that can be described in sufficient detail. A description of the sequence of steps for solving a particular problem is called an algorithm. Algorithms can be turned into software (programs) that determines what the hardware (physical machine) can and does accomplish. The process of creating software is called programming
1.9.Chapter Summary 19 Computer science is the study of what can be computed.Computer sci- entists use the techniques of design,analysis,and experimentation.Com- puter science is the foundation of the broader field of computing which includes areas such as networking,databases,and information manage- ment systems,to name a few. A basic functional view of a computer system comprises a central process- ing unit(CPU),main memory,secondary memory,and input and output devices.The CPU is the brain of the computer that performs simple arith- metic and logical operations.Information that the CPU acts on(data and programs)is stored in main memory (RAM).More permanent information is stored on secondary memory devices such as magnetic disks and optical devices.Information is entered into the computer via input devices,and output devices display the results. Programs are written using a formal notation known as a programming language.There are many different languages,but all share the property of having a precise syntax(form)and semantics (meaning).Computer hardware only understands a very low-level language known as machine language.Programs are usually written using human-oriented high-level languages such as Python.A high-level language must either be compiled or interpreted in order for the computer to understand it.High-level lan- guages are more portable than machine language. Python is an interpreted language.One good way to learn about Python is to use the interactive interpreter for experimentation. A Python program is a sequence of commands(called statements)for the Python interpreter to execute.Python includes statements to do things such as print output to the screen,get input from the user,calculate the value of a mathematical expression,and perform a sequence of statements multiple times (loop). A mathematical model is called chaotic if very small changes in the input lead to large changes in the results,making them seem random or un- predictable.The models of many real-world phenomena exhibit chaotic behavior,which places some limits on the power of computing
1.9. Chapter Summary 19 Computer science is the study of what can be computed. Computer sci- entists use the techniques of design, analysis, and experimentation. Computer science is the foundation of the broader field of computing which includes areas such as networking, databases, and information management systems, to name a few. A basic functional view of a computer system comprises a central processing unit (CPU), main memory, secondary memory, and input and output devices. The CPU is the brain of the computer that performs simple arithmetic and logical operations. Information that the CPU acts on (data and programs) is stored in main memory (RAM). More permanent information is stored on secondary memory devices such as magnetic disks and optical devices. Information is entered into the computer via input devices, and output devices display the results. Programs are written using a formal notation known as a programming language. There are many different languages, but all share the property of having a precise syntax (form) and semantics (meaning). Computer hardware only understands a very low-level language known as machine language. Programs are usually written using human-oriented high-level languages such as Python. A high-level language must either be compiled or interpreted in order for the computer to understand it. High-level languages are more portable than machine language. Python is an interpreted language. One good way to learn about Python is to use the interactive interpreter for experimentation. A Python program is a sequence of commands (called statements) for the Python interpreter to execute. Python includes statements to do things such as print output to the screen, get input from the user, calculate the value of a mathematical expression, and perform a sequence of statements multiple times (loop). A mathematical model is called chaotic if very small changes in the input lead to large changes in the results, making them seem random or unpredictable. The models of many real-world phenomena exhibit chaotic behavior, which places some limits on the power of computing
20 Chapter 1.Computers and Programs 1.10 Exercises Review Questions True/False 1.Computer science is the study of computers. 2.The CPU is the "brain"of the computer. 3.Secondary memory is also called RAM. 4.All information that a computer is currently working on is stored in main memory. 5.The syntax of a language is its meaning,and semantics is its form. 6.A function definition is a sequence of statements that defines a new com- mand. 7.A programming environment refers to a place where programmers work. 8.A variable is used to give a name to a value so it can be referred to in other places. 9.A loop is used to skip over a section of a program. 10.A chaotic function describes things that are random and unpredictable. Multiple Choice 1.What is the fundamental question of computer science? a)How fast can a computer compute? b)What can be computed? c)What is the most effective programming language? d)How much money can a programmer make? 2.An algorithm is like a a)newspaper b)venus flytrap c)drum d)recipe 3.A problem is intractable when a)you cannot reverse its solution b)it involves tractors
20 Chapter 1. Computers and Programs 1,10| Exercises Review Questions True/ False 1. Computer science is the study of computers. 2. The CPU is the "brain" of the computer. 3. Secondary memory is also called RAM. 4. All information that a computer is currently working on is stored in main memory. 5. The S3mtax of a language is its meaning, and semantics is its form. 6. A function definition is a sequence of statements that defines a new com- mand. 7. A programming environment refers to a place where programmers work. 8. A variable is used to give a name to a value so it can be referred to in other places. 9. A loop is used to skip over a section of a program. 10. A chaotic function describes things that are random and unpredictable. Multiple Choice 1 . What is the fundamental question of computer science? a) How fast can a computer compute? b) What can be computed? c) What is the most effective programming language? d) How much money can a programmer make? 2. An algorithm aigontnm is like iK a a) newspaper b) venus flytrap c) drum d) recipe 3. A problem is intractable when a) you cannot reverse its solution b) it involves tractors