Is there any easy way to calculate integral with cylinder coordinates in Matlab numerically?
16 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Dag Borlaug
el 19 de En. de 2022
Editada: John D'Errico
el 19 de En. de 2022
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 comentario
Respuesta aceptada
John D'Errico
el 19 de En. de 2022
Editada: John D'Errico
el 19 de En. de 2022
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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Solvers en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!