MATLAB Answers

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

2 views (last 30 days)
sonu kumar
sonu kumar on 13 Feb 2020
Commented: sonu kumar on 14 Feb 2020
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 Comments

Show 2 older comments
sonu kumar
sonu kumar on 13 Feb 2020
@ Kalyan , I tried it and i'm getting without loop but with loop it's not working.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 13 Feb 2020
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

More Answers (0)

Sign in to answer this question.