====== Differences ====== This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
gibson:teaching:fall-2013:math445:lecture5functions [2013/09/10 12:54] gibson created |
gibson:teaching:fall-2013:math445:lecture5functions [2013/09/10 13:05] (current) gibson |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | The functions we wrote in class are provided below. Each has a description of what we're illustrating, the filename, and then the file contents. You can cut & paste as needed. | + | The functions we wrote in class are provided below. Each lists the filename, a description of what we're illustrating, and then the file contents. You can cut & paste as needed. |
- | An example of an if-else statement. | ||
- | isOdd.m | + | square.m : a super-simple example of a function that takes one argument and returns one value |
+ | <code> | ||
+ | function s = square(x); % declare a function named square | ||
+ | % square takes an argument x and returns its square, s=x*x | ||
+ | |||
+ | s = x*x; | ||
+ | |||
+ | end | ||
+ | </code> | ||
+ | |||
+ | |||
+ | pow.m : an example of a function that takes two arguments | ||
+ | <code> | ||
+ | function y = pow(x,n) % this function takes two arguments | ||
+ | % pow(x,n): return x to the n | ||
+ | |||
+ | y = x^n; % semicolon to suppress printing | ||
+ | |||
+ | end | ||
+ | </code> | ||
+ | |||
+ | |||
+ | square_cube.m : an example of a function with two return values | ||
+ | |||
+ | <code> | ||
+ | function [s,c] = square_cube(x); % example of multiple return values | ||
+ | % return the square and the cube of x | ||
+ | |||
+ | s = x*x; | ||
+ | c = s*x; | ||
+ | |||
+ | end | ||
+ | </code> | ||
+ | |||
+ | |||
+ | isOdd.m : an example of an if-else statement | ||
<code> | <code> | ||
function r = isOdd(n) | function r = isOdd(n) | ||
Line 16: | Line 50: | ||
- | A simpler example of an if-else statement | + | isTwo.m : a simpler example of an if-else statement |
- | + | ||
- | isTwo.m | + | |
<code> | <code> | ||
function r = isTwo(n); | function r = isTwo(n); | ||
Line 33: | Line 65: | ||
end | end | ||
</code> | </code> | ||
+ | |||
+ | printtoten.m : a simple example of a for-loop, with no arguments and no return value | ||
+ | <code> | ||
+ | function printtoten(); | ||
+ | % print the numbers from 1 to 10 | ||
+ | |||
+ | % example for loop | ||
+ | for n=1:10 | ||
+ | n | ||
+ | end | ||
+ | | ||
+ | end | ||
+ | </code> | ||
+ | |||
+ | |||
+ | sumtoten.m : a more meaningful example of a for-loop | ||
+ | <code> | ||
+ | function s = sumtoten(); | ||
+ | % return the sum of 1 through 10 | ||
+ | |||
+ | s = 0; % set s to zero | ||
+ | |||
+ | for n=1:10 | ||
+ | s = s+n; % add each number from 1 to 10 to s | ||
+ | end | ||
+ | |||
+ | end | ||
+ | </code> | ||
+ | |||
+ | |||
+ | matvecmult.m : a home-grown matrix-vector multiply function | ||
+ | <code> | ||
+ | function y = matvecmult(A,x); | ||
+ | % return y = A*x; | ||
+ | |||
+ | [m,n] = size(A); % get size of matrix | ||
+ | | ||
+ | y = zeros(m,1); | ||
+ | |||
+ | % Compute y(i) = sum A_ij x_j | ||
+ | | ||
+ | % "nested loops" | ||
+ | for i=1:m | ||
+ | for j=1:n | ||
+ | y(i) = y(i) + A(i,j)*x(j); | ||
+ | end | ||
+ | end | ||
+ | | ||
+ | end | ||
+ | </code> | ||
+ | |||
+ | |||
+ | |||