1178 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:43:19 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币

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


Anonymous Functions


This example shows how to define functions at the command line with anonymous functions.


Integrating a Function

Consider the function 10*x.


If we want to allow any multiplier of x, not just 10, we might create a variable g (where g is initially set to 10), and create a new function


Let's do this in MATLAB® by creating a function handle h.

g = 10;h = @(x) g*x;


You can integrate the function by passing its handle to the INTEGRAL function.

integral(h,1,10)


ans =  495.0000


Consider another function:


Create a function handle to this function where alpha = 0.9.

alpha = 0.9;f = @(x) sin(alpha*x);


Plot the function and shade the area under it.

x = 0:pi/100:pi;area(x,f(x)); % You can evaluate f without fevaltitle('f(x) = sin(\alpha x), \alpha =.9');


We can use the INTEGRAL function to calculate the area under the function between a range of values.

integral(f,0,pi)


ans =    2.1678


Minimizing a Function

Consider the function:


where a = 1, b = -2, and c = 1

Create a function handle for it.

a = 1;b = -2;c = 1;f = @(x)(a*x.^2+b*x+c);


ezplot(f); % Plot the functiontitle('f(x)=ax^2+bx+c, a=1,b=-2,c=1');hold on;% Find and plot the minimumminimum = fminbnd(f,-2,2);       % We can pass the function handle directly                                 % to the minimization routineplot(minimum,f(minimum),'d');    % We can evaluate the function without                                 % using fevalgrid;hold off;


2D Functions

We can create handles to functions of many variables

a = pi;b = 15;f = @(x,y) (a*x+b*y);ezsurf(f);title('f(x,y) = ax+by, a = \pi, b = 15');


Function Composition

We can also create handles to functions of functions

f = @(x) x.^2;g = @(x) 3*x;h = @(x) g(f(x));h(3)


ans =    27





二维码

扫码加我 拉你入群

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

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

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


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

沙发
我有我的love 在职认证  发表于 2016-3-9 14:44:04
人生处处皆学问。。

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

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