Can MATLAB anonymous function return two outputs?
Yes, for example
>> f = @(x) deal(x, x^2);
>> [a, b] = f(2)
a =
2
b =
4
But then you will always need to call it with two output arguments. The following will not work
>> a = f(3)
Error using deal (line 37)
The number of outputs should match the number of inputs.
So it is better to write a function in seperate file to handle such cases.
"anonymous function can only have one "executable" statement"
It means that you cannot do something like the following in an anonymous function
f = @(x) y=1, x+y;
f = @(x) if x=1, y=2, end;
"if the function file name is "nonlinear_equations", then in the script file, I store a variable f = @nonlinear_equations, and then use f instead."
Yes, you can directly use nonlinear_equations instead of f; there will be no difference. However, it is not entirely useless. For example, instead of nonlinear_equations, you need to use some other function. In the current code, you will only need to change the line: f = @new_function; however, in the other case, you will need to change it everywhere.
Best Answer