1 / 20

4. Iteration

4. Iteration. The idea of Repetition The for-loop construct. Motivating Problem: Computing Square Roots. Given a positive number A, find its square root. A Geometric Restatement. Given a positive number A, find a square whose area is A. An Initial Guess. W = 1. L = A.

iris-jensen
Download Presentation

4. Iteration

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 4. Iteration The idea of Repetition The for-loop construct

  2. Motivating Problem:Computing Square Roots Given a positive number A, find its square root.

  3. A Geometric Restatement Given a positive number A, find a square whose area is A.

  4. An Initial Guess W = 1 L = A How can we make this rectangle “ more square”?

  5. Observation s The answer is in between L and W: W < s < L

  6. Idea: W L L1 = (L+W)/2 W1 = A/L1 W1 L1

  7. Repeat: W1 L1 L2 = (L1+W1)/2 W2 = A/L2

  8. A Script A = input(‘A:’); L0 = A; W0 = A/L0; L1 = (L0 + W0)/2; W1 = A/L1; L2 = (L1 + W1)/2; W2 = A/L2; L3 = (L2 + W2)/2; W3 = A/L3; L4 = (L3 + W3)/2; W4 = A/L4;

  9. A Modified Script A = input(‘A:’); L = A; W = A/L; L = (L + W)/2; W = A/L; L = (L + W)/2; W = A/L; L = (L + W)/2; W = A/L; L = (L + W)/2; W = A/L;

  10. Handling the Repetition A = input(‘A:’); L = A; W = A/L; for k=1:4 L = (L + W)/2; W = A/L; end

  11. More General A = input(‘A:’); nSteps = input(‘nSteps:’); L = A; W = A/L; for k=1:nSteps L = (L + W)/2; W = A/L; end

  12. To repeat something N times: N = _____ for i = 1:N end Put the something here.

  13. To repeat something N times: N = _____ for i = 1:N end Put the something here. The Loop “body”

  14. To repeat something N times: N = _____ for i = 1:N end The “count variable” Put the something here. The Loop “body”

  15. Another Example for k = 1:10 x = rand; fprintf(‘%10.6f\n’,x) end Displays 10 random numbers.

  16. Built-In Function rand The statement x = rand assigns a “random” number between 0 and 1 to the variable x.

  17. Another Example for k = 1:10 x = rand; fprintf(‘%10.6f\n’,x)) end Displays 10 random numbers.

  18. E.g., 0.579736 0.609194 0.256451 0.246079 0.149936 0.564178 0.027311 0.790830 0.437630 0.997130

  19. Simulation Using rand Question: A stick with unit length is split into two parts. The breakpoint is randomly selected. On average, how long is the shorter piece?

  20. s = 0; for k=1:1000 % Break the k-th stick x = rand; if x<=.5 % Shorter part has length x s = s+x; else % Shorter part has length 1-x s = s+(1-x); end end ave = s/1000

More Related