User Tools

Site Tools


gibson:teaching:spring-2016:math445:lecture:forloop

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
gibson:teaching:spring-2016:math445:lecture:forloop [2016/02/15 09:45]
gibson [User-defined functions]
gibson:teaching:spring-2016:math445:lecture:forloop [2016/02/17 07:38] (current)
gibson
Line 1: Line 1:
-====== Math 445 lecture 7: the "for" loopfprintf ​======+====== Math 445 lecture 7: fprintf, ​for loopsfunctions ​======
  
-===== Formatted printing with fprintf =====+===== fprintf: formatted printing ​=====
  
 **fprintf** is Matlab'​s **formatted print function**. It's used for printing variables to the screen (or files) in a format that you specify. The basic syntax is **fprintf** is Matlab'​s **formatted print function**. It's used for printing variables to the screen (or files) in a format that you specify. The basic syntax is
  
 <code matlab> <code matlab>
-fprintf("format-string", variable, variable, variable, ...)+fprintf('format-string', variable, variable, variable, ...)
 </​code>​ </​code>​
  
Line 18: Line 18:
 </​code>​ </​code>​
  
-===== for loops =====+The format string contains two //slots// ''​%s''​ and ''​%w''​. The percent sign is a special marker that signals a slot, and the character following it specifies the type of variable that goes in the slot, ''​%s'' ​for string and ''​%d''​ for decimal. The values of the variables $w$ and $n$ are placed in those slots when the fprintf function is executed. The ''​\n''​ is a special //control character// for "new line." ​
  
 +The most important slot types are
  
 +  * ''​%d''​ decimal (integer), e.g. 5
 +  * ''​%f''​ floating-point,​ e.g. 2.75
 +  * ''​%e''​ floating-point in scientific (exponential) notation, e.g. 2.75e+00 for 2.75
 +  * ''​%s''​ string (a sequence of characters),​ e.g. ''​ '​banana'''​.
 +  * ''​%c''​ character, e.g. ''​ '​q'​ ''​
 +
 +The decimal and floating-point slots can be specialized to print numbers in particular ways, for example,
 +
 +<code matlab>
 +>> fprintf('​The value of pi is %8.3f\n',​ pi)
 +The value of pi is    3.142
 +</​code> ​
 +
 +This prints $\pi$ with 3 digits after the decimal in a fixed-width field of 8 characters.
 +
 +You can put as many slots in a format string as you like. Just provide as many variables as slots, and make sure the types of the slots match the variables.
 +
 +===== for loops: repeating sequences of commands=====
 +
 +**for** loops are used to repeat a sequence of commands a specified number of times. A **for** loop has an index variable (often $n$ or $i$) whose value changes over every iteration of the loop. For example, this Matlab **for** loop
 +
 +<code matlab>
 +for n=1:4
 +   ​fprintf('​The value of n is %d.\n',​n);​
 +end
 +</​code>​
 +
 +produces the output
 +
 +<​code>​
 +The value of n is 1.
 +The value of n is 2.
 +The value of n is 3.
 +The value of n is 4.
 +</​code>​
 +
 +Note how the //body// of the loop (the fprintf) statement is executed four times, once for each value of $n$ from 1 to 4. 
 +
 +===== functions =====
 +
 +The Matlab **function** command lets you define your own functions. For example, putting
 +the following code in a file named ''​quadratic.m''​
 +
 +<file matlab quadratic.m>​
 +function y = quadratic(x)
 +  y = x^2 + 3*x -7;
 +end
 +</​file>​
 +
 +defines a function named ''​quadratic''​ that computes the polynomial $y(x) = x^2 +3x -7$. The filename must match the name of the function. If the ''​quadratic.m''​ function is in Matlab'​s current working directory, you'll be able to execute the function just like a regular Matlab built-in function, for example
 +
 +<​code>​
 +>> quadratic(0)
 +ans =
 +    -7
 +
 +>> quadratic(1)
 +ans =
 +    -3
 + 
 +>> quadratic(12.74)
 +ans =
 +  193.5276
 +</​code>​
 +
 +===== example functions =====
 +
 +==== sum the components of a vector ====
 +This ''​mysum''​ function computes the sum of the components in the input vector $x$. There are a 
 +number of extraneous print statements which show how the calculation works, step-by-step.
 +<file matlab mysum.m>
 +function s = mysum(x);
 +% compute the sum of the components of vector x
 +
 +  s = 0;           % variable to hold partial sum
 +  n = length(x); ​  % get length of vector x
 + 
 +  fprintf('​Will need to do %d additions.\n\n',​ n);
 +  ​
 +  for i=1:n
 +    fprintf('​The current partial sum is s = %f.\n\n',​ s);
 +    pause
 +    fprintf('​Add %f.\n',​ x(i));
 +    s = s + x(i);
 +  end
 +  ​
 +  fprintf('​The total sum is s = %f.\n',​ s);
 +  pause
 +end
 +</​file>​
 +
 +You can execute this function with on the input vector ''​[4 2 9 1]]''​ like this
 +<​code>​
 +>> mysum([4 2 9 1])
 +Will need to do 4 additions.
 +
 +The current partial sum is s = 0.000000.
 +
 +Add 4.000000.
 +The current partial sum is s = 4.000000.
 +
 +Add 2.000000.
 +The current partial sum is s = 6.000000.
 +
 +Add 9.000000.
 +The current partial sum is s = 15.000000.
 +
 +Add 1.000000.
 +The total sum is s = 16.000000.
 +ans =
 +    16
 +>> ​
 +</​code>​
 +
 +The function is a lot easier to read if you take out the print statements
 +
 +<file matlab mysum.m>
 +function s = mysum(x);
 +% compute the sum of the components of vector x
 +
 +  s = 0;           % variable to hold partial sum
 +  n = length(x); ​  % get length of vector x
 +
 +  for i=1:n        % for each number i from 1 to n
 +    s = s + x(i);  %    add x(i) to the partial sum
 +  end
 + 
 +end
 +</​file>​
 +==== compute the factorial ====
 +
 +This function computes $n!$, the factorial of $n$, according to the formula
 +
 +\begin{eqnarray*}
 +n! = \prod_{i=1}^n i = 1\cdot 2 \cdot 3 \cdot \ldots \cdot (n-2) \cdot (n-1) \cdot n
 +\end{eqnarray*}
 +
 +<file matlab myfactorial.m>​
 +function p = myfactorial(n);​
 +% compute factorial of n = 1 * 2 * ... (n-2)*(n-1)*n
 +
 +  p = 1;   % variable to store partial products
 +  ​
 +  % each step through the loop does one more multiplication
 +  % in the sequence of products 1, 1*2, 1*2*3, etc.
 +  for i=1:n
 +    p = p * i;
 +  end
 +
 +end
 +</​file>​
gibson/teaching/spring-2016/math445/lecture/forloop.1455558306.txt.gz ยท Last modified: 2016/02/15 09:45 by gibson