SpringLec1

SpringLec1 - 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

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 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 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.Figure 1: A plot of the size of a black widow spider population over several years....
View Full Document

This note was uploaded on 01/26/2011 for the course CAAM 210 taught by Professor Steve during the Spring '10 term at Rice.

Page1 / 7

SpringLec1 - 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

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online