A question using trapz integration

Hello all
I have a problem using the trapz integration. I’m trying to integrate this integral (in the attached file) using trapz twice when I’m trying to approximate the analytic solution to z = 0.
(For arctan(R/Z) far from pi/2 it is working ok). I’m taking z to be very small but final and it seems that the code I wrote doesn't handles it very well; I guess it is because tan diverges in pi/2.
(the analytic solution is pi*R^2)
Maybe there is another way to solve this problem?
Thanks
(In the next step I need to perform this integral where in the integrand I have a function which is dependent on theta and phi, And that is why I can’t just take the area of the circle)
The code I wrote:
theta_val_new = (round(linspace(0,pi./2,100).*100)/100)';
phi_val_new = linspace(0,2.*pi,120);
theta_val_mat = repmat(theta_val_new,1,120);
function_to_integrate = abs(tan(theta_val_mat)./(cos(theta_val_mat)).^2) .*(30./tan(theta_val_new(end,1))).^2;
energy1 = (trapz(phi_val_new,trapz(theta_val_new,function_to_integrate))

11 个评论

No file was attached.
Maybe we can help if you explain what original 2d-integral you are trying to solve.
Best wishes
Torsten.
Hello Torsten
I've attached the integral in a png file. the original integral is this integral where the integrand is multiplied by a function that depend on theta and phi (which I calculated numerically)
Thank you
Lilach
You write
And that is why I can’t just take the area of the circle
What is the original integral (over a 2d-region, I guess (maybe a circle ?)) you are trying to solve ? What are theta and phi for this region ?
Best wishes
Torsten.
the interval:
0<=phi<=2pi, 0<=theta<=atan(R/z)
the integrand:
tan(theta)*z^2/(cos(theta))^2
Thank you
It's not just tan(pi/2) that's unbounded, you also have 1/cos^2(theta) which also blows up at pi/2, although you might be able to show the z^2 cancels that out. How close does z actually get to 0?
I should point out that you can evaluate this integral exactly by remembering that tan = sin/cos and letting u = cos(theta). I got that it equals pi z^2 * (1/ cos^2(arctan(R/z)) - 1), but you should verify this yourself.
It's not possible to simply write your integral as
int_{phi=0}^{phi=2*pi} int_{r=0}^{r=R} f(r,phi)*r dr dphi
?
If not, I'd use MATLAB's integral2 instead of applying trapz twice. Or do you only have a list of values for your function f ?
Best wishes
Torsten.
Hi Torsten
I can't that because I need to integrate over the gamma function, and it is numerically calculated (from anther code I've created) for theta and phi. I can't express gamma in Polaric coordinate system, only in the azimuthal part of the sperical coordinates system.
the real integral i need to calculate is:
I see you added a second integral that can't be integrated exactly. Do you know if gamma is 0 at theta = pi/2 (for all phi)? If it isn't, then that integral is unbounded since Rz/cos^2(arctan(R/z)) goes to infinity as z goes to 0.
If you do wish to do this integral numerically for other values of z I agree with Torsten that integral2 is a much better route.

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by