forming a function handles in matrix

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??

回答 (1 件)

Steven Lord
Steven Lord 2021 年 10 月 4 日

0 投票

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.

7 件のコメント

Bathala Teja
Bathala Teja 2021 年 10 月 4 日
Can you show me how to do that?
thanks in advance
Steven Lord
Steven Lord 2021 年 10 月 4 日
f = {@sin, @cos, @(t) t.^2}
f = 1×3 cell array
{@sin} {@cos} {@(t)t.^2}
y = f{3}(1:10) % returns the first 10 perfect squares
y = 1×10
1 4 9 16 25 36 49 64 81 100
fh = f{2} % returns the function handle @cos
fh = function_handle with value:
@cos
Bathala Teja
Bathala Teja 2021 年 10 月 5 日
sorry i did mistake in giving my script
this is my script, now iam expecting array(contains Nr = 20 elements) of function handles as output
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*(phi+2*n));
end
wr(1, n) = @(phi)nr(1, n);
end
NR = @(phi)nr(phi)+(3*56/2)
Steven Lord
Steven Lord 2021 年 10 月 5 日
Creating an array of function handles is not allowed. It will not work.
Creating a cell array containing function handles is allowed.
f = cell(1, 3);
for k = 1:3
f{k} = @(x) x.^k + k*x;
end
% Use the elements of f by indexing then passing the input
y = f{2}(1:10) % Evaluate the function handle corresponding to k = 2 at x = 1:10
y = 1×10
3 8 15 24 35 48 63 80 99 120
check = (1:10).^2 + 2*(1:10) % Manual check, will be the same as y
check = 1×10
3 8 15 24 35 48 63 80 99 120
Bathala Teja
Bathala Teja 2021 年 10 月 6 日
Script is fine.
If i want to do f{1}*f{2}, it is giving error.
How to do this operation.
Stephen23
Stephen23 2021 年 10 月 6 日
編集済み: Stephen23 2021 年 10 月 6 日
@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
Bathala Teja 2021 年 10 月 6 日
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

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeData Type Identification についてさらに検索

製品

リリース

R2021b

質問済み:

2021 年 10 月 4 日

コメント済み:

2021 年 10 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by