使用匿名函数和内嵌函数处理多变量传递问题

问题:有一个多变量函数 f ( a b c x ) ,现需要分别在 a = a 0 b = b 0 c = c 0 a = a 1 b = b 1 c = c 1 的条件下对 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的使用。

例:对任意二次多项式进行数 此处显然可以解析得到,此例使用anonymous
function做演示)

解:(1)使用匿名函数

编写文件intpoly2.m
如下

<span style="color: rgb(0, 0, 255);">function</span> y_int=intpoly2<span style="color: rgb(0, 136, 0);">(</span>a,b,c<span style="color: rgb(0, 136, 0);">)</span>
y_int=quad<span style="color: rgb(0, 136, 0);">(</span>@<span style="color: rgb(0, 136, 0);">(</span>x<span style="color: rgb(0, 136, 0);">)</span><span style="color: rgb(0, 136, 0);">(</span>poly2<span style="color: rgb(0, 136, 0);">(</span>a,b,c,x<span style="color: rgb(0, 136, 0);">)</span><span style="color: rgb(0, 136, 0);">)</span>, <span style="color: rgb(51, 51, 255);">0</span>,<span style="color: rgb(51, 51, 255);">1</span><span style="color: rgb(0, 136, 0);">)</span>;  <span style="color: rgb(34, 139, 34);">%此处利用matlab内部函</span>
                              <span style="color: rgb(34, 139, 34);">%数quad(fun, x0,xt)进行积分,</span>
                              <span style="color: rgb(34, 139, 34);">%被积函数fun我们使用匿名函</span>
                              <span style="color: rgb(34, 139, 34);">%数“@(x)(poly2(a,b,c,x))”以便</span>
                              <span style="color: rgb(34, 139, 34);">%将自变量限制为x. </span>
 
<span style="color: rgb(0, 0, 255);">function</span> y=poly2<span style="color: rgb(0, 136, 0);">(</span>ai,bi,ci,x<span style="color: rgb(0, 136, 0);">)</span>  <span style="color: rgb(34, 139, 34);">%此处定义一个多变量的子函数 poly2( a,b,c,x)</span>
y=ai.*x.^<span style="color: rgb(51, 51, 255);">2</span>+bi.*x+ci;         <span style="color: rgb(34, 139, 34);">%此处使用矩阵元素运算(.*  和.^)等便于</span>
                              <span style="color: rgb(34, 139, 34);">%被矩阵化的内部函数调用,且提高程序效率</span>

执行:

保存该文件并将matlab切换至该文件目录下,命令行输入intpoly2(1,2,3), 便给出积分结果ans=4.33

实际上,上例也可以简化成:

<span style="color: rgb(0, 0, 255);">function</span> y_int=intpoly2<span style="color: rgb(0, 136, 0);">(</span>a,b,c<span style="color: rgb(0, 136, 0);">)</span>
y_int=quad<span style="color: rgb(0, 136, 0);">(</span>@<span style="color: rgb(0, 136, 0);">(</span>x<span style="color: rgb(0, 136, 0);">)</span><span style="color: rgb(0, 136, 0);">(</span>a.*x.^<span style="color: rgb(51, 51, 255);">2</span>+b.*x+c<span style="color: rgb(0, 136, 0);">)</span>, <span style="color: rgb(51, 51, 255);">0</span>,<span style="color: rgb(51, 51, 255);">1</span><span style="color: rgb(0, 136, 0);">)</span>;  <span style="color: rgb(34, 139, 34);">%此处利用matlab内部函</span>
                              <span style="color: rgb(34, 139, 34);">%数quad(fun, x0,xt)进行积分,</span>
                              <span style="color: rgb(34, 139, 34);">%被积函数fun我们使用匿名函</span>
                              <span style="color: rgb(34, 139, 34);">%数@(x)(a.*x.^2+b.*x+c)以便</span>
                              <span style="color: rgb(34, 139, 34);">%将自变量限制为x. </span>

(2)使用nested function

编写函数保存为intnest.m, 内容如下

<span style="color: rgb(0, 0, 255);">function</span> y_int=intnest<span style="color: rgb(0, 136, 0);">(</span>a,b,c<span style="color: rgb(0, 136, 0);">)</span>
y_int=quad<span style="color: rgb(0, 136, 0);">(</span>@poly2, <span style="color: rgb(51, 51, 255);">0</span>,<span style="color: rgb(51, 51, 255);">1</span><span style="color: rgb(0, 136, 0);">)</span>;      <span style="color: rgb(34, 139, 34);">%此处利用matlab内部函</span>
                               <span style="color: rgb(34, 139, 34);">%数quad(fun, x0,xt)进行积分,</span>
                               <span style="color: rgb(34, 139, 34);">%被积函数fun我们使用内嵌函</span>
                               <span style="color: rgb(34, 139, 34);">%数poly2(x)的句柄@poly2 </span>
 
<span style="color: rgb(0, 0, 255);">function</span> y=poly2<span style="color: rgb(0, 136, 0);">(</span>x<span style="color: rgb(0, 136, 0);">)</span>           <span style="color: rgb(34, 139, 34);">%此处定义一个内嵌函数 poly2( a,b,c,x) </span>
y=a.*x.^<span style="color: rgb(51, 51, 255);">2</span>+b.*x+c;            <span style="color: rgb(34, 139, 34);">%直接调用母函数中的变量a,b,c</span>
<span style="color: rgb(0, 0, 255);">end</span>                           <span style="color: rgb(34, 139, 34);">% 结束内嵌函数poly2</span>
<span style="color: rgb(0, 0, 255);">end</span>                           <span style="color: rgb(34, 139, 34);">% 结束母函数intpoly2</span>

保存后执行,同样效果。

可见nested
function只是将主调函数和被调函数封装到了一起以共享主调函数的变量。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值