Grátis
433 pág.
Denunciar
Pré-visualização | Página 50 de 50
line of code. So, if you place try: above the equation set (indenting the equations!) 197 and except: after the equation set (with an indented pass below except:) we should be able to use any of these equations without the fear of generating an error. 9) We are going to take a closer look at the function z = z - sin(z)/cos(z) from exercise 8. The figure that represents this plot is found in figure Exercise 7a. Uncomment the equation for this function (commenting all others!) into your Newton’s method program and modify hcenter to hcenter = 1.55 and axrng to axrng = 0.50. These modifications should center and zoom into the interesting figure on the right side of the graphics window. Your plot should look something like the plot shown in figure Exercise 9a. Now set axrng = 0.30 and run the program again. You should see something like figure Exercise 9b. 10) We don’t have to use integer powers when using Newton’s Method. We can find the complex iteration of z3.5 – 1 just as easily as we can an integer power. Modify the equation line as follows (or simply add this equation to your list): z = z - (z**3.5 - 1)/(3.5*z**2.5) Using the color code block from the original Newton’s Method program, we will get a plot similar to the one shown in figure Exercise 10a. A somewhat more interesting plot can be found by comment out the original color code block (as we did in exercise 4) and substituting this color assignment prior to the glVertex2f statement: if z.imag < 0: glColor3f(sin(n),cos(n),sin(z.real)) else: glColor3ub(5*abs(z),cos(z.real),1.5*abs(z)) The resulting plot is found in figure Exercise 10b. I didn’t say it was pretty! You can also try other z equations (with any color scheme) such as: z = z - (z**4.5 - 1)/(4.5*z**3.5) z = z – (z**4.8 – 1)/(4.8*z**3.8) z = z – (z**3.75 – 1)/(3.75*z**2.75) By now you may have some idea of how to find the derivative of a simple polynomial function? If so, you might try making up some of your own z equations. 11) Here is a "small" list of equations you may want to try in your program. Make certain that you comment out every equation except the one you want to try! # Newton's Method Equation #z = z - (sin(z)/cos(z) - 1)/(1/((cos(z)*cos(z)))) #z = z - (z**4 + .84*z**2 - .16)/(4*z**3 + 1.68*z) #z = z - (z**3 - 1)/(3*z**2) #z = z - sin(z)/cos(z) z = z - (z**5 - 1)/(5*z**4) #z = z - log(z)/(1/z) #z = z - (z**3 + z**2 + z - 2)/(3*z**2 + 2*z + 1) #z = z - (z**4 + .84*z**3 + .16*z - 2)/(4*z**3 + 2.52*z**2 + .16) #z = z - (z**4 + .84*z**2 - .16)/(4*z**3 + 1.68*z) 198 #z = z - (z**3 - 5*z)/(3*z**2 - 5) #z = z - (sin(z*z) - z*z + 1)/(2*z*cos(z*z) - 2*z) #z = z - (z**3.7 - 1)/(3.7*z**2.7) #z = z - (sin(z*z) - z*z*z + cos(z*z*z))/(2*z*cos(z*z) - 3*z*z - 3*z*z*sin(z*z*z)) #z = z - (z**z - 3**z - z**3 - 1)/((1+log(z))*z**z - log(3)*3**z - 3*z**2) Can you tell which equation we are trying to plot? One final modification you might try is to uncomment TWO equations at the same time. What will happen? I don't know... try it! Here's what I THINK will happen. z will be calculated using the first equation and then the complex value for z will be "fed" to the second equation for calculation/iteration. Certainly the plot will take longer to create. What is uncertain is how interesting the final result will be. This is an excellent opportunity for you to experiment! 12) There are some excellent freeware fractal programs available online. I would recommend that you Google for “fractals” and “freeware” and see what you can find. Some of the current programs that you might find very interesting are “Xaos”, “Chaospro”, and “Fractint”. There may be others as well. What we are programming here can hardly be termed a professional application, but hopefully you will get the flavor of fractals and find them interesting enough to explore them further on your own. Another interesting program is Gnofract 4D, which runs on linux. You also should definitely look up "Newton's Method Fractals" and see what you find. You may be able to take the information you find online and convert the equations to a Python program. Give yourself extra credit if you can do so! 13) One of the most important aspects of a computer program is interactivity. In a future section we will explore mouse interaction with our graphics window. In this exercise we are going to add some additional keyboard options. Until now, in order to view a different Newton's Method fractal we had to comment and uncomment specific equations. What if we could simply press a number key and have the equations automatically change for us? Here's how we might do that. First, add a new global variable, global newtfrac, to the variable section at the beginning of the program and set its initial value equal to 1 (How?). Add the following def keyboard function to your program: def keyboard(key, x, y): global newtfrac if key == chr(27) or key == "q": sys.exit() else: newtfrac = eval(key) if newtfrac > 0 and newtfrac < 10: glutPostRedisplay() Since we are going to change the value of the newtfrac variable, we need to declare it as global in this def keyboard function. The program will end if we press the "q" or "Esc" keys. If we press any other key, its numeric value will be stored in newtfrac by the eval(key) function. If this value is between 0 and 10 (in other words, 1 through 9), then we update the display. Note: What statement must we add to def main() in order for the keyboard function to work? The real 199 work is done in the display function. Modify your display function by changing/adding the following: while n < 1000 and endit == 0: n+=1 old = z # Newton's Method Equation try: if newtfrac == 1: z = z - (z**3 - 1)/(3*z**2) elif newtfrac == 2: z = z - (z**4 + .84*z**2)/(4*z**3 + 1.68*z) elif newtfrac == 3: z = z - (z**5.4-1)/(5.4*z**4.4) elif newtfrac == 4: z = z - sin(z)/cos(z) elif newtfrac == 5: z = z - (z**5 - 1)/(5*z**4) elif newtfrac == 6: z = z - log(z)/(1/z) elif newtfrac == 7: z = z - (z**3.7 - 1)/(3.7*z**2.7) elif newtfrac == 8: z = z - (3**z - 1)/(log(3)*3**z) else: z = z - (z**3 - 5*z)/(3*z**2 - 5) except: pass if abs(z - old) < 0.000001: endit = 1 The above is not completely new, obviously. Simply modify the appropriate section of code in the def drawnewton() function to match what you see here. Run the program. Once the original Newton's Method fractal has finished drawing, press any number key other than 1 or 0 and see what happens. How does this work? Look carefully at the if...elif...else code block and see if you can understand the logic. How does this particular block of code work with the keyboard function? Why did I not need to specifically check for the number "9"? If 9 works, why doesn't the number "0" trigger the else statement? 200 Exercise 1 Exercise 2 Exercise 3 Exercise 4 Exercise 5a Exercise 5b 201 Exercise 5c Exercise 6 Exercise 7a Exercise 7b Exercise 7c Exercise 7d 202 Exercise 7e Exercise 7f Exercise 9a Exercise 9b Exercise 10a Exercise 10b 203 Addendum: I want to encourage you to explore some of these (and other) fractals on your own. Try zooming in on various locations and see what happens. If you generate an error message, what might be causing the error? Are you dividing by zero? Figure glitch on the next page illustrates another type