Now we're going to explore some programs that draw fractals. Really to illustrate how simple programs not much more difficult than the ones that we just looked at, can bring us into a whole new world of understanding of really nature. So, let's look at these examples. We're going to start out with a very simple application. It's kind of a random game. So, the idea is we have a triangle with the vertices labeled zero, one, and two, and we're going to make zero the current point and that's got a circle on it. Then our process is going to be to pick a vertex at random and draw a point halfway between that vertex in the current point and then just repeat that. So, in this example, we start at 0,0 with vertex 0, and this is just a little table explaining the rules. So, with probability one-third and we're going to pick each one of the points as as the next point, and then these formulas give what we need to do if that happens to be the one picked. So, let's not worry about those so much. Let's just look at the process now. So, if we were at zero and we pick two, then that point's halfway between zero and two. Now, we pick one and then we move to a point halfway between zero and one. Again, these little equations here give us what we need to do to update. So, now two halfway there, zero halfway there. So, for zero, it's easy if we're up here then we just take half the x-coordinate, half the y-coordinate, and the others you just have to add a half or whatever else you need to do. So, we're just iterating this process. Looks pretty random. But let's take a look at a program that implements this and again pretty easy to do and then we can run it for a lot of points and see what happens. So, this is a program and we'll call it chaos for now, seems random. We're going to take the number of points that we want to run from the command line, we call that trials, the way that we usually do, and then we'll have these variables here to help us do the computation. So, we need the square root of three over two in the same way, and then these arrays give us the coordinates that we need in order to do the computation. Those are the points of the triangle to get started. So, we set the pen radius, started at zero, and then in a four loop that iterates the number of times given by trials which we found on the command line, we're going to compute a random integer zero, one or two, and then we're going to just update the x and y coordinates to go halfway between the current point and the point given by the arrays. Then once we get the updated x and y, we'll just plot it. So, again that's a very simple program easily implemented with standard draw. So, let's take a look at how we get random points within our triangles. So, this is plotting 10,000 points with that program. Actually, not random at all, doesn't take much for an amazing and interesting patterns to emerge. But looks random. So, what's going on, and this is introduction to the idea of fractals and computing with chaos that scientists are still studying from all kinds of points of view yet today and maybe you've seen this pattern before, it's called the Sierpinski triangle and there's many many different ways to look at it or see it even in nature. One thing that you can do is, if you just take the Pascal's triangle and you take the odd numbers, you get that pattern and maybe you've seen it in popular culture. You can build it with coke cans or with pennies or where you can make a cookie or candy corn. All of these are different realizations of this thing and this many applications in math and applied math as well. That's the three-dimensional version in high-def. So, with a simple random chaos program, we can get Sierpinski triangles. That's interesting, but if we change the rules, we can get even more interesting patterns. So, with the Sierpinski triangle or rules were with probability one third. We choose each point and so we can adjust those probabilities, and for the Sierpinski triangle, we always took the average of the current point and the next point to get halfway between, but you couldn't use any function that you want of the current point. So, this is just a different example. So, what happens if we write a program to change our rules to that. It's not much or very little much more difficult than the program that we just wrote, and there's a program in the book or on the book site that takes the coefficients from standard input and then runs the iteration, but otherwise, it's the same program that we just looked at. It's just that it takes its data usually from a file. So, here's an example of a file that's got the coefficients of how we're going to do the iteration, how to update this just really encoding those tables and numbers and you can study that program in the book. For lecture, the idea is simple program where we play that game, where we start at a point and we go randomly to another point. It's just that the transformation is dictated by a file. So, here's a file it's a little bit more complicated but not hugely more complicated and I still only three points and so forth, and let's run this one. This is a completely different route random pattern. Still from a pretty simple program. In this case, you while, you can tell from the name, it's an idea for modeling the behavior of coral in nature. So, there's something about this random process that seems to relate to what happens in the real world and actually scientists are interested in knowing simple rules that might govern what we observe in the real world, with this type of program, we can study that. Here's another example of changing the rules. This time there's four points, as one of them it's got a tiny probability, one of them that's really likely. Let's run that one. Yeah, it's pretty much the same program, we're just giving it slightly different data. You'd like to think that nature kind of works that way and many people do. There's a relatively small number of simple rules that have created the world around us. So, this one is creating something that looks a little bit like a fern. This one's called a Barnsley fern. So, and actually not just scientists but also people in the entertainment industry, use processes like this to build models of nature. So, they can make worlds and you experienced this all the time. In a video game or a movie nowadays, it's often the case that what you see is produced by a process like this. With simple iterative computations, we get patterns that are extremely similar to those that we find in the natural world. Also there's a fern just like the one that I just did except green and there's a real fern and can compare those. Is that real or is that generated. That one's actually real. This one, that's a real plant and that one's generated by an iterated function system, and it really brings up the question, what does computation tell us about nature or what does nature tell us about computation? Really profound questions that we get to with relatively simple programs using standard output and using a little bit of randomness. I'll make this point. Now, often throughout this course, in the 20th century, science was all about math models and formulas. A lot of people believe in the 21st century, science is really all about algorithms and programs to help us understand what's going on in the real world. Now, again, you've seen really lots of applications of iterated function systems in movies and games. Maybe you remember this one, that's an entire world. None of that's real. It's all built with technology like the iterated function systems that we just demonstrated.