This preview shows pages 1–4. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 14:440:127 Introduction to Computers for Engineers Notes for Lecture 9 Rutgers University, Fall 2008 Instructor Blase E. Ur Dont forget that Exam 2 runs 11/5  11/11. Whats the best way to study? DOING the recitation problems, and then looking up the answers if you get stuck. 1 Review Loops 1.1 For Loops Sometimes, youll know exactly how many times you need to perform some action. Take the fol lowing example: Display the numbers from 1 to 100, individually. Rather than type ( disp(1); disp(2); disp(3)...) and so on, you could just use a for loop. A for loop lets you specify that Matlab perform some repetitive action over and over, changing one variable each time. Heres the syntax: for VARIABLE = VECTOR % replace capitalized parts STATEMENTS % replace capitalized parts end for x = 1:100 disp(x) end The first time you go through the loop, x will be 1. The next time you go through the loop, x will be 2, and so on, until the loop runs one final time with x equal to 100. Note that when youre writing a for loop and youre not sure how to write it, try writing out each statement individually and look for the pattern: v(1)=1; %fibonacci v(2)=1; v(3)=v(2)+v(1); v(4)=v(3)+v(2); v(5)=v(4)+v(3); % What is the pattern? for x=3:100 v(x)=v(x1)+v(x2); end Whenever youre using a loop to find any sort of sum, youll need to have some variable that keeps track of the running total. Here, that variable is s , and this for loop sums the odd numbers from 1 to 99 s = 0; for z = 1:3:99 s = s+z; end disp(z) Of course, if statements (or any other Matlab statements youve seen) can be used in a loop. i.e. if you wanted to sum all of the prime numbers from 1 to 1000 using a loop, you could do the following: s = 0; for x = 1:1000 if(isprime(x)) s = s+x; end end 1.2 While Loops While a for loop lets you repeat some segment of code a specified number of times, a while loop lets you repeat some segment of code until a particular condition is false. While the condition is true, Matlab will loop through that code. Heres the syntax for a while loop: while(CONDITION) % replace capitalized parts STATEMENTS % replace capitalized parts end Example: Use a while loop to keep generating random numbers until you find one that is between .5 and .7. Also, display how many numbers you had to generate: n=rand(1); count=1; while(n<0.5  n>0.7) n=rand(1); count=count+1; end disp(n) disp(count) Example: If you add the integers 1 through n, what is the smallest n for which the sum is a 3 digit number? s = 0; n=1; while(s<100) s = s+n; n=n+1; end fprintf(The smallest n is %.0f,n1) Now, lets see how to write a for loop as a while loop . Theres a simple 3 step process! As our example, well use a for loop that displays all 4 digit twin primes (numbers separated by two that are both prime) for x = 1001:2:9997 if(isprime(x) & isprime(x+2)) fprintf(%.0f and %.0f are both prime\n,x,x+2) end end...
View
Full
Document
 Fall '08
 Finch

Click to edit the document details