Algorithmization and Programming ======================================= ========= FUNCTIONS ============= ======================================= each function accessible from the Command Window has to be stored at a file with the same name ========================================= ### Example 1 ---- write a matlab function for f(x) = (x^3-7x+6)/7 --- ================== == file f1.m === ================== function y = f1(x) % % function y = f1(x) % input: x ... an array % output: y = (x^3-7x+6)/7 % y = (x.^3-7.*x+6)/7; end ----- using the function f1 ------- >> help f1 >> f1(5) >> u = 0:0.1:4; >> v = f1(u); >> plot(u,v) ========================================= ### Example 2 ---- function computing volume and surface of a cuboid --- ======================= == file VolSurf.m === ======================= function [V, S] = VolSurf(a, b, c) % % [V, S] = VolSurf(a, b, c) % computes volume and surface of a cuboid % input: a, b, c ... size of cuboid edges % output: V ... the volume % S ... the surface % V = a*b*c; S = 2*(a*b + b*c + a*c); end --------- calling the function ------------- >> [p,q] = VolSurf(1,3,4) p = 12 q = 38 >> VolSurf(1,3,4) % returns the first value only ans = 12 --- parameters can be expressions >> a = 5; >> [u,v] = VolSurf(1/8, 3*a, a*sin(4)); --- Matlab command "help" can be used >> help VolSurf ========================================================== SCOPE OF THE VARIABLES function parameters are passed by VALUE, not by REFERENCE ========================================================== ### Example 3 ================= == file t.m === ================= % illustration of scope of the variables % clear all % all - clears also all functions a = 1; b = 2; fprintf('before f2: a=%d, b=%d\n', a, b); c = f2(b); % b ... actual parameter fprintf(' after f2: a=%d, b=%d, c=%d\n', a, b, c); disp(p) ================== == file f2.m === ================== function y = f2(a) % a ... formal parameter % % input: a ... a number % output: y = a^2 % % y = a^2; b = 7; a = a+11; p = 26; fprintf('inside f2: a=%d, b=%d, p=%d, y=%d\n', a, b, p, y); end ========================================================== ### Example 4 ************************************************ Explicit Euler's method for solving y' = f(x, y) ************************************************ f(x,y) = y / x^2, x_0 = 1, y_0 = 2 Solving in the Dialog window: ============================= >> h = 0.2; % choose a step-size >> x = 1; y = 2; % initial condition - starting point Repeat the following 2 steps: >> y = y + y / x^2 * h % new value of y (on the tangent line) >> x = x + h; % new value of x The same, using Anonymous function: =================================== definition of function f(x,y) - in the variable f will be so called Function Handle: >> f = @(x,y) y / x^2; >> h = 0.2; % choose a step-size >> x = 1; y = 2; % initial condition - starting point Repeat the following 2 steps: >> y = y + f(x,y) * h % new value of y (on the tangent line) >> x = x + h; % new value of x The same, using a script and a function: ========================================= =================== == file Eul.m === =================== clear all h = 0.2; % choose a step-size x = 1; y = 2; % initial condition - starting point for k=1:5 y = y + f(x,y) * h; % new value of y (on the tangent line) x = x + h; % new value of x disp(y); % print the new value on the screen end ================= == file f.m === ================= function z = f(x,y) % f(x,y) = y / x^2; z = y / x^2; end =================================== you can store auxiliary functions in the file with the main one (however, only the main function is available from outside): =================== == file Eul.m === =================== function y = Eul(x0, y0, h, n) % % input: % x0, y0 - initial condition (starting point) % h - a step-size % n - number of steps x = x0; y = zeros(1,n); y(1) = y0; for k=2:n y(k) = y(k-1) + f(x,y(k-1)) * h; x = x + h; end end function z = f(x,y) % f(x,y) = y / x^2; z = y / x^2; end --------------- --- calling the function Eul: >> p = Eul(1, 2, 0.2, 5) >> f(2,6) % error - the function is not accessible outside Eul ==================================== ==================================== Learn from Matlab/Octave programmers warning: DO NOT CHANGE Matlab FUNCTIONS - make a copy of your own, under different name >> edit trace >> edit range >> edit date ====================================