Defining function handles in MATLAB

1.148 visualizaciones (últimos 30 días)
Richard
Richard el 8 de En. de 2012
Comentada: Voss el 4 de Feb. de 2023
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 comentarios
Leia Sofia Mendez
Leia Sofia Mendez el 26 de Jul. de 2017
Editada: Walter Roberson el 26 de Jul. de 2017
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 el 26 de Jul. de 2017
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.

Iniciar sesión para comentar.

Respuesta aceptada

Chandra Kurniawan
Chandra Kurniawan el 8 de En. de 2012
Editada: MathWorks Support Team el 22 de Mayo de 2019
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 comentarios
Piyush Gade
Piyush Gade el 4 de Feb. de 2022
Whatever this is, this doesn't work.
Mark Jackson
Mark Jackson el 11 de Oct. de 2022
I recommend clicking 'Getting Started.'

Iniciar sesión para comentar.

Más respuestas (6)

Walter Roberson
Walter Roberson el 8 de En. de 2012
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 comentario
Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh el 13 de Mzo. de 2014
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.

Iniciar sesión para comentar.


Junaid
Junaid el 8 de En. de 2012
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.

cyril
cyril el 21 de Mzo. de 2014
Editada: cyril el 21 de Mzo. de 2014
> f = @(x) 2*x^3+7*x^2+x;
> f(0)
0
surprising no one mentioned anonymous functions...
  1 comentario
Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh el 21 de Mzo. de 2014
@ Cyril
Walter did, just make sure you checked the other answers and comments!

Iniciar sesión para comentar.


samy youssef
samy youssef el 11 de Mzo. de 2015
Editada: Walter Roberson el 26 de Sept. de 2016
here is a function i developed to calculate the log of any number with different base:
function d =log_for_diff_base(myNumber,myBase)
x=log(myNumber);
y=log(myBase);
d=x/y;
end
  1 comentario
Walter Roberson
Walter Roberson el 26 de Sept. de 2016
Okay... but irrelevant to the original question.

Iniciar sesión para comentar.


Nikitha Challa
Nikitha Challa el 26 de Sept. de 2016
x = x + a/x 2 in matlab code
  2 comentarios
John D'Errico
John D'Errico el 26 de Sept. de 2016
Editada: John D'Errico el 26 de Sept. de 2016
Not a function at all. This is not even valid MATLAB code as written.
Walter Roberson
Walter Roberson el 26 de Sept. de 2016
That does not appear to be a question, and it is not an Answer to what was asked here?
If the question is to solve the equation
x == x + a/(x^2)
then for finite a values, the solutions are -inf and +inf as a/(x^2) would be 0 for those values, leading to the equality -inf == -inf and +inf == +inf

Iniciar sesión para comentar.


Karima Benkhlil
Karima Benkhlil el 4 de Feb. de 2023
f(x) = exp(x^2 ) (x3+4x+1) how to define it

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by