Is there any easy way to calculate integral with cylinder coordinates in Matlab numerically?
조회 수: 16 (최근 30일)
이전 댓글 표시
Is there any easy way to calculate integral with cylinder coordinates in Matlab numerically? (3 dimensional space)
Can I use: integral3 for this work?
댓글 수: 1
채택된 답변
John D'Errico
2022년 1월 19일
편집: John D'Errico
2022년 1월 19일
It seems pretty easy. Go back to calc 101. Maybe 102. WTP?
For example, what is the integral of z*(x^2+y^2), over a unit cylinder. So a cylinder of unit radius, centered at the origin, where lets say the cylinder has base at z==0, and top at z==1. Just making things up, so I have no idea what the solution is. We can trivially do it analytically. (Actually, pencil and paper should suffice too.)
syms z r theta
x = r*cos(theta);y = r*sin(theta);
Kernel = z*(x^2 + y^2);
int(int(int(r*Kernel,theta,[0,2*pi]),r,[0,1]),z,[0,1])
I hope you remembered the differential element has volume r*dr*dtheta*dz. Consider that extra factor of r in there, multiplied by Kernel. That is important.
But can we do it numerically? Again, of course. Make sure you use the dotted operators, for exponentiation and multiplication. Your function needs to be properly vectorized.
x = @(r,theta) r.*cos(theta);
y = @(r,theta) r.*sin(theta);
Kernel = @(r,theta,z) z.*(x(r,theta).^2 + y(r,theta).^2);
integral3(@(r,theta,z) r.*Kernel(r,theta,z),0,1,0,2*pi,0,1)
That should look very much like pi/4.
A simple Monte Carlo should also work. I'll just use rejection to do the sampling. And I'll sample in the first quadrant only, then multiply by 4.
n = 1e8;
x = rand(n,1);
y = rand(n,1);
z = rand(n,1);
keep = sqrt(x.^2 + y.^2) <= 1;
x = x(keep,1);
y = y(keep,1);
z = z(keep,1);
4*sum(z.*(x.^2 + y.^2))/n
But is it easy? Easy is difficut to quantity, since easy for one person is not necessarily easy for another.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Solvers에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!