MATLAB: Cannot input function handles into array

arraysfunction handleMATLAB

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 1×1, while I need them to be 1×1,1×2,1×3 and 1×4.
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.

Best Answer

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