Hi there, I've been working on a piece of code for calculating voltage and electric field inside coaxial cables of different sizes.
While doing my computation, I needed to do an if/else statement and was boggled as to why these two versions of the statement did not work interchangeably. For reference, both these statements are executed in a nested for loop (i, j are declared – there is no syntax error – just a logic one).
Option 1:
if ((i < innerstartx) || (i > innerendx) || (j < innerstarty) || (j > innerendy)) V(i,j) = (V(i+1,j) + V(i-1,j) + V(i,j+1) + V(1,j-1))/4;elseend
Option 2:
if (i>=innerstartx && i<=innerendx && j>=innerstarty && j<=innerendy)else V(i,j)=0.25*(V(i+1,j)+V(i-1,j)+V(i,j+1)+V(i,j-1));end
Currently, option 2 works, but option 1 doesn't. I checked over the logic a few times and I am pretty sure it is sound (simply De Morgan's inversion).
I was just curious though, as I'd like to be able to avoid this sort of problem should it arise in the future.
Thanks for your help.
sas
Best Answer