# How to vectorize integral2?

12 views (last 30 days)

Commented: 祥宇 崔 on 10 Apr 2023
We can use 'integral' for twice only when these two variables are independent to each other. And then the option 'Arrayvalued' could be used then. But what if we can't seperate them? For instance:
fun = @(n,x,y) sin(n*x-y).*sqrt(x-n*y);
for i=1:1e3
integral2(@(x,y) fun(i,x,y),0,2*pi,0,2*pi);
end
Is there anaway to speed it up by vectorizing it?

Walter Roberson on 10 Apr 2023
No, there is no way to speed that up by vectorizing.
In general, if you have multiple functions to calculate at the same time, then they might well have different convergence properties. integral2() would have no way of splitting up a vector-valued function into its different components, so if it did support vector-valued functions, it would end up having to evaluate every segment according to the worst convergence over that segment.
Walter Roberson on 10 Apr 2023
By the way, under the assumption that n is a positive integer, there does turn out to be a closed form solution
-pi^(1/2)*(-n^3*(fresnels(2/n^(1/2))-fresnels((-2*n+2)^(1/2)/n^(1/2)*2^(1/2)))* ...
cos((2*n^2-2)*pi/n)-n^3*(fresnelc(2/n^(1/2))-fresnelc((-2*n+2)^(1/2)/n^(1/2)* ...
2^(1/2)))*sin((2*n^2-2)*pi/n)+1i*n^3*fresnels(2)+fresnels(2*1i*n)+...
fresnels(2*n^(1/2))-fresnels((-2*n+2)^(1/2)*2^(1/2)*n^(1/2)))/n^(3/2)*2^(1/2)/(2*n^2-2)

Thanks a lot!

### Categories

Find more on Logical in Help Center and File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!