Symbolic integration and integration of function handle

3 views (last 30 days)
I am trying to accomplish one of the two codes. The second one can be executed, but it has errors. The other, he can't solve my problem. Could you help me solve my problem?
% Datas
B = 1.31;
a = 0.005;
L = 0.010;
sig = 5.97*(10^7);
vv = 0.01;
Boo = B.*a./(4*pi);
syms sup(z1,y,zz) deno(r,z1,y,zz) Bd(r,z1,y,zz)
sup(z1,y,zz) = (z1 - zz).*cos(y);
deno(r,z1,y,zz) = (r.^2 + a.^2 + (z1 - zz).^2 - 2.*a.*r.*cos(y)).^(3/2);
Bd(r,z1,y,zz) = sup(z1,y,zz)./deno(r,z1,y,zz);
syms int1(r,z1,zz) int2(r,z1) Br(r,z1) J(r,z1)
int1(r,z1) = int(int(Bd(r,z1,y,zz),y,[0 2*pi]),zz,[-L/2 L/2]);
Brr(r,z1) = Boo.*int1(r,z1);
J(r,z1) = sig.*vv.*Brr(r,z1);
fcontour(J,[0.006 0.0225 0 0.007])
B = 1.31;
a = 0.005;
L = 0.010;
sig = 5.97*(10^7);
vv = 0.01;
Boo = B.*a./(4*pi);
sup = @(z1,y,zz) (z1 - zz).*cos(y);
deno = @(r,z1,y,zz)(r.^2 + a.^2 + (z1 - zz).^2 - 2.*a.*r.*cos(y)).^(3/2);
Bd = @(r,z1,y,zz) sup(z1,y,zz)./deno(r,z1,y,zz);
int1 = @(r,z1) integral2(@(y,zz) Bd(r,z1,y,zz),0,2*pi,-L/2,L/2);
Brr = @(r,z1) Boo.*int1(r,z1);
J = @(r,z1) sig.*vv.*Brr(r,z1);
fcontour(J,[0.006 0.0225 0 0.007])
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly
vectorize your function to return an output with the same size and shape as the input
arguments.
> In matlab.graphics.function.FunctionContour>getImplicitFunction
In matlab.graphics.function.FunctionContour/updateFunction
In matlab.graphics.function.FunctionContour/set.Function
In matlab.graphics.function.FunctionContour
In fcontour>singleFContour (line 154)
In fcontour>@(f)singleFContour(cax,f,extraOpts,args) (line 134)
In fcontour>vectorizeFContour (line 134)
In fcontour (line 113)
In CurrentDensisty (line 22)

Accepted Answer

Walter Roberson
Walter Roberson on 20 Apr 2021
fcontour is going to invoke J passing a vector of values for each of the two variables. J passes the vectors through into a couple of layers, including making them (temporarily) constant inputs for integral2. integral2 will pass in vectors for the two free variables, and it expects to receive an array of results the same size as the two free variables passed in. But those two vectors provided by fcontour are inputs and you vectorized over them, so (best case) you would create an output that was length of the fcontour vectors by length of the integral2 vectors, which is not the size expected by integral2.
integral() offers an 'ArrayValued' option to handle this kind of situation, but integral2 does not.
Furthermore, the vectors created by fcontour are not the same size as the vectors created by integral2 so you cannot add or multiply them. In order to take advantage of vectorization you would have had to carefully arrange to have the vectors extend along different dimensions so that implicit expansion could take place.
Thus there are multiple reasons why your logic is not properly vectorized.
To deal with this, your int1 function needs to arrayfun over its inputs doing the integral2 for each r, z1 in turn. Or have J or Brr do that arrayfun, doesn't matter.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!