When integrating numerically with integral2, the integral function contains a sign variable, causing the integral to keep reporting an error.

3 visualizzazioni (ultimi 30 giorni)
B is a vector expression with three directions ex, ey and ez, and each component has a double integral.I want to figure out the magnitude of B.So when we do double integrals, because we have A lot of repetitive code, we'll use A instead And then there was an error. If you type it directly, it can run.I want to know if integral2 can't have signed variables in its integral function, and if not, how can I modify it. Or how can the program be optimized?
syms phii rr theta r;
format short
phi = pi./2;
R = 0.05;
A = (((r.*sin(theta)-rr.*sin(phii)).^2+r.^2.*cos(theta).^2+rr.^2.*cos(phii).^2).^(3./2));
B = [];
for theta = [0,pi/6,pi/4,pi/3,pi/2]
Ix = []; % direction of ex
for r = 0.06:0.001:0.09
fx = @(rr,phii) rr.*r.*cos(theta).*cos(phii)./A;
ex = integral2(fx,0,R,0,2*pi);
Ix = [Ix,ex];
end
Iy = []; % direction of ey
for r = 0.06:0.001:0.09
fy = @(rr,phii) rr.*r.*cos(theta).*sin(phii)./A;
ey = integral2(fy,0,R,0,2*pi);
Iy = [Iy,ey];
end
Iz = []; % direction of ez
for r = 0.06:0.001:0.09
fz = @(rr,phii) rr.*(rr-r.*sin(phii).*sin(theta))./A;
ez = integral2(fz,0,R,0,2*pi);
Iz = [Iz,ez];
end
Bb = sqrt((Ix.^2+Iy.^2+Iz.^2)); % B
B = [B;Bb];
end
Error using integral2Calc>integral2t/tensor
Input function must return 'double' or 'single' values. Found 'sym'.

Error in integral2Calc>integral2t (line 55)
[Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);

Error in integral2Calc (line 9)
[q,errbnd] = integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);

Error in integral2 (line 105)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);

Risposta accettata

Dyuman Joshi
Dyuman Joshi il 24 Mar 2023
Modificato: Dyuman Joshi il 24 Mar 2023
You don't need to use symbolic variables for this code. Define A as a function handle and use r values as an input to define the functions input to integral2 accordingly.
Club the for loops together as they have common loop indices, to make the code faster. Another suggestion would be to Preallocate the variables B, Ix, Iy and Iz.
P.S - You have defined phi, but you don't use it anywhere in the code.
phi = pi./2;
R = 0.05;
%Defining A as a function handle
A = @(r,rr,theta,phii) (((r.*sin(theta)-rr.*sin(phii)).^2+r.^2.*cos(theta).^2+rr.^2.*cos(phii).^2).^(3./2));
%r values
r0=0.06:0.001:0.09;
%B = zeros(5,numel(r0));
B=[];
for theta = [0,pi/6,pi/4,pi/3,pi/2]
%Preallocation, note that you can also define Ix, Iy and Iz out of the theta for loop, as
%the values will get over-written with each iteration
Ix = zeros(size(r0)); % direction of ex
Iy = zeros(size(r0)); % direction of ey
Iz = zeros(size(r0)); % direction of ez
%or use "[Ix,Iy,Iz] = deal(zeros(size(r0)))" to achieve the same result
%in a single command.
%Clubbing the for loops together
for k = 1:numel(r0)
fx = @(rr,phii) rr.*r0(k).*cos(theta).*cos(phii)./A(r0(k),rr,theta,phii);
ex = integral2(fx,0,R,0,2*pi);
Ix(k) = ex;
fy = @(rr,phii) rr.*r0(k).*cos(theta).*sin(phii)./A(r0(k),rr,theta,phii);
ey = integral2(fy,0,R,0,2*pi);
Iy(k) = ey;
fz = @(rr,phii) rr.*(rr-r0(k).*sin(phii).*sin(theta))./A(r0(k),rr,theta,phii);
ez = integral2(fz,0,R,0,2*pi);
Iz(k) = ez;
end
Bb = sqrt((Ix.^2+Iy.^2+Iz.^2)); % B
B = [B;Bb];
end
B
B = 5×31
0.7433 0.7165 0.6908 0.6663 0.6429 0.6205 0.5990 0.5785 0.5589 0.5401 0.5221 0.5048 0.4883 0.4724 0.4572 0.4426 0.4286 0.4152 0.4022 0.3898 0.3779 0.3664 0.3554 0.3448 0.3346 0.3248 0.3153 0.3062 0.2975 0.2890 0.7829 0.7530 0.7244 0.6972 0.6712 0.6464 0.6227 0.6001 0.5785 0.5579 0.5381 0.5193 0.5013 0.4840 0.4675 0.4517 0.4366 0.4221 0.4082 0.3948 0.3821 0.3698 0.3581 0.3468 0.3360 0.3256 0.3156 0.3060 0.2967 0.2879 0.8369 0.8021 0.7690 0.7376 0.7077 0.6793 0.6522 0.6265 0.6020 0.5787 0.5565 0.5354 0.5152 0.4960 0.4777 0.4603 0.4436 0.4277 0.4125 0.3980 0.3841 0.3709 0.3582 0.3461 0.3345 0.3234 0.3127 0.3025 0.2928 0.2834 0.9236 0.8793 0.8375 0.7981 0.7609 0.7257 0.6926 0.6613 0.6318 0.6038 0.5775 0.5525 0.5289 0.5066 0.4855 0.4655 0.4465 0.4286 0.4115 0.3953 0.3800 0.3654 0.3516 0.3384 0.3258 0.3139 0.3025 0.2917 0.2814 0.2716 1.2366 1.1328 1.0424 0.9629 0.8926 0.8300 0.7739 0.7234 0.6777 0.6362 0.5985 0.5639 0.5323 0.5031 0.4763 0.4515 0.4285 0.4072 0.3874 0.3689 0.3517 0.3356 0.3205 0.3064 0.2932 0.2807 0.2690 0.2579 0.2475 0.2377

Più risposte (0)

Prodotti


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by