Do vector fields mapping R^2 to R^2 have to be coded in separate function files, or can I define anonymous functions for them directly in the script file?

Hi,
I've been writing functions from R^2 to R^2, i.e. F( f_1(x,y), f_2(x,y) )= (z_1, z_2), in separate function files, and then calling F in a script file. Could I instead write the two component functions f_1 and f_2 as anonymous functions directly in the script file? I read something on the Mathworks documentation that an anonymous function can only have one "executable" statement, but I'm not sure what that means.
Thanks,

 Réponse acceptée

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; % not allowed
f = @(x) if x=1, y=2, end; % not allowed
"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.

9 commentaires

Hi Ameer,
What if F has two component functions, say
f_1 = cos(x) + y, and
f_2 = sin(y) + x^2,
so that the vector field F has outputs z = (cos(x) + y, sin(y) + x^2)?
Can I define an anonymous function for F, which has two component functions, or would I have to define F in a separate function file?
Thanks,
Yes, if it returns two values as a vector in a single variable, then you can define an anonymous function without any difference.
As long as it returns something that can be captured in a single variable, there is no issue.
Ah, I see what you're saying now.
So, define the single variable first, maybe:
x = [x(1), x(2)];
f_1 = cos(x(1)) + x(2);
f_2 = sin(x(2)) + (x(1))^2;
Then I can simply use this function handle directly in the script file:
F = @(x) (f_1, f_2)
Is that ok?
Thanks,
This is correct, but not useful. f_1 and f_2 are just constant values, and x is not doing anything in F. Try the following
f_1 = @(x) cos(x(1)) + x(2);
f_2 = @(x) sin(x(2)) + (x(1))^2;
F = @(x) [f_1(x), f_2(x)];
Not quite. If you want two functions:
f_1 = @(x) cos(x(1)) + x(2);
f_2 = @(x) sin(x(2)) + x(1)^2;
and want to have one function to evaluate them all (and in the darkness bind them ;) you need to have that one function evaluate them.
F = @(x) [f_1(x), f_2(x)];
F([1, 2])
Compare with the following, which returns a cell array each element of which contains a function handle.
G = @(x) {f_1, f_2};
G([1, 2])
If your functions are more complicated than can "fit" in an anonymous function consider writing a function file.
function [value, fcnHandles] = F(x)
value = [f_1(x), f_2(x)];
fcnHandles = {@f_1, @f_2};
end
function y = f_1(x)
% Do stuff to compute f_1
end
function y = f_2(x)
% Do stuff to compute f_2
end
This assumes you may need to call f_1 and f_2 outside F. Even though f_1 and f_2 are local functions inside F.m (and so normally out of scope outside of F.m) they can be called via their function handles that were created when they were in scope.
[value, fh] = F([1, 2]);
f_1 = fh{1};
v1 = f_1([1, 2]); % v1 should be equal to value(1)
If you don't need to call f_1 and f_2 outside F, you can eliminate the fcnHandles output.
Hi Ameer,
Ah, right. Ok, awesome -- thanks for your help. I'll have a lot less function files now - thanks!
Hi Steve,
Thanks for your comment; for this piece of code you suggested:
F = @(x) [f_1(x), f_2(x)];
F([1, 2])
What is F( [1, 2] ) for?
Wouldn't defining F by passing the anonymous functions f_1 and f_2 in be sufficient?
That's an example demonstrating that what I wrote works. F([1, 2]) should return a two element vector, the first element of which is f_1([1, 2]) and the second is f_2([1, 2]).
Hi Steve,
Ok, got it -- thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Centre d'aide et File Exchange

Produits

Version

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by