实验六 Matlab 函数进阶一、实验目的1.了解函数句柄\匿名函数\主函数\嵌套函数\子函数\私有函数等概念;二、实验内容与要求1.参照实验提示,练习数句柄\匿名函数\主函数\嵌套函数\子函数\私有函数相关程序.2.Help菜单查: Anonymous Functions, Primary Functions, Nested Functions, Subfunctions, Private Functions, function handle3.完成实验习题4.将完成每题所用的命令写入一个文本文件,取名为: 学号最后两位_机号_姓名.txt, 下课前通过管理系统提交作业三、实验习题1. 用匿名函数定义函数2. 首先用嵌套函数定义(取不同的值时返回不同的函数)然后求用fzero求时方程的根.实验提示:matlab帮助中搜索Types of Functions,可以看到matlab常用的函数种类:Anonymous Functions -- 匿名函数Primary Functions -- 主函数Nested Functions -- 嵌套函数Subfunctions -- 子函数Private Functions -- 私有函数 Private functions are functions that reside in subdirectories with the special name private. These functions are called private because they are visible only to M-file functions and M-file scripts that meet these conditions: A function that calls a private function must be defined in an M-file that resides in the directory immediately above that private subdirectory. A script that calls a private function must itself be called from an M-file function that has access to the private function according to the above rule.1.函数句柄function_handle (@)Syntaxhandle = @functionnamehandle = @(arglist)anonymous_function fhandle = @(arglist) expr如sqr = @(x) x.^2;Handle used in calling functions indirectlyA function handle is a MATLAB value that provides a means of calling a function indirectly. You can pass function handles in calls to other functions (often called function functions). You can also store function handles in data structures for later use (for example, as Handle Graphics callbacks). A function handle is one of the standard MATLAB data types.handle = @(arglist)anonymous_function constructs an anonymous function and returns a handle to that function. The body of the function, to the right of the parentheses, is a single MATLAB statement or command. arglist is a comma-separated list of input arguments. Execute the function by calling it by means of the function handle, handle.ExamplesExample 1 — Constructing a Handle to a Named FunctionThe following example creates a function handle for the humps function and assigns it to the variable fhandle.fhandle = @humps;Pass the handle to another function in the same way you would pass any argument. This example passes the function handle just created to fminbnd, which then minimizes over the interval [0.3, 1].x = fminbnd(fhandle, 0.3, 1)x = 0.6370Example 2 — Constructing a Handle to an Anonymous FunctionThe statement below creates an anonymous function that finds the square of a number. When you call this function, MATLAB assigns the value you pass in to variable x, and then uses x in the equation x.^2:sqr = @(x) x.^2;函数句柄实际上提供了一种间接调用函数的方法。
创建函数句柄需要用到操作符@其实,我们一般用的匿名函数实际上就是一种函数句柄,而对Matlab提供的各种M文件函数和内部函数,也都可以创建函数句柄,从而可以通过函数句柄对这些函数实现间接调用函数句柄的优点如下:1)方便地实现函数间互相调用2)兼容函数加载的所有方式3)拓宽子函数,包括局部函数的使用范围4)提高函数调用的可靠性5)减少程序设计中的冗余6)提高重复执行的效率创建函数句柄的一般语法如下所示:fhandle = @function_filename例如fhandle = @sin就创建了sin的句柄,输入fhandle(x)其实就是sin(x)的功能 %%%句柄中包含了函数的搜索路径,避免每次执行时都要去查找sin文件在哪里2nested functions1)function z=fh(x)function u=hg(v)u=v^2+v+x;%u=v^2+venda=@hg;z=a(x);end2). function fhandle = makeParabola(a, b, c)% MAKEPARABOLA returns a function handle with parabola % coefficients.fhandle = @parabola; % @ is the function handle constructor function y = parabola(x) y = a*x.^2 + b*x + c; endend3).function fh=nestedFunc(y) function r=pow(x) r=x^y; endfh=@pow;end4). function varScope5x = 5;z = nestfun; % @nestfun function y = nestfun y = x + 1; endzend4使用匿名函数(Anonymous Function)和内嵌函数(Nested Function)处理多变量传递问题(Matlab 7.0以上)问题:有一个多变量函数f(abcx),现需要分别在a=a0b=b0c=c0和a=a1b=b1c=c1的条件下对f(abcx)进行某一操作。
此类问题常在数值积分时出现解决方案:1. 使用全局变量 可在主调函数和被调函数中分别将a,b,c声明为全局变量(global a b c),这时f通过全局变量传递abc, 定义时可以只有一个参数x2. 使用anonymous function3. 使用 nested function 下面举例说明anonymous function和nested function的使用例:对任意二次多项式 ax2+bx+c 进行数值积分(此处显然可以解析得到,此例使用anonymousfunction做演示)解:(1)使用匿名函数编写文件intpoly2.m如下 function y_int=intpoly2(a,b,c)y_int=quad(@(x)(poly2(a,b,c,x)), 0,1); %此处利用matlab内部函 %数quad(fun, x0,xt)进行积分, %被积函数fun我们使用匿名函 %数"@(x)(poly2(a,b,c,x))"以便 %将自变量限制为x. function y=poly2(ai,bi,ci,x) %此处定义一个多变量的子函数 poly2( a,b,c,x)y=ai.*x.^2+bi.*x+ci; %此处使用矩阵元素运算(.* 和.^)等便于 %被矩阵化的内部函数调用,且提高程序效率执行:保存该文件并将matlab切换至该文件目录下,命令行输入intpoly2(1,2,3), 便给出积分结果ans=4.33 (2)使用nested function编写函数保存为intnest.m, 内容如下function y_int=intnest(a,b,c)y_int=quad(@poly2, 0,1); %此处利用matlab内部函 %数quad(fun, x0,xt)进行积分, %被积函数fun我们使用内嵌函 %数poly2(x)的句柄@poly2 function y=poly2(x) %此处定义一个内嵌函数 poly2( a,b,c,x) y=a.*x.^。