Cannot input function handles into array
4 views (last 30 days)
Show older comments
Caleb Nickens
on 1 Feb 2021
Commented: Caleb Nickens
on 2 Feb 2021
basisN0 = @(x)[ 1 ];
basisN1 = @(x)[ 1, x ];
basisN2 = @(x)[ 1, x, x^2 ];
basisN3 = @(x)[ 1, x, x^2, x^3 ];
When I use the command size(), I am told that all of these variables are 1x1, while I need them to be 1x1,1x2,1x3 and 1x4.
f = @(x)x;
basisN0 = { 1 };
basisN1 = { 1, f };
basisN2 = { 1, f, f^2 };
basisN3 = { 1, f, f^2, f^3 };
I also tried inputting the variables like this but am told that I cant square and cube the function handle.
f = @(x)x;
fsqr = @(x)x^2;
fcub = @(x)x^3;
basisN0 = { 1 };
basisN1 = { 1, f };
basisN2 = { 1, f, fsqr };
basisN3 = { 1, f, fsqr, fcub };
Lastly, I tried this approach and achieved the correct array sizes. However, I had to utilize three different function handles and cannot plug in numbers to the arrays. For example, basisN3(5) would not give me [1 5 25 125]. Please help explain how I would input these variables to achieve my desired outcome.
0 Comments
Accepted Answer
James Tursa
on 2 Feb 2021
Edited: James Tursa
on 2 Feb 2021
When you do this:
basisN0 = @(x)[ 1 ];
basisN1 = @(x)[ 1, x ];
basisN2 = @(x)[ 1, x, x^2 ];
basisN3 = @(x)[ 1, x, x^2, x^3 ];
the size of basisN0, basisN1, basisN2, and basisN3 are all 1x1 because they are just function handles. The result of using the function handle with an input value will depend on what the function handle does. So basisN3(5) certainly would give you a 1x4 vector consisting of [1 5 25 125]. Did you actually try it? E.g.,
>> basisN3 = @(x)[ 1, x, x^2, x^3 ];
>> size(basisN3)
ans =
1 1
>> size(basisN3(5))
ans =
1 4
>> basisN3(5)
ans =
1 5 25 125
More Answers (1)
Walter Roberson
on 2 Feb 2021
f = @(x)x;
Pow = @(f,n) @(x)f(x).^n
basis = @(f,x) cellfun(@(F)F(x), f, 'uniform', 0)
basisN0 = { Pow(f,0) };
basisN1 = { Pow(f,0), Pow(f,1) };
basisN2 = { Pow(f,0), Pow(f,1), Pow(f,2) };
basisN3 = { Pow(f,0), Pow(f,1), Pow(f,2), Pow(f,3) };
basis(basisN3,5)
and the individual basisN have the size you require.... they just can't be executed directly.
The above code does not assume that your x is scalar, so if you passed in [3,5] for example, it would return a cell array where each element was length 2. If you wished to change that, it would be easy enough: just use
basis = @(f,x) cellfun(@(F)F(x), f)
Individual function handles are always scalar. This is because there is an semantic conflict between potentially indexing an array of function handle (indexing uses () ) and invoking the vector all on a single set of arguments (invocation uses () too.) Does f(3) mean to return the third element of the vector of function handles, or does it mean to invoke the vector with parameter 3? That might sound obvious at first, that if it is a vector "of course" it should index, but remember that f = @(x)x means that f is a vector of function handles of length 1, so f(1) would be conflicting between indexing f to return the one function handle, compared to invoking the one function handle with argument 1.
To prevent this semantic conflict, you cannot use create [] lists of function handles, only {} lists (or store them in struct or tables or whatever.)
The symbolic toolbox has the same conflict:
syms f1(t) f2(t)
f = [f1, f2]
Now is f(1) indexing f to return function f1, or is it invoking f with parameter 1? The symbolic toolbox resolves it by saying that creating a list of symbolic functions is to be interpreted as creating a single symbolic function that returns a list:
f(t) = [f1(t), f2(t)]
and that cannot be indexed by any normal method to extract the f1 part without executing the function and indexing the result. (There are internal ways to break it up for symbolic processing though.)
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!