How to calculate a function of multiple variables which also has an integral in its definition?

1 次查看(过去 30 天)
Dea All,
I have the following function whose definition needs an integral to be evaluated. The integral itself is dependent on the function input variables.
r0 = 0.5;
z0 = 0.5;
G(r,z,z-z0) = 1/2*r*r0^2 * integral(cos(lambda)/sqrt((r^2+r0^2-2*r*r0*cos(lambda)+(z-z0)^2)) dlambda, -pi, pi);
Could someone please help me how I can get for example G(0.75, 0.75, 0.25)? My final goal is to find G over a rectangular meshgrid.
Thanks,
Ahmad

回答(2 个)

Matt J
Matt J 2012-10-30
Create an anonymous function for the integrand as a function of lambda
G=@(r,z,z-z0) 1/2*r*r0^2 * integral(@(lambda) cos(lambda)/sqrt((r^2+r0^2-2*r*r0*cos(lambda)+(z-z0)^2)) , -pi, pi);
  5 个评论
Matt J
Matt J 2012-10-30
Replace all the * and / by elementwise operations .* and ./
G=@(r,z,z_minus_z0) 1/2.*r.*r0^2 .* ... integral(@(lambda) cos(lambda)./sqrt((r.^2+r0.^2-2.*r.*r0.*cos(lambda)+z_minus_z0.^2)) , -pi, pi);

请先登录,再进行评论。


Star Strider
Star Strider 2012-10-30
You need to ‘vectorize’ it:
r0 = 0.5;
z0 = 0.5;
r = 1;
z = 1;
G = @(r,z,z0) 1/2.*r.*r0.^2 .* integral(@(lambda) cos(lambda)./sqrt((r.^2+r0.^2-2.*r.*r0.*cos(lambda)+(z-z0).^2)) , -pi, pi);
G(r,z,z0)
  3 个评论
AP
AP 2012-10-30
编辑:AP 2012-10-30
Thanks all. Runs perfectly with elementwise operations.
r0 = 0.5;
z0 = 0.5;
r = 1;
z = 1;
G=@(r, r0, z, z0) 1/2.*r.*r0^2 .* integral(@(lambda) cos(lambda)./sqrt((r.^2+r0.^2-2*r.*r0.*cos(lambda)+(z-z0).^2)) , -pi, pi);
G(r,r0,z,z0)
ans =
0.1390
Star Strider
Star Strider 2012-10-30
You have to vectorize it using the ‘dot’ operators:
G = @(r, r0, z, z0) 1/2.*r.*r0.^2 .* integral(@(lambda) cos(lambda)./sqrt((r.^2+r0.^2-2.*r.*r0.*cos(lambda)+(z-z0).^2)) , -pi, pi);
See if that works as you want it to.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by