In the following diary, we call functions defined in files of the same name. These functions are provided in the Lecture table on the course website
% How to call the square function, defined in file square.m square(4) ans = 16 square(5) ans = 25 % Note that "help square" returns the help string we wrote in the % the line after the function declaration in square.m help square square takes an argument x and returns its square, s=x*x % Here's help for the pow(x) function we wrote help pow pow(x,n): return x to the n % and an example of use pow(4,3) ans = 64 % Our square_cube function has *two* return values. help square_cube [s,c] = square_cube(x): return the square and the cube of x % If you run it without specifying variables for both return values, % Matlab drops the second and just gives you the first square_cube(3) ans = 9 % To get both return values, run it like this [s,c] = square_cube(3) s = 9 c = 27 % Example run of printtoten function printtoten n = 1 n = 2 n = 3 n = 4 n = 5 n = 6 n = 7 n = 8 n = 9 n = 10 % Example run of sumtoten function sumtoten() ans = 55 % Example run of our matvecmult function A = rand(3,3); x = rand(3,1); A A = 0.8147 0.9134 0.2785 0.9058 0.6324 0.5469 0.1270 0.0975 0.9575 x x = 0.9649 0.1576 0.9706 y = matvecmult(A,x) y = 1.2004 1.5045 1.0673 y = A*x y = 1.2004 1.5045 1.0673 % Hooray, our matvecmult(A,x) produces the same result as matlab's A*x! % You can also execute control flow statements at the matlab command prompt s=0; for i=1:10 s = s + i; end s s = 55 % You can put that all on one line s = 0; for i=1:10; s = s+ i; end s s = 55