1113 1

[Weka及其他] MATLAB课程:代码示例之Programming(三) [推广有奖]

企业贵宾

已卖:160份资源

巨擘

0%

还不是VIP/贵宾

-

威望
4
论坛币
624047 个
通用积分
180.5582
学术水平
918 点
热心指数
987 点
信用等级
841 点
经验
399203 点
帖子
9786
精华
48
在线时间
17322 小时
注册时间
2014-8-19
最后登录
2022-11-2

楼主
widen我的世界 学生认证  发表于 2016-3-9 14:30:15 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

MATLAB课程:代码示例之Programming(三)

Nested Functions


This example shows how to use nested functions to easily share data, as well as for creating customized functions.


Example 1: Sharing Data

Let's first take a look at taxDemo.m, which contains a nested function.

type taxDemo.m


function y = taxDemo(income)%TAXDEMO Used by NESTEDDEMO.% Calculate the tax on income.% Copyright 1984-2014 The MathWorks, Inc.AdjustedIncome = income - 6000; % Calculate adjusted income% Call 'computeTax' without passing 'AdjustedIncome' as a parameter.y = computeTax;   function y = computeTax            % This function can see the variable 'AdjustedIncome'      % in the calling function's workspace      y = 0.28 * AdjustedIncome;   endend


The nested function computeTax can see the variables in the parent function's workspace. This makes sharing of data between multiple nested functions easy and particularly useful when processing large data sets. We can call the function in the usual way.

y = taxDemo(80e3) % What is the tax on $80k income?


y =   2.0720e+04


For nested functions, the end statement is required at the end of a function. You can also nest functions to any level.

Example 2: Creating Customized Functions

Nested functions allow the ability to create customized functions. Let's look at makefcn.m which contains a nested function.

type makefcn.m


function fcn = makefcn(a,b,c)%MAKEFCN Used by NESTEDDEMO.% This function returns a handle to a customized version of 'parabola'.% a,b,c specifies the coefficients of the function.% Copyright 1984-2014 The MathWorks, Inc.fcn = @parabola;   % Return handle to nested function   function y = parabola(x)      % This nested function can see the variables 'a','b', and 'c'      y = a*x.^2 + b.*x + c;   endend


When you call makefcn, it returns a function handle to a customized function. For example:

f = makefcn(3,2,10);g = makefcn(0,5,25);


f and g are handles to two functions, each with different coefficients. We can evaluate the functions by using their function handles and passing in parameters.

y = f(2)


y =    26


y = g(2)


y =    35


We can also pass the handle to function functions, such as optimization or integration.

minimum = fminbnd(f,-5,5);


Or plot the function over a range.

fplot(f, [-6 6], 'b-')                  % Plot f over a range of xhold on;plot(2,f(2),'bd');                      % Plot a marker at (2,f(2))plot(minimum,f(minimum),'bs');          % Plot at minimum of ftext(minimum,f(minimum)-2,'Minimum');fplot(g, [-6 6], 'r-')plot(2,g(2),'rd');                      % Plot a marker at (2,g(2))ylim([-10 60])hold off;


Example 3: Creating Customized Functions with State

Let's look at makecounter.m which contains a nested function.

type makecounter.m


function countfcn = makecounter(initvalue)%MAKECOUNTER Used by NESTEDDEMO.% This function returns a handle to a customized nested function 'getCounter'.% initvalue specifies the initial value of the counter whose's handle is returned.% Copyright 1984-2014 The MathWorks, Inc.currentCount = initvalue; % Initial valuecountfcn = @getCounter;   % Return handle to getCounter   function count = getCounter      % This function increments the variable 'currentCount', when it      % gets called (using its function handle) .      currentCount = currentCount + 1;      count = currentCount;   endend


When you call makecounter, it returns a handle to its nested function getCounter. getCounter is customized by the value of initvalue, a variable it can see via nesting within the workspace of makecounter.

counter1 = makecounter(0);  % Define counter initialized to 0counter2 = makecounter(10); % Define counter initialized to 10


Here we have created two customized counters: one that starts at 0 and one that starts at 10. Each handle is a separate instance of the function and its calling workspace. Now we can call the inner nested function via its handle. counter1 does not take parameters, but it could.

counter1Value = counter1()


counter1Value =     1


We can call the two functions independently as there are two separate workspaces for the parent functions kept. They remain in memory while the handles to their nested functions exist. In this case the currentCount variable gets updated when counter1 is called.

counter1Value = counter1()


counter1Value =     2


counter2Value = counter2()


counter2Value =    11





二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Programming MATLAB课程 Program MATLAB atlab MATLAB课程 代码示例 Programming NestedFunctions


https://www.cda.cn/?seo-luntan
高薪就业·数据科学人才·16年教育品牌

沙发
聆听故事city 在职认证  发表于 2016-3-9 14:30:51
受用了,thanks。

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-10 03:35