The best way to create user-defined functions in Matlab is to type them in as function m-files.



To create a new function called "function_name", open a new m-file by selecting from the menu buttons:
File - New - M-File.
This will open an editor window.

Type in the definition of the function with statements such as:
function y=function_name(x)
y= 2*x.^3+ 3*x.^2 +4*log(x)-exp(x);

Then save the file as an m-file in the work subdirectory as:
function_name.m
It must be saved as the same name that appears in the function statement in order to avoid confusion.


To see the value of the function for a given value of x, say x=5, simply type in the command window:
function_name(5)
or
feval('function_name',5)
or
a=function_name(5)


To see multiple values of the function for multiple values of x, say x=1:10, simply type in the command window:
function_name(1:10)
or
feval('function_name',1:10)
or
a=function_name(1:10)


To plot the function between limits, say, x=1 and x=8, simply type in the command window:
fplot('function_name',[1 8])