Asked by Richard
on 8 Jan 2012

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.

Answer by Chandra Kurniawan
on 8 Jan 2012

Accepted Answer

Hi, Richard.

First, you need to create a new m-file then type this code

function y = f(x) y = 2 * (x^3) + 7 * (x^2) + x;

Save with filename 'f.m'

Then create a new m-file and type this code

x = randi(7); y = f(x)

**randi** will generate random number

Chandra Kurniawan
on 8 Jan 2012

to evaluate this function just type :

y = feval(@f,x)

Richard
on 8 Jan 2012

Thanks, Chandra! :)

Salaheddin Hosseinzadeh
on 13 Mar 2014

The problem or lets say the dificulty of this code is that this couple of lines should be saved as a function and I personally don't wanna do that for a simple function like this! or even complicated ones!

Not my favorite way of defining a function

Log in to comment.

Answer by Walter Roberson
on 8 Jan 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.

Salaheddin Hosseinzadeh
on 13 Mar 2014

Log in to comment.

Answer by Junaid
on 8 Jan 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.

Answer by cyril
on 21 Mar 2014

Edited by cyril
on 21 Mar 2014

> f = @(x) 2*x^3+7*x^2+x; > f(0) 0

surprising no one mentioned anonymous functions...

Salaheddin Hosseinzadeh
on 21 Mar 2014

@ Cyril

Walter did, just make sure you checked the other answers and comments!

Log in to comment.

Answer by samy youssef
on 11 Mar 2015

Edited by Walter Roberson
on 26 Sep 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

Walter Roberson
on 26 Sep 2016

Okay... but irrelevant to the original question.

Log in to comment.

Answer by Nikitha Challa
on 26 Sep 2016

x = x + a/x 2 in matlab code

John D'Errico
on 26 Sep 2016

Not a function at all. This is not even valid MATLAB code as written.

Walter Roberson
on 26 Sep 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

Log in to comment.

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 2 Comments

## Leia Sofia Mendez (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/25568-defining-functions#comment_472138

This was the code I was trying to write:

This code gave an error saying that my function (convtime) was undefined. How you define a function in MATLAB?

## Walter Roberson (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/25568-defining-functions#comment_472149

Leia Sofia Mendez:

convtime appears to be from the File Exchange https://www.mathworks.com/matlabcentral/fileexchange/28204-time---rate-unit-conversion-function .

You should go to that link, and click the download button, and download the .zip file. You should unzip to a directory that is

notunder your MATLAB installation directory. You would then use pathtool in MATLAB to add that directory to your MATLAB path.Log in to comment.