symfun
Create symbolic functions
Description
Examples
Create and Define Symbolic Functions
Define the symbolic function f(x,y) = x + y
. First, create the function by using syms
. Then define the function.
syms f(x,y)
f(x,y) = x + y
f(x, y) =
Find the value of f
at x = 1
and y = 2
.
f(1,2)
ans =
Define the function again by using the formal way.
syms x y f = symfun(x+y,[x y])
f(x, y) =
Return Body and Arguments of Symbolic Function
Return the body of a symbolic function by using formula
. You can use the body for operations such as indexing into the function. Return the arguments of a symbolic function by using argnames
.
Index into the symbolic function [x^2, y^4]
. Since a symbolic function is a scalar, you cannot directly index into the function. Instead, index into the body of the function.
syms f(x,y)
f(x,y) = [x^2, y^4];
fbody = formula(f);
fbody(1)
ans =
fbody(2)
ans =
Return the arguments of the function.
fvars = argnames(f)
fvars =
Combine Two Symbolic Functions
Create two symbolic functions.
syms f(x) g(x) f(x) = 2*x^2 - x; g(x) = 3*x^2 + 2*x;
Combine the two symbolic functions into another symbolic function with the data type symfun
.
h(x) = [f(x); g(x)]
h(x) =
Evaluate the function at and .
h(1)
ans =
h(2)
ans =
You can also combine the two functions into an array of symbolic expressions with the data type sym
.
h_expr = [f(x); g(x)]
h_expr =
Index into h_expr
to access the first and the second symbolic expressions.
h_expr(1)
ans =
h_expr(2)
ans =
Input Arguments
formula
— Function body
symbolic expression | vector of symbolic expressions | matrix of symbolic expressions
Function body, specified as a symbolic expression, vector of symbolic expressions, or matrix
of symbolic expressions that can be converted to sym
data
type
Example: x + y
inputs
— Input argument or arguments of function
symbolic variable | array of symbolic variables
Input argument or arguments of a function, specified as a symbolic variable or an array of symbolic variables, respectively.
Example: [x,y]
Data Types: sym
Output Arguments
f
— Symbolic function
symfun
object
Symbolic function, returned as a symfun
object.
While the data type of the function f
is
symfun
, the data type of the evaluated function, such
as f(1,2)
, is sym
.
Version History
Introduced in R2012a
Apri esempio
Si dispone di una versione modificata di questo esempio. Desideri aprire questo esempio con le tue modifiche?
Comando MATLAB
Hai fatto clic su un collegamento che corrisponde a questo comando MATLAB:
Esegui il comando inserendolo nella finestra di comando MATLAB. I browser web non supportano i comandi MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)