Please help to fix this error "First input argument must be a function handle".

3 次查看(过去 30 天)
For single iteration(without loop), i'm getting the integral result (there is no error of function handle). But for iteration (using loop) I'm not getting the same.
%% Hallen's integral for wire antenna
clc;
clear all;
close all;
%syms z;
a = 0.00065; % inner radius
h = 0.005;
f = 1e9;
c = 3e8;
eo = 8.8541878176e-12;
uo = (4*pi)*10^ -7;
eta = 377;
er = 1;
w = 2*pi*f;
k = (w*sqrt(er))/c;
N = 9;
vo = 1;
dz = (2*h)/N;
U = zeros(1,N);
zn = -h+(dz/2);
C = zeros(N,1);
B = zeros(N,1);
for m = 1:N
zm(m) = (m - 0.5)*dz - h;
r(m) = @(z) sqrt(a^2 + (zm(m)-z).^2);
K(m) = @(r)(exp(-1i*k*r(m)))/(r(m))
U(m) = double((1/(4*pi))* integral(K(m),zn-(dz/2),zn+(dz/2),'ArrayValued',true));
end
It poping up error message "First input argument must be a function handle". please, help me to fix this .
Thank you for help!
  5 个评论

请先登录,再进行评论。

采纳的回答

Steven Lord
Steven Lord 2020-2-13
for m = 1:N
zm(m) = (m - 0.5)*dz - h;
r(m) = @(z) sqrt(a^2 + (zm(m)-z).^2);
K(m) = @(r)(exp(-1i*k*r(m)))/(r(m))
U(m) = double((1/(4*pi))* integral(K(m),zn-(dz/2),zn+(dz/2),'ArrayValued',true));
end
You cannot create arrays of function handles in MATLAB. When m is equal to 1, it assigns to the first element of K. MATLAB should probably complain in some way whenever you try to assign a function handle into an element of an array using parentheses subscripted indexing, even if you're trying to assign into the first element, but it doesn't. So after that third line inside your loop executes we have:
K =
function_handle with value:
@(r)(exp(-1i*k*r(m)))/(r(m))
Now in your integral call you're passing K(m) as the first input. This is not the first element of the function handle array K (remember, you can't create arrays of function handles.) It instead evaluates K with the current value of m as input and attempts to pass whatever that evaluation returns into integral. But integral requires its first input to be a function handle, not a number. [Adam, this is why the second iteration of the for loop doesn't throw an error; the error occurs during the first iteration.]
As a bit of an aside, if you'd written a function handle that itself returns a function handle when evaluated, that would be different.
fh = @(k) @(x) x.^k;
y = fh(2); % Evaluating fh returns a function handle
z = y(1:10) % Evaluating the function handle returns the vector (1:10).^2
integral(fh(2), 0, 10) % 10^3/3 - 0^3/3 = 333.333 ...
If you need to keep all your function handles around, store them in a cell array.
fhCell = cell(1, 5);
for k = 1:5
fhCell{k} = @(x) x.^k;
end
fhCell{2}(1:10) % (1:10).^2
integral(fhCell{2}, 0, 10) % 1000/3

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by