How can I put an integral inside a for loop when the bounds depend on the loop's variable?
Show older comments
Hi, I would like to do this integration where the j of cj is the loop variable.

I tried this but when I calculate Phi_ij1(1,1) for example, Matlab says "unrecognized function or variable 'y' ". Can you help me?
for j=1:13
x_max1 = @(y,j) (-sqrt(3)/3).*y + c(j);
y_min1 = @(j) (sqrt(3)/2)*c(j);
y_max1 = @(j) c(j)*sqrt(3);
Phi_ij1 = @(i,j) integral2(B_zi(i,x,y), 0, x_max1(y,j),0,y_max1(j)) - integral2(B_zi(i,x,y),0, x_max1(y,j),y_min1(j),y_max1(j)) ;
end
5 Comments
darova
on 1 Apr 2020

sawdiia_too
on 1 Apr 2020
sawdiia_too
on 1 Apr 2020
darova
on 1 Apr 2020
Try to specify variables integral depends on
Phi_ij1 = @(i,j) integral2(@(x,y)B_zi(i,x,y), 0, @(y)x_max1(y,j),0,y_max1(j)) - ...
integral2(@(x,y)B_zi(i,x,y), 0, @(y)x_max1(y,j),y_min1(j),y_max1(j)) ;
sawdiia_too
on 1 Apr 2020
Accepted Answer
More Answers (1)
Torsten
on 2 Apr 2020
1 vote
Phi_ij1 =@(i,j) integral2(@(y,x) B_zi(i,x,y),0,y_min1(j),0,@(y)x_max1(y,j));
7 Comments
sawdiia_too
on 2 Apr 2020
Torsten
on 2 Apr 2020
Note that @(y,x) B_zi(...) instead of @(x,y) B_zi(...) because you changed the order of x- and y-integration.
This is wrong in Darova's answer.
sawdiia_too
on 2 Apr 2020
Torsten
on 2 Apr 2020
Note also that you only need to call integral2 once because you can calculate the difference of the two integrals as one integral with new y-limits as shown in my answer.
sawdiia_too
on 2 Apr 2020
Torsten
on 2 Apr 2020
integral 0 -> ymax1 - integral ymin1 -> ymax1 = integral 0 -> ymin1 (outer integral)
sawdiia_too
on 2 Apr 2020
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!