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 Don’t forget that Exam 2 runs 11/5  11/11. What’s 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, you’ll 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. Here’s 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 you’re writing a for loop and you’re 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 you’re using a loop to find any sort of sum, you’ll 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 you’ve 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. Here’s 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, let’s see how to write a for loop as a while loop . There’s a simple 3 step process! As our example, we’ll 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
This note was uploaded on 11/09/2011 for the course ENGINEERIN 127 taught by Professor Finch during the Fall '08 term at Rutgers.
 Fall '08
 Finch

Click to edit the document details