From: Alex on
Hi all,

I am integrating a function as follows

clc;
clear;

R = 1;
y = 0.2:0.1:50;

for i = 1:length(lamdba);
F4(i) = @(phi,theta)exp(j*(2*pi/y(i))*R*sin(theta)*cos(phi));
Q4(i) = dblquad(F4(i),0,2*pi,0,pi);
end


I am getting the following message:


??? Input argument "theta" is undefined.

I do not understand why when the integration is in a loop it does not work. As you might have guessed I want to evaluate/integrate this function for many values of y so It would be very helpful if I could do this in a loop.

I would appreciate anyone's help

Cheers

Alex
From: Steven Lord on

"Alex" <mammasis82(a)hotmail.com> wrote in message
news:22733192.1210077712230.JavaMail.jakarta(a)nitrogen.mathforum.org...
> Hi all,
>
> I am integrating a function as follows
>
> clc;
> clear;
>
> R = 1;
> y = 0.2:0.1:50;
>
> for i = 1:length(lamdba);
> F4(i) = @(phi,theta)exp(j*(2*pi/y(i))*R*sin(theta)*cos(phi));

The only reason this doesn't throw a warning ...

> Q4(i) = dblquad(F4(i),0,2*pi,0,pi);

is because this line threw an error when i was equal to 1. You can't create
a vector of function handles; using F(k) notation does not extract a
function handle from a vector, it evaluates the function handle F with one
input, k. MATLAB interprets your DBLQUAD call as evaluating F4 [which is
scalar for i = 1, and is why the previous line didn't warn] with one input,
1, and passing the output of that call into DBLQUAD as the integrand
function. However, on the line above, you've defined F4 to be an anonymous
function that expects two inputs.

Use a cell array of function handles if you need to keep all the anonymous
functions around:


R = 1;
y = 0.2:0.1:50;
% Preallocating Q4
Q4 = zeros(size(y));

for i = 1:length(y) % I think you meant y instead of lambda
F4{i} = @(phi,theta)exp(j*(2*pi/y(i))*R*sin(theta)*cos(phi));
Q4(i) = dblquad(F4{i},0,2*pi,0,pi);
end


or simply use a scalar anonymous function if you don't.


R = 1;
y = 0.2:0.1:50;
% Preallocating Q4
Q4 = zeros(size(y));

for i = 1:length(y)
F4 = @(phi,theta)exp(j*(2*pi/y(i))*R*sin(theta)*cos(phi));
Q4(i) = dblquad(F4,0,2*pi,0,pi);
end


--
Steve Lord
slord(a)mathworks.com