# forming a function handles in matrix

7 views (last 30 days)
Bathala Teja on 4 Oct 2021
Commented: Bathala Teja on 6 Oct 2021
I want to form a set of function handles in a row matrix.
i wrote script like below.
w = 2;
Nr = 20
nr = @(phi)zeros(1, Nr);
wr = @(phi)zeros(1, Nr);
for n = 1:Nr
for i = 1:w
Awr = 50*(cos(i*2)-cos(i*9));
nr(1, n) = @(phi)nr(1, n)+Awr*cos(i*1.7);
end
wr(1, n) = @(phi)nr(1, n)
end
Iam getting the below error
Nonscalar arrays of function handles are not allowed; use cell arrays instead.
how to rectify this??

Steven Lord on 4 Oct 2021
MATLAB used to allow nonscalar arrays of function handles, but that functionality was removed probably 10 to 15 years ago. As the error message suggested, store your function handles in a cell array instead.
Stephen23 on 6 Oct 2021
Edited: Stephen23 on 6 Oct 2021
@Bathala Teja: function handles cannot be multiplied (or have any numeric operation applied to them). But you can certainly evaluate the function handles (just as Steven Lord showed) and multiply their outputs:
f{1}(3)*f{2}(3)
% ^^^ ^^^ Evaluate!
Bathala Teja on 6 Oct 2021
See this
Here i performed multiflication of two function handles and after that i integrated total function.
What is the difference in between this one and above one?
m = 10;
nAi = @(phi)zeros(1, 1);
for i=1:2:m
Ams = (2/(pi*i))*sin(pi*i/3)*(1+(2*cos(pi*i/9)));
nAi = @(phi)nAi(phi)+Ams*cos(2*i*phi);
end
nA = @(phi)50+nAi(phi)
nA = function_handle with value:
@(phi)50+nAi(phi)
wA = @(phi)nAi(phi)
wA = function_handle with value:
@(phi)nAi(phi)
nBi = @(phi)zeros(1, 1);
for i=1:2:m
Ams = (2/(pi*i))*sin(pi*i/3)*(1+2*cos(pi*i/9));
nBi = @(phi)nBi(phi)+Ams*cos(2*i*(phi-((2*pi/3)/4)));
end
nB = @(phi)50+nBi(phi)
nB = function_handle with value:
@(phi)50+nBi(phi)
wB = @(phi)nBi(phi)
wB = function_handle with value:
@(phi)nBi(phi)
Lab = integral(@(phi)(nA(phi).*wB(phi)), 0, 2*pi)
Lab = 3.9695

### Categories

Find more on Particle Swarm in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!