实验六 matlab 函数进阶

上传人:kms****20 文档编号:40297437 上传时间:2018-05-25 格式:DOC 页数:5 大小:54KB
返回 下载 相关 举报
实验六  matlab 函数进阶_第1页
第1页 / 共5页
实验六  matlab 函数进阶_第2页
第2页 / 共5页
实验六  matlab 函数进阶_第3页
第3页 / 共5页
实验六  matlab 函数进阶_第4页
第4页 / 共5页
实验六  matlab 函数进阶_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《实验六 matlab 函数进阶》由会员分享,可在线阅读,更多相关《实验六 matlab 函数进阶(5页珍藏版)》请在金锄头文库上搜索。

1、1实验六 Matlab 函数进阶函数进阶一、实验目的一、实验目的 1了解函数句柄匿名函数主函数嵌套函数子函数私有函数等概念;二、实验内容与要求二、实验内容与要求1参照实验提示,练习数句柄匿名函数主函数嵌套函数子函数私有函数相关程 序.2Help 菜单查: Anonymous Functions, Primary Functions, Nested Functions, Subfunctions, Private Functions, function handle3完成实验习题4将完成每题所用的命令写入一个文本文件,取名为: 学号最后两位_机号_姓名.txt, 下课前通过管理系统提交作业三、实

2、验习题三、实验习题 1.用匿名函数定义函数2( )21f xxx2.首先用嵌套函数定义(取不同的值时返回不同的函数)2( )1f bxbxb然后求用 fzero 求时方程的根.2.5b 实验提示:实验提示: matlab 帮助中搜索 Types of Functions,可以看到 matlab 常用的函数种类: Anonymous Functions - 匿名函数 Primary Functions - 主函数 Nested Functions - 嵌套函数 Subfunctions - 子函数 Private Functions - 私有函数 Private functions are fu

3、nctions 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

4、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 () Syntax handle = functionname handle = (arglist)anonymous_function fhandl

5、e = (arglist) expr 如 sqr = (x) x.2; Handle used in calling functions indirectly2A 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

6、 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 parent

7、heses, 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. Examples Example 1 Constructing a Handle to a Named Function The following example creates a function handle for the humps

8、 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.6370 E

9、xample 2 Constructing a Handle to an Anonymous Function The 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; 函数句柄实际上提供了一种间接调用函数的方法。 创建函

10、数句柄需要用到操作符 其实,我们一般用的匿名函数实际上就是一种函数句柄,而对 Matlab 提供的各种 M 文件 函数和内部函数,也都可以创建函数句柄,从而可以通过函数句柄对这些函数实现间接调 用。 函数句柄的优点如下: 1)方便地实现函数间互相调用 2)兼容函数加载的所有方式 3)拓宽子函数,包括局部函数的使用范围 4)提高函数调用的可靠性 5)减少程序设计中的冗余 6)提高重复执行的效率 创建函数句柄的一般语法如下所示: fhandle = function_filename 例如 fhandle = sin 就创建了 sin 的句柄,输入 fhandle(x)其实就是 sin(x)的功能

11、。 %句 柄中包含了函数的搜索路径,避免每次执行时都要去查找 sin 文件在哪里 2nested functions 1) function z=fh(x) function u=hg(v) u=v2+v+x;%u=v2+v3end a=hg; z=a(x); end 2). function fhandle = makeParabola(a, b, c) % MAKEPARABOLA returns a function handle with parabola % coefficients. fhandle = parabola; % is the function handle cons

12、tructorfunction y = parabola(x)y = a*x.2 + b*x + c;end end 3).function fh=nestedFunc(y)function r=pow(x)r=xy;end fh=pow; end 4). function varScope5 x = 5; z = nestfun; % nestfunfunction y = nestfuny = x + 1;end z end 4 使用匿名函数使用匿名函数(Anonymous Function)和内嵌函数和内嵌函数(Nested Function)处理多变量传递问题处理多变量传递问题 (Ma

13、tlab 7.0 以上以上) 问题:有一个多变量函数 f(a b c x),现需要分别在 a=a0 b=b0 c=c0 和 a=a1 b=b1 c=c1 的条件 下对 f(a b c x)进行某一操作。 此类问题常在数值积分时出现。 解决方案: 1 使用全局变量 可在主调函数和被调函数中分别将 a,b,c 声明为全局变量(global a b c),这时 f 通过全局变量 传递 abc, 定义时可以只有一个参数 x。 2 使用 anonymous function 3 使用 nested function 下面举例说明 anonymous function 和 nested function

14、的使用。 例:例:对任意二次多项式 ax2+bx+c 进行数值积分(此处显然可以解析得到,此例使用 anonymous function 做演示) 解:(解:(1)使用匿名函数)使用匿名函数 编写文件 intpoly2.m 如下 4function 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,

15、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,x

16、t)进行积分,%被积函数 fun 我们使用内嵌函%数 poly2(x)的句柄poly2 function y=poly2(x) %此处定义一个内嵌函数 poly2( a,b,c,x) y=a.*x.2+b.*x+c; %直接调用母函数中的变量 a,b,c end % 结束内嵌函数 poly2 end % 结束母函数 intpoly2保存后执行,同样效果。 可见 nested function 只是将主调函数和被调函数封装到了一起以共享主调函数的变量只是将主调函数和被调函数封装到了一起以共享主调函数的变量。 注意:注意:一般函数不需要 end,当出现 nested function 时,主调函数和被调函数都必须有主

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号