% We opened a new function m-file with the % file menu and created sumavg.m help SumAvg SumAvg(a,b) returns the sum and average of the input arguments a and b. % If we do not see the documentation for % our script, we may have to set the path, % adding the location of the save .m file % to the path. path MATLABPATH C:\Users\janv\Documents\MATLAB C:\Program Files\MATLAB\R2009b\toolbox\matlab\general C:\Program Files\MATLAB\R2009b\toolbox\matlab\ops C:\Program Files\MATLAB\R2009b\toolbox\matlab\lang C:\Program Files\MATLAB\R2009b\toolbox\matlab\elmat C:\Program Files\MATLAB\R2009b\toolbox\matlab\randfun C:\Program Files\MATLAB\R2009b\toolbox\matlab\elfun C:\Program Files\MATLAB\R2009b\toolbox\matlab\specfun C:\Program Files\MATLAB\R2009b\toolbox\matlab\matfun C:\Program Files\MATLAB\R2009b\toolbox\matlab\datafun C:\Program Files\MATLAB\R2009b\toolbox\matlab\polyfun C:\Program Files\MATLAB\R2009b\toolbox\matlab\funfun C:\Program Files\MATLAB\R2009b\toolbox\matlab\sparfun C:\Program Files\MATLAB\R2009b\toolbox\matlab\scribe C:\Program Files\MATLAB\R2009b\toolbox\matlab\graph2d C:\Program Files\MATLAB\R2009b\toolbox\matlab\graph3d C:\Program Files\MATLAB\R2009b\toolbox\matlab\specgraph C:\Program Files\MATLAB\R2009b\toolbox\matlab\graphics C:\Program Files\MATLAB\R2009b\toolbox\matlab\uitools C:\Program Files\MATLAB\R2009b\toolbox\matlab\strfun C:\Program Files\MATLAB\R2009b\toolbox\matlab\imagesci C:\Program Files\MATLAB\R2009b\toolbox\matlab\iofun C:\Program Files\MATLAB\R2009b\toolbox\matlab\audiovideo C:\Program Files\MATLAB\R2009b\toolbox\matlab\timefun C:\Program Files\MATLAB\R2009b\toolbox\matlab\datatypes C:\Program Files\MATLAB\R2009b\toolbox\matlab\verctrl C:\Program Files\MATLAB\R2009b\toolbox\matlab\codetools C:\Program Files\MATLAB\R2009b\toolbox\matlab\helptools C:\Program Files\MATLAB\R2009b\toolbox\matlab\winfun C:\Program Files\MATLAB\R2009b\toolbox\matlab\winfun\NET C:\Program Files\MATLAB\R2009b\toolbox\matlab\demos C:\Program Files\MATLAB\R2009b\toolbox\matlab\timeseries C:\Program Files\MATLAB\R2009b\toolbox\matlab\hds C:\Program Files\MATLAB\R2009b\toolbox\matlab\guide C:\Program Files\MATLAB\R2009b\toolbox\matlab\plottools C:\Program Files\MATLAB\R2009b\toolbox\local C:\Program Files\MATLAB\R2009b\toolbox\shared\controllib C:\Program Files\MATLAB\R2009b\toolbox\shared\dastudio C:\Program Files\MATLAB\R2009b\toolbox\matlab\datamanager [x,y] = SumAvg(4,9) x = 13 y = 6.5000 a = [3,4,5]; b = [3,9,0]; SumAvg(a,b) ans = 6 13 5 [x,y] = SumAvg(a,b) x = 6 13 5 y = 3.0000 6.5000 2.5000 help CentDiff CentDiff(f,x,h) returns the central difference approximation for the derivative of the function f at x, using step size h > 0, with the formula (f(x+h/2) - f(x-h/2)/h). CentDiff('sin',pi/2,0.001) ans = 0 % Note that we pass the function % to the function CentDiff as a string. CentDiff('cos',pi/2,0.001) ans = -1.0000 CentDiff('cos',pi/2,0.1) ans = -0.9996 f = inline('cos(x)*sin(x) + x',x) f = Inline function: f(x,P1,P2,P3,P4,P5,P6) = cos(x)*sin(x) + x f = inline('cos(x)*sin(x) + x','x') f = Inline function: f(x) = cos(x)*sin(x) + x f(8) ans = 7.8560 cos(8)*sin(8) + 8 ans = 7.8560 % We inline we quickly make one line functions, % defined by a string. % We added a check in the CentDiff function: CentDiff('sin',0,0) h must be positive ans = 0 help bisect Applies the bisection method to the function f on [a,b], f is assumed to be continuous and f(a)*f(b) < 0. Stops when |f(a)| < eps or |f(b)| < eps or |b-a| < eps. Failure is reported (fail = 1) when the accuracy requirement is not satisfied in N steps; otherwise fail = 0 on return. Example : >> [a,b,fail] = bisect(’cos’,pi/4,2*pi/3,0.0001,100) [a,b,fail] = bisect('cos',pi/4,2*pi/3,0.0001,100) {Warning: Invalid escape sequence appears in format string. See help sprintf for valid escape sequences.} > In bisect at 12 running the bisection method... a = 1.439897, b = 2.094395, m = 1.439897, f(m) = 0.130526 a = 1.439897, b = 1.767146, m = 1.767146, f(m) = -0.195090 a = 1.439897, b = 1.603521, m = 1.603521, f(m) = -0.032719 a = 1.521709, b = 1.603521, m = 1.521709, f(m) = 0.049068 a = 1.562615, b = 1.603521, m = 1.562615, f(m) = 0.008181 a = 1.562615, b = 1.583068, m = 1.583068, f(m) = -0.012272 a = 1.562615, b = 1.572842, m = 1.572842, f(m) = -0.002045 a = 1.567728, b = 1.572842, m = 1.567728, f(m) = 0.003068 a = 1.570285, b = 1.572842, m = 1.570285, f(m) = 0.000511 a = 1.570285, b = 1.571563, m = 1.571563, f(m) = -0.000767 a = 1.570285, b = 1.570924, m = 1.570924, f(m) = -0.000128 a = 1.570605, b = 1.570924, m = 1.570605, f(m) = 0.000192 a = 1.570764, b = 1.570924, m = 1.570764, f(m) = 0.000032 succeeded after 13 steps a = 1.5708 b = 1.5709 fail = 0 % Our last script prompts the user for input. AreaCircle Give the radius 2 ans = 12.5664 diary off