% scripts and MATLAB functions are stored % in files ending with ".m". We can place % the scripts in the current directory, where % MATLAB will find them, or change the path path MATLABPATH N:\MATLAB~1\toolbox\matlab\general N:\MATLAB~1\toolbox\matlab\ops N:\MATLAB~1\toolbox\matlab\lang N:\MATLAB~1\toolbox\matlab\elmat N:\MATLAB~1\toolbox\matlab\elfun N:\MATLAB~1\toolbox\matlab\specfun N:\MATLAB~1\toolbox\matlab\matfun N:\MATLAB~1\toolbox\matlab\datafun N:\MATLAB~1\toolbox\matlab\polyfun N:\MATLAB~1\toolbox\matlab\funfun N:\MATLAB~1\toolbox\matlab\sparfun N:\MATLAB~1\toolbox\matlab\scribe N:\MATLAB~1\toolbox\matlab\graph2d N:\MATLAB~1\toolbox\matlab\graph3d N:\MATLAB~1\toolbox\matlab\specgraph N:\MATLAB~1\toolbox\matlab\graphics N:\MATLAB~1\toolbox\matlab\uitools N:\MATLAB~1\toolbox\matlab\strfun N:\MATLAB~1\toolbox\matlab\imagesci N:\MATLAB~1\toolbox\matlab\iofun N:\MATLAB~1\toolbox\matlab\audiovideo N:\MATLAB~1\toolbox\matlab\timefun N:\MATLAB~1\toolbox\matlab\datatypes N:\MATLAB~1\toolbox\matlab\verctrl N:\MATLAB~1\toolbox\matlab\codetools N:\MATLAB~1\toolbox\matlab\helptools N:\MATLAB~1\toolbox\matlab\winfun N:\MATLAB~1\toolbox\matlab\demos N:\MATLAB~1\toolbox\local N:\MATLAB~1\toolbox\simulink\simulink N:\MATLAB~1\toolbox\simulink\blocks N:\MATLAB~1\toolbox\simulink\components N:\MATLAB~1\toolbox\simulink\fixedandfloat N:\MATLAB~1\toolbox\simulink\fixedandfloat\fxpdemos N:\MATLAB~1\toolbox\simulink\fixedandfloat\obsolete N:\MATLAB~1\toolbox\simulink\simdemos N:\MATLAB~1\toolbox\simulink\simdemos\aerospace N:\MATLAB~1\toolbox\simulink\simdemos\automotive N:\MATLAB~1\toolbox\simulink\simdemos\simfeatures N:\MATLAB~1\toolbox\simulink\simdemos\simfeatures\mdlref N:\MATLAB~1\toolbox\simulink\simdemos\simgeneral N:\MATLAB~1\toolbox\simulink\simdemos\simnew N:\MATLAB~1\toolbox\simulink\dee N:\MATLAB~1\toolbox\shared\dastudio N:\MATLAB~1\toolbox\stateflow\stateflow N:\MATLAB~1\toolbox\rtw\rtw N:\MATLAB~1\toolbox\shared\hds N:\MATLAB~1\toolbox\shared\timeseries N:\MATLAB~1\toolbox\stateflow\sfdemos N:\MATLAB~1\toolbox\stateflow\coder N:\MATLAB~1\toolbox\control\control N:\MATLAB~1\toolbox\control\ctrlguis N:\MATLAB~1\toolbox\control\ctrlobsolete N:\MATLAB~1\toolbox\control\ctrlutil N:\MATLAB~1\toolbox\control\ctrldemos N:\MATLAB~1\toolbox\shared\controllib N:\MATLAB~1\toolbox\ident\ident N:\MATLAB~1\toolbox\ident\idobsolete N:\MATLAB~1\toolbox\ident\idguis N:\MATLAB~1\toolbox\ident\idutils N:\MATLAB~1\toolbox\ident\iddemos N:\MATLAB~1\toolbox\ident\idhelp N:\MATLAB~1\toolbox\images\images N:\MATLAB~1\toolbox\images\imuitools N:\MATLAB~1\toolbox\images\imdemos N:\MATLAB~1\toolbox\images\iptutils N:\MATLAB~1\toolbox\nnet\nnet N:\MATLAB~1\toolbox\nnet\nnutils N:\MATLAB~1\toolbox\nnet\nncontrol N:\MATLAB~1\toolbox\nnet\nndemos N:\MATLAB~1\toolbox\nnet\nnobsolete N:\MATLAB~1\toolbox\optim N:\MATLAB~1\toolbox\shared\optimlib N:\MATLAB~1\toolbox\pde N:\MATLAB~1\toolbox\signal\signal N:\MATLAB~1\toolbox\signal\sigtools N:\MATLAB~1\toolbox\signal\sptoolgui N:\MATLAB~1\toolbox\signal\sigdemos N:\MATLAB~1\toolbox\stats N:\MATLAB~1\toolbox\symbolic N:\MATLAB~1\toolbox\wavelet\wavelet N:\MATLAB~1\toolbox\wavelet\wavedemo pwd % print working directory ans = C:\Documents and Settings\janv edit fschange('C:\Documents and Settings\janv\my_script.m'); clear('C:\Documents and Settings\janv\my_script.m'); % We just created a funtion stored in my_script % to see if MATLAB can get to my_script, we type help my_script returns the sum of a and b my_script(3,4) ans = 7 % By default, the current directory was in the % matlabpath, otherwise, if my_script was save % somewhere else, we had to adjust the path. % The next example is an interactive function, % to compute the area of a circle. The user is edit fschange('C:\Documents and Settings\janv\areacircle.m'); clear('C:\Documents and Settings\janv\areacircle.m'); % prompted to give the radius. areacircle Give the radius : 45 The area of circle with radius 45.00 is 6361.725124 edit fschange('C:\Documents and Settings\janv\traprule.m'); clear('C:\Documents and Settings\janv\traprule.m'); % We just created a function "traprule" % to implement the trapezoidal rule help traprule DESCRIPTION : Returns the trapezoidal rule to approximate the integral of f over [a,b]. % suppose we want to approximate the integral % of cos(x) for x in [0,1] t = traprule(cos,0,1) ??? Error using ==> cos Not enough input arguments. Error in ==> cos at 14 [varargout{1:nargout}] = builtin('cos', varargin{:}); % To give a function name as argument to another % function, we must pass it as a string: t = traprule('cos',0,1) t = 0.7702 % Let us implement the composite trapezoidal rule edit fschange('C:\Documents and Settings\janv\comptrap.m'); clear('C:\Documents and Settings\janv\comptrap.m'); % to improve approximation. t = comptrap('cos',0,1,1) t = 0.7702 t = comptrap('cos',0,1,2) t = 0.8239 t = comptrap('cos',0,1,3) t = 0.8337 t = comptrap('cos',0,1,4) t = 0.8371 format long e for n = 1:10 t = comptrap('cos',0,1,n) end; t = 7.701511529340699e-001 t = 8.238668574122213e-001 t = 8.336651200085851e-001 t = 8.370837513522271e-001 t = 8.386642098070081e-001 t = 8.395222329270183e-001 t = 8.400394247082814e-001 t = 8.403750340273868e-001 t = 8.406050957359873e-001 t = 8.407696420884199e-001 help quad QUAD Numerically evaluate integral, adaptive Simpson quadrature. Q = QUAD(FUN,A,B) tries to approximate the integral of function FUN from A to B to within an error of 1.e-6 using recursive adaptive Simpson quadrature. The function Y = FUN(X) should accept a vector argument X and return a vector result Y, the integrand evaluated at each element of X. Q = QUAD(FUN,A,B,TOL) uses an absolute error tolerance of TOL instead of the default, which is 1.e-6. Larger values of TOL result in fewer function evaluations and faster computation, but less accurate results. The QUAD function in MATLAB 5.3 used a less reliable algorithm and a default tolerance of 1.e-3. [Q,FCNT] = QUAD(...) returns the number of function evaluations. QUAD(FUN,A,B,TOL,TRACE) with non-zero TRACE shows the values of [fcnt a b-a Q] during the recursion. QUAD(FUN,A,B,TOL,TRACE,P1,P2,...) provides for additional arguments P1, P2, ... to be passed directly to function FUN, FUN(X,P1,P2,...). Pass empty matrices for TOL or TRACE to use the default values. Use array operators .*, ./ and .^ in the definition of FUN so that it can be evaluated with a vector argument. Function QUADL may be more efficient with high accuracies and smooth integrands. Example: FUN can be specified as: An anonymous function: F = @(x) 1./(x.^3-2*x-5); Q = quad(F,0,2); A function handle: Q = quad(@myfun,0,2); where myfun.m is an M-file: function y = myfun(x) y = 1./(x.^3-2*x-5); Class support for inputs A, B, and the output of FUN: float: double, single See also quadv, quadl, dblquad, triplequad, @, trapz. Reference page in Help browser doc quad quad('cos',0,1) ans = 8.414709828359894e-001 comptrap('cos',0,1,100) ans = 8.414639725380023e-001 f = inline('x^7 + 3*x + 1') f = Inline function: f(x) = x^7 + 3*x + 1 f(3) ans = 2197 comptrap(f,0,1,9) ans = 2.632157348291407e+000 diary off