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/16 11:51]
gibson [compute the factorial]
gibson:teaching:spring-2016:math445:lecture:forloop [2016/02/17 07:38] (current)
gibson
Line 24: Line 24:
   * ''​%d''​ decimal (integer), e.g. 5   * ''​%d''​ decimal (integer), e.g. 5
   * ''​%f''​ floating-point,​ e.g. 2.75   * ''​%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'''​.   * ''​%s''​ string (a sequence of characters),​ e.g. ''​ '​banana'''​.
   * ''​%c''​ character, e.g. ''​ '​q'​ ''​   * ''​%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: repeating sequences of commands=====
  
Line 76: Line 89:
 ===== example functions ===== ===== example functions =====
  
-====mysum====+==== sum the components of a vector ​====
 This ''​mysum''​ function computes the sum of the components in the input vector $x$. There are a  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. number of extraneous print statements which show how the calculation works, step-by-step.
gibson/teaching/spring-2016/math445/lecture/forloop.1455652275.txt.gz ยท Last modified: 2016/02/16 11:51 by gibson