Need to apply trapezoidal rule for double integration with an array by using the for loop

z=(0:0.1:10);
F1= @(r1,r2)exp(-(r1.^2+r2.^2).*A).*exp(-i*B.*z.*(r1.^2+r2.^2)).*besseli(n,r1.*r2./(sig^2)).*r1.*r2;
I want to use trapezoidal numerical integration on the F1 for the variables r1 and r2 for which limits are 0 to 1, where z is a matrix. The direct command of integral2 cannot be applied due to the array function. I am stuck at first loop, where the error "Conversion to double from function handle is not possible" is showing. Please help. Thanks in advance.
for i=1:m
xi=i*h;
sumx(i)=@(r2)F1(xi);
i=i+1;
end

Answers (1)

There are two things that need correction:
1) Remove the line i=i+1. This is what the for loop does. (also don't use "i" in general because i=sqrt(-1)))
2) The funciton handle. I will try to explain how function handles work. I have a function:
F = @(a,b,X1,X2)a*X1.^2 + b*X2.^2;
a,b are parameters. I will set them, for example a=1, b=2;
Now i want to loop over X1, given values of a, b, and X2:
a=1;
b=2;
X2 = 0.1;
h=0.001;
y = NaN(1,m);
for ii = 1:m
X1 = ii*h;
y(1,ii) = F(a,b,X1,X2);
end
Now the value of F, given a,b,X1(ii),X2 are assigned to y(ii). I hope that clarifies something.

5 Comments

Thanks it helps. But I have still a doubt, if X2 is considered variable then how this loop will be applicable because @(r2) gives warning of "conversion to double from function handle is not possible" and otherwise it is unidentified parameter.
Can you explain a bit better what you want to calculate in your loop? Because I think it is just a syntax error.
If you want to keep the value of x1 constant, and loop over values of x2, then you need to specificy:
x1 = some value;
and then you can simply evaluate:
for ii = ...
x2 = ii*h;
y = F(x1,x2)
end
I mean, if you loop over x2, then x1 still needs to have some value...
In trapezoidal method, first we apply loop over first variable, keeping second variable as it is. Now, when I am trying to apply over first variable, the error of "Conversion to double from function handle is not possible" is not resolved. When I try below command
Q = integral(@(r1) integral(@(r2)F1(r1,r2) ,0,1, 'ArrayValued',true), 0,1, 'ArrayVrealued',true)
the matlab keeps on processing. So, basically I need help in applying over first variable but not giving any values for second variable and keep the second variable for second time of the loop.
Thanks for your reply and also for your efforts.
You NEED to specify the value of BOTH r1 and r2, when you want to evaluate F1. Otherwise, how should matlab know hwo to calculate the value of F1
Anyway, what i think you want to do is:
%loop over value of r1:
for ii = 1:m
r1 = ii*h
% take 1d integral for r2 (given value of r1)
f_int_r2 = @(r2)F1(r1,r2);
q(1,ii) = integral(f_int_r2,0,1);
end
Anyway, I am pretty sure you can do this with the function integral2. Just it's not clear how z comes into play... is it a 3rd variable? Or you want to know the double integral for each value of z? Then you should loop over z...

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

on 22 Jun 2021

Commented:

on 24 Jun 2021

Community Treasure Hunt

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

Start Hunting!