Defining Functions and Procedures 1. You can define the real valued function f whose rule is f(x)=x^2: > f := x-> x^2; 2 f := x -> x > f(2); 4 > f(a+b); 2 (a + b) > expand("); 2 2 a + 2 a b + b > (f(x+h)-f(x))/h; 2 2 (x + h) - x ------------- h > simplify("); 2 x + h 2. Create a procedure using proc to define more complicated real-valued functions: > f := proc(x) > if x > 3 then x^2 > else x-5 fi > end; f := proc(x) if 3 < x then x^2 else x - 5 fi end > f(2); -3 > f(5); 25 > plot(f); > plot(f, style=POINT); Unassign the definition of f: > f := 'f'; f := f Functions of Several Variables > f := (x,y) -> x^2 + y^2 -3; 2 2 f := (x, y) -> x + y - 3 > f(2,5); 26 Such functions can be ploted in three-dimensions: > plot3d(f, -2..2, -2..2); Automating Commands Frequently, you will want to repeat some command or set of commands several times with slightly different values. You do this with a looping structure using for and do commands. 1. Print a table of values: > for k from 1 to 3 > do > print(k, k^2); > od; 1, 1 2, 4 3, 9 > for k from 1 by .5 to 3 > do > print(k,k^2); > od; 1, 1 1.5, 2.25 2.0, 4.00 2.5, 6.25 3.0, 9.00 >