Let's say I want to store the function y(x) = x + 2 in a variable.
Is there any way to save y = x + 2, and access as y(x)? For example, y(2)?
An anonymous function is what you're looking for:
>> y = @(x) x+2;
>> y(2)
ans =
4
@(x) x+2, and y is a function handle to that anonymous functionsyms x first?x in the anonymous function's definition is an ordinary variable, not a symbolic variable
ythe anonymous functions seem to be the way to go. However, for most you may want to just create regular functions. Note that you can have many functions in 1 file for easy handling.