Defining function handles in MATLAB

291 ビュー (過去 30 日間)
Richard
Richard 2012 年 1 月 8 日
コメント済み: Mark Jackson 2022 年 10 月 11 日
How might I define a function handle?
For example, I want to define a function f(x)=2*x^3+7*x^2+x
I want MATLAB to evaluate f(x) at random values x. I have heard of feval and fhandles, but I don't know how to do it.
Thanks.
  2 件のコメント
Leia Sofia Mendez
Leia Sofia Mendez 2017 年 7 月 26 日
編集済み: Walter Roberson 2017 年 7 月 26 日
This was the code I was trying to write:
a= 0.0009
a= convtime([1],'samples','seconds')
This code gave an error saying that my function (convtime) was undefined. How you define a function in MATLAB?
Walter Roberson
Walter Roberson 2017 年 7 月 26 日
Leia Sofia Mendez:
You should go to that link, and click the download button, and download the .zip file. You should unzip to a directory that is not under your MATLAB installation directory. You would then use pathtool in MATLAB to add that directory to your MATLAB path.

サインインしてコメントする。

採用された回答

Chandra Kurniawan
Chandra Kurniawan 2012 年 1 月 8 日
編集済み: MathWorks Support Team 2019 年 5 月 22 日
Hi, Richard.
To evaluate f(x) at different values of x, you can create an .m file and write this code:
function y = f(x)
y = 2 * (x^3) + 7 * (x^2) + x;
If you save the file under the name 'f.m', you can run the function by typing this code in the Command Window or a separate .m file.
x = randi(7);
y = f(x)
The randi function above generates a 1-by-5 row vector of random integers between 1 and 10. The values returned by f are stored in a 1-by-5 row vector y.
For more information about creating functions, see:
You can create a handle to the function f with an @ sign. For example, create a handle named myHandle as follows:
myHandle = @f;
Now you can run f indirectly by using its handle.
y = myHandle(x)
For more information about function handles, see:
  5 件のコメント
Piyush Gade
Piyush Gade 2022 年 2 月 4 日
Whatever this is, this doesn't work.
Mark Jackson
Mark Jackson 2022 年 10 月 11 日
I recommend clicking 'Getting Started.'

サインインしてコメントする。

その他の回答 (3 件)

Walter Roberson
Walter Roberson 2012 年 1 月 8 日
Function handle version:
f = @(x) 2*x^3+7*x^2+x;
Then f is already the function handle, and you can call f(3.7) (for example)
There is no need to use feval() for this, but you could.
  1 件のコメント
Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh 2014 年 3 月 13 日
I rather this anonymous way of defining a function! It's way easier. I also know another way of doing this, surprisingly nobudy mentioned that so far! lol I'm gonna put it in the answers.

サインインしてコメントする。


Junaid
Junaid 2012 年 1 月 8 日
Dear Richard,
To define a function in matlab you can do following syntax of given function:
function n = F(x)
n= 2*x^3+7*x^2+x;
that is it. You can put end at the end of function. But it is also acceptable not to put to various matlab versions. If you put end for one function then you have to put for all function in single m file.
then you can generate random numbers, either integer or double, and can get the values of this function.
  1 件のコメント
Richard
Richard 2012 年 1 月 8 日
Thanks, Junaid! :)

サインインしてコメントする。


cyril
cyril 2014 年 3 月 21 日
編集済み: cyril 2014 年 3 月 21 日
> f = @(x) 2*x^3+7*x^2+x;
> f(0)
0
surprising no one mentioned anonymous functions...
  1 件のコメント
Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh 2014 年 3 月 21 日
@ Cyril
Walter did, just make sure you checked the other answers and comments!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFunction Handles についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by