1 view (last 30 days)

Here,

u=-0.5:0.1:0.5

v= -1:0.1:1

r is a user inputed value. Could be +0.35 or -0.45 depending upon the probe position needed. If the inputed value of r is equal to or greater than 0.8, and if the round off value r1, is to be checked to find the nearest corresponding v(i) value,once this is found, if u(i) is equal to 0,the value of B is the value Bm2(i) corresponding to that v(i). Elseif u(i) is not equal to 0, the value of B is found using a compensation function but depending on the value of B corresponding to u(i)=0.

for i=1:numel(u)

Bxcf(i)= (((1.2566e-6 * I)/(4* pi * dxc(i))) * (1.8/(sqrt((dxc(i))^2+((1.8^2)/4)))));

for i=1:numel(u)

Bx1(i)= (((1.2566e-6 * I4(i))/(4* pi * dx1(i))) * (1.8/(sqrt((dx1(i))^2+((1.8^2)/4)))));

end

for i=1:numel(u)

Bx2(i)= (((1.2566e-6 * I5(i))/(4* pi * dx2(i))) * (1.8/(sqrt((dx2(i))^2+((1.8^2)/4)))));

end

for i=1:numel(u)

Bx3(i)= (((1.2566e-6 * I6(i))/(4* pi * dx3(i))) * (1.8/(sqrt((dx3(i))^2+((1.8^2)/4)))));

end

end

Bcx = abs(Bxcf-abs(Bx1)-abs(Bx2)-abs(Bx3));

p1 = -9.221e-08 ;

p2 = 9.08e-06 ;

p3 = -0.000331 ;

p4 = 0.002805 ;

p5 = 3.83 ;

p11 = -43.09;

p12 = 48.76;

p13 = -20.68;

p14 = 4.303;

count1 =round(numel(u)/2);

Bm1 = Bcx(count1);

r1 = round(r,1);

for i=1:numel(v)

y2(i) = abs(p1* abs(v(i)*100)^4 + p2* abs(v(i)*100)^3 + p3*abs(v(i)*100)^2 + p4*abs(v(i)*100) + p5)/3.83;

Bm2(i) = y2(i) * Bm1;

end

if abs(r1) >= 0.8

for i= 1:numel(u)

if abs(v(i)) == r1 && u(i)==0

y2(i) = abs(p1* abs(v(i)*100)^4 + p2* abs(v(i)*100)^3 + p3*abs(v(i)*100)^2 + p4*abs(v(i)*100) + p5)/3.83;

Bx(i)=y2(i) * Bm1;

elseif u(i) ~=0

y3 = abs((p11* abs(u(i))^3 + p12*abs(u(i))^2 + p13*abs(u(i)) + p14)/ (y2(i) * Bm1) *10^-6) ;

Bx(i) = abs(y3*10^-7);

end

elseif abs(r1) < 0.8

Bx(i)= Bcx;

end

end

Steven Lord
on 11 Feb 2020

for i=1:numel(u)

Bxcf(i)= (((1.2566e-6 * I)/(4* pi * dxc(i))) * (1.8/(sqrt((dxc(i))^2+((1.8^2)/4)))));

for i=1:numel(u)

Bx1(i)= (((1.2566e-6 * I4(i))/(4* pi * dx1(i))) * (1.8/(sqrt((dx1(i))^2+((1.8^2)/4)))));

end

for i=1:numel(u)

Bx2(i)= (((1.2566e-6 * I5(i))/(4* pi * dx2(i))) * (1.8/(sqrt((dx2(i))^2+((1.8^2)/4)))));

end

for i=1:numel(u)

Bx3(i)= (((1.2566e-6 * I6(i))/(4* pi * dx3(i))) * (1.8/(sqrt((dx3(i))^2+((1.8^2)/4)))));

end

end

There are two problems and one opportunity here. First, using the same variable for the outermost loop and the inner loops is not a good idea. Second, you're computing all of Bx1, Bx2, and Bx3 during each iteration of the outer loop, meaning you're doing a lot of repetitive computations. And finally, you could vectorize these computations and avoid the loops entirely. [Pay particular attention to the "Array Operations" section on that page.]

snipped more of the code

for i=1:numel(v)

y2(i) = abs(p1* abs(v(i)*100)^4 + p2* abs(v(i)*100)^3 + p3*abs(v(i)*100)^2 + p4*abs(v(i)*100) + p5)/3.83;

Bm2(i) = y2(i) * Bm1;

end

This too looks like it can be vectorized.

if abs(r1) >= 0.8

for i= 1:numel(u)

if abs(v(i)) == r1 && u(i)==0

Bx(i)=y2(i) * Bm1;

elseif u(i) ~=0

y3 = abs((p11* abs(u(i))^3 + p12*abs(u(i))^2 + p13*abs(u(i)) + p14)/ (y2(i) * Bm1) *10^-6) ;

Bx(i) = abs(y3*10^-7);

end

elseif abs(r1) < 0.8

Bx(i)= Bcx;

end

end

This is invalid code. If you copy it into the MATLAB Editor (if you're not already working in MATLAB Editor) and smart indent it, you'll see that the second elseif statement is inside the for loop while the if with which it's paired is outside the for loop. You need to either pull that second elseif statement outside the loop or pull the initial if statement inside the loop. Since the body of the elseif statement depends on the loop variable I'm guessing you want to pull the if statement inside the for loop.

You should also decide what you want Bx to contain in the case where abs(r1) is neither greater than or equal to 0.8 nor less than 0.8. NaN is not greater than, not less than, and not equal to 0.8.

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/504817-else-if-inside-for-loop#comment_795874

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/504817-else-if-inside-for-loop#comment_795874

Sign in to comment.