Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Defining functions

Asked by Richard

Richard (view profile)

on 8 Jan 2012
Latest activity Edited by samy youssef

samy youssef (view profile)

on 11 Mar 2015

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.

0 Comments

Richard

Richard (view profile)

Tags

Products

6 Answers

Answer by Chandra Kurniawan

Chandra Kurniawan (view profile)

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

3 Comments

Chandra Kurniawan

Chandra Kurniawan (view profile)

on 8 Jan 2012

to evaluate this function just type :
y = feval(@f,x)

Richard

Richard (view profile)

on 8 Jan 2012

Thanks, Chandra! :)

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

Chandra Kurniawan

Chandra Kurniawan (view profile)

Answer by Junaid

Junaid (view profile)

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.

1 Comment

Richard

Richard (view profile)

on 8 Jan 2012

Thanks, Junaid! :)

Junaid

Junaid (view profile)

Answer by Walter Roberson

Walter Roberson (view profile)

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.

1 Comment

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.

Walter Roberson

Walter Roberson (view profile)

Answer by cyril

cyril (view profile)

on 21 Mar 2014
Edited by cyril

cyril (view profile)

on 21 Mar 2014
> f = @(x) 2*x^3+7*x^2+x;
> f(0)
0

surprising no one mentioned anonymous functions...

1 Comment

@ Cyril

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

cyril

cyril (view profile)

Answer by Pratik Bhattarai

Pratik Bhattarai (view profile)

on 7 May 2014

i want to solve this function M*D^(X)|+C*D(x)+K*X=F(T); to get response X(T). for any value of m c k & ft....

0 Comments

Pratik Bhattarai

Pratik Bhattarai (view profile)

Answer by samy youssef

samy youssef (view profile)

on 11 Mar 2015
Edited by samy youssef

samy youssef (view profile)

on 11 Mar 2015
if true
  % code
end

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

0 Comments

samy youssef

samy youssef (view profile)

Contact us