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

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

Do you need to be storing the function handles in an array every time round the loop given you use them on the next line? Do you intend to use them again later?
If so you need to save them in a cell array as you cannot save function handles in a regular array so I'm not sure why this line:
K(m) = @(r)(exp(-1i*k*r(m)))/(r(m))
wouldn't throw an error 2nd time round the loop before the one you mention, which I would assume is on the following line.
You can just put a breakpoint in or use 'Pause on Errors' from the 'Run' dropdown menu to investigate what is actuallu happening though.
@Adam, i do need to store the value in each loop.
@ Kalyan , I tried it and i'm getting without loop but with loop it's not working.
Thank you @ Adam ,@ Kalyan for your help.

댓글을 달려면 로그인하십시오.

 채택된 답변

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개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2020년 2월 13일

댓글:

2020년 2월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by