Algorithmization and Programming Examples from the 3-nd lecture ======================================= ========= FUNCTIONS ============= ======================================= ------------------------- === Introduction ======= ------------------------- >> t = date t = 07-Oct-2016 >> edit date function t = date %DATE Current date as date string. % S = DATE returns a string containing the date in dd-mmm-yyyy format. % % See also NOW, CLOCK, DATENUM. % Copyright 1984-2002 The MathWorks, Inc. % \$Revision: 1.15 \$ \$Date: 2002/04/15 03:20:04 \$ c = clock; mths = ['Jan';'Feb';'Mar';'Apr';'May';'Jun';'Jul'; 'Aug';'Sep';'Oct';'Nov';'Dec']; d = sprintf('%.0f',c(3)+100); t = [d(2:3) '-' mths(c(2),:) '-' sprintf('%.0f',c(1))]; >> c = clock c = 1.0e+03 * 2.0160 0.0100 0.0070 0.0100 0.0480 0.0202 >> help clock CLOCK Current date and time as date vector. ... CLOCK = [year month day hour minute seconds] The first five elements are integers. The seconds element is accurate to several digits beyond the decimal point. FIX(CLOCK) rounds to integer display format. >> mths = ['Jan';'Feb';'Mar';'Apr';'May';'Jun';'Jul'; 'Aug';'Sep';'Oct';'Nov';'Dec'] mths = Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec >> d = c(3)+100 d = 107 >> size(d) ans = 1 1 >> d = sprintf('%.0f',c(3)+100) d = 107 >> size(d) ans = 1 3 >> t = [d(2:3) '-' mths(c(2),:) '-' sprintf('%.0f',c(1))] t = 07-Oct-2016 =================================== each function has to be stored at a file with the same name ======================= == file my_date.m === ======================= function t = my_date % % s = my_date ... a string containing % the current date in dd.mm.yyyy format % % according to Matlab function "date" % Copyright 2013 certik % c = clock; mths = ['01';'02';'03';'04';'05';'06';'07'; '08';'09';'10';'10';'12']; d = sprintf('%.0f',c(3)+100); t = [d(2:3) '.' mths(c(2),:) '.' sprintf('%.0f',c(1))]; ================================ >> clear >> t = my_date t = 13.10.2016 >> c error: 'c' undefined near line 1 column 1 >> --------------------- === Examples ======= --------------------- ### Example 1 ---- write a matlab function for f(x) = x sin(x) --- file f1.m --------------------- function y = f1(x) % % function y = f1(x) % input: x ... an array % output: y = x*sin(x) % y = x.*sin(x); end ----- using the function f1 ------- >> help f1 >> f1(5) >> u = 1:0.1:pi; >> 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 ------------- >> help VolSurf >> [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 >> d=2; >> [p,q] = VolSurf(d, 2*d, sin(1.2)*d); ========================================================== ### Example 3 you can store auxiliary functions in the file with the main one (however, only the main function is available from outside): ----------------------------- content of a file BMI_main.m ----------------------------- function BMI_main % function asks for a height in cm and a weight in kg % and writes the BMI index and its interpretation h = input('give the height in cm: '); w = input('give the weight in kg: '); [id, k] = BMI(h/100, w); sid = sprintf('%.1f', id); switch k case 1 sk = 'too small'; case 2 sk = 'all right'; case 3 sk = 'too high'; case 4 sk = 'extremely high'; otherwise sk = ' ? an error occured at a function BMI'; end result = ['BMI index is ', sid, ' - for the given height, the weight is ', sk, '.']; disp(result) end function [idx, ca] = BMI(height, weight) % idx = BMI(height, weight) % % input: % height ... height in meters % weight ... weight in kg % % output: % idx ... BMI index: idx = weight/height^2 % ca ... category - for the given height, the weight is: % 1 - too little % 2 - OK % 3 - too much % 4 - extremely much idx = weight/height^2; if idx < 18.5 ca = 1; elseif idx < 25 ca = 2; elseif idx < 30 ca = 3; else ca = 4; end end ### Example 4 ************************************************ Explicit Euler's method for solving y' = f(x, y) ************************************************ f(x) = 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 4 steps: >> f = y / x^2; % f ... direction of the tangent line at [x, y] >> x = x + h; % new value of x >> y = y + f * h; % new value of y (on the tangent line) >> disp(y); % print the new value on the screen The same, using Anonymous function: =================================== >> f = @(x,y) y / x^2; % f(x,y) ... direction of the tangent line at [x, y] >> h = 0.2; % choose a step-size >> x = 1; y = 2; % initial condition - starting point Repeat the following 3 steps: >> 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 ===================================================== ===================================================== Passing a function as a parameter to another function ===================================================== ### Example 5 function S = integ(a, b, n, f) % % numerical integration of f on (a, b) % % input: % a , b ... endpoints of the interval % n ... the number of integration points inside (a, b) % f ... link to a function of one parameter % % output: % S ... integral from a to b of f % h = (b-a)/(n+1); % the integration step x = a:h:b; % the integration points fx = f(x); % function values at the integration points S = h * sum( fx(1:n+1)); % left Riemann sum end ================== >> integ(0, pi, 20, @sin) ans = 1.9963 >> g = @(x) x.^2 - 3*x g = @(x)x.^2-3*x >> integ(1, 3, 20, g) % exact value: -3.333... ans = -3.4255 >> ====================================================