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:
http://www.mathworks.com/help/matlab/matlab_prog/create-functions-in-files.html.
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:
https://www.mathworks.com/help/matlab/matlab_prog/creating-a-function-handle.html. Answer from Chandra Kurniawan on mathworks.com
MathWorks
mathworks.com › matlab › programming
Functions - MATLAB & Simulink
Scope Variables and Generate Names Share data between functions or workspaces, generate valid variable names · Error Handling Generate, catch, and respond to warnings and errors · Programming: Structuring Code (MathWorks Teaching Resources) You clicked a link that corresponds to this MATLAB ...
MathWorks
mathworks.com › matlab › programming › functions › function creation
function - Declare function name, inputs, and outputs - MATLAB
This MATLAB function declares a function named myfun that accepts inputs x1,...,xM and returns outputs y1,...,yN.
Videos
18:58
How to Create Functions in MATLAB | Writing Functions in MATLAB ...
09:01
How to Create a MATLAB Function - MATLAB
03:22
Functions with Multiple Inputs or Outputs - MATLAB
01:37
Using MATLAB Functions - MATLAB
25:16
How to use for loop in MATLAB - YouTube
07:17
MATLAB for Engineers - Introduction to for Loops (Part 1 of 4): ...
TutorialsPoint
tutorialspoint.com › matlab › matlab_functions.htm
MATLAB - Functions
A function is a group of statements that together perform a task. In MATLAB, functions are defined in separate files. The name of the file and of the function should be the same.
MathWorks
mathworks.com › matlab › programming › functions
Function Creation - MATLAB & Simulink
There are several types of functions available with MATLAB®, including local functions, nested functions, private functions, and anonymous functions.
MathWorks
mathworks.com › matlab › programming › functions › function creation
Create Functions in Files - MATLAB & Simulink
Program files can contain multiple functions. If the file contains only function definitions, the first function is the main function, and is the function that MATLAB associates with the file name. Functions that follow the main function or are included in script code are called local functions.
MathWorks
mathworks.com › matlab › programming › functions › function creation
Types of Functions - MATLAB & Simulink
There are several types of functions available with MATLAB, including local functions, nested functions, private functions, and anonymous functions.
MathWorks
mathworks.com › symbolic math toolbox › code generation
matlabFunction - Convert symbolic expression to function handle or file - MATLAB
To generate a MATLAB function with input arguments that are vector variables, specify the Vars name-value argument as a cell array. Create a symbolic expression that finds the dot product of two 1-by-3 vectors. ... Convert the expression f to a MATLAB function.
MathWorks
mathworks.com › matlab › language fundamentals › data types
Function Handles - MATLAB & Simulink
A function handle is a MATLAB® data type that represents a function. A typical use of function handles is to pass a function to another function.
MathWorks
mathworks.com › matlab › language fundamentals › data types › function handles
functions - Information about function handle - MATLAB
This information includes the function name, type, and file name.
MathWorks
mathworks.com › simulink › simulation integration › create large-scale model components › integrate external code into simulink › integrate matlab code into simulink
MATLAB Function - Include MATLAB code in Simulink models - Simulink
The MATLAB Function block enables you to write MATLAB functions that execute in Simulink models.
MathWorks
mathworks.com › matlab › language fundamentals › data types › function handles
Create Function Handle - MATLAB & Simulink
An anonymous function is a one-line expression-based MATLAB function that does not require a program file. Construct a handle to an anonymous function by defining the body of the function, anonymous_function, and a comma-separated list of input arguments to the anonymous function, arglist.
Portland State University
web.cecs.pdx.edu › gerry › MATLAB › programming › basics.html
MATLAB Functions -- Basic Features
In addition to providing the obvious support for interactive calculation, it also is a very convenient way to debug functions that are part of a bigger project. MATLAB functions have two parameter lists, one for input and one for output. This supports one of the cardinal rules of MATLAB ...
ScienceDirect
sciencedirect.com › topics › engineering › matlab-function
Matlab Function - an overview | ScienceDirect Topics
A MATLAB function is defined as a programmable routine in MATLAB that performs a specific task, such as finding the minimum of an objective function in the case of the fminunc function, which is used for unconstrained non-linear optimization.
MathWorks
mathworks.com › matlab › language fundamentals › data types › function handles
function_handle - Handle to function - MATLAB
A function handle is a MATLAB® data type that represents a function. A typical use of function handles is to pass a function to another function. For example, you can use function handles as input arguments to functions that evaluate mathematical expressions over a range of values.
MathWorks
mathworks.com › matlab › language fundamentals › data types › function handles
Parameterizing Functions - MATLAB & Simulink
MATLAB function functions evaluate mathematical expressions over a range of values. They are called function functions because they are functions that accept a function handle (a pointer to a function) as an input. Each of these functions expects that your objective function has a specific ...
MathWorks
mathworks.com › simulink › modeling › modeling guidelines › high-integrity system modeling › simulink block considerations
MATLAB Functions - MATLAB & Simulink
High-integrity guidelines for MATLAB Functions.
MathWorks
mathworks.com › matlab › programming › functions › function creation
Local Functions - MATLAB & Simulink
This topic explains the term local function, and shows how to create and use local functions. MATLAB® program files can contain code for more than one function. In a function file, the first function in the file is called the main function. This function is visible to functions in other files, ...