{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

SpringLec1

# SpringLec1 - Lab 1 Loops Matrices and Fractals To iterate...

This preview shows pages 1–3. Sign up to view the full content.

Lab 1: Loops, Matrices and Fractals To iterate is to apply the same rule over and over again. Though simple to say and easy to code this can produce some amazing patterns, e.g., eye balls, palm trees and galaxies. To get started let us consider a few basic examples. Consider a population of rabbits that multiplies in size by a factor of 6 every spring. If we begin with 10 rabbits and iterate with this rule, the population of rabbits will grow as: 10, 60, 360, 2160, 12960 .... Not an amazing pattern, but it’s a start. If we instead have a species with cannibalistic tendencies (like black widow spiders) whose population size is halved each year, beginning with 128 spiders and applying the rule, we will arrive at 64, 32, 16, 8, 4, 2, 1 spiders after each year. Again, nothing earth-shattering but it’s a simple example of rule iteration that can be coded with just a few lines in Matlab : >> num_years = 7; >> num_spiders = 128; >> for i=1:num_years num_spiders = num_spiders*(0.5) end The first few lines of output we will see in the MATLAB command window are: num_spiders = 64 num_spiders = 32 num_spiders = 16 ... What does the above code mean in English? Exactly what you think it does! First we set num years to 7 (the number of years we are going to compute the spider population) and num spiders to 128 (the starting population size). The next three lines is the actual rule iteration where we start killing off some spiders. The general structure: for i=1:n commands .... end known as a for loop , tells Matlab to do everything in ”commands” 1 to n times. In our for loop we tell MATLAB to update the number of spiders. This process happens 7 times since that is what num years is set to. 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Numerical results are nice, but pictures are more fun to look at and often more informa- tive. A graph can be an excellent way of summarizing results. MATLAB has many tools for data visualization, some of which we will explore in this course. To produce a graph of the spider population vs. year, we can add a few lines of code to our initial commands: >> num_years = 7; >> num_spiders = 128; >> figure(1) >> hold on >> for i=1:num_years num_spiders = num_spiders*(0.5); plot(i,num_spiders,’bx’,’MarkerSize’,12,’LineWidth’,4) end >> title(’Size of Black Widow Spider Population’) >> xlabel(’Year’) >> ylabel(’Number of Spiders’) >> hold off The resulting graph is shown in Figure 1 1 2 3 4 5 6 7 0 10 20 30 40 50 60 70 Size of Black Widow Spider Population Year Number of Spiders Figure 1: A plot of the size of a black widow spider population over several years.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern