Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unexpected behavior with conditions in if loops....

Status
Not open for further replies.

grimesd2

Bioengineer
Nov 26, 2008
2
IE
Thanks in advance for looking at this; I have an .m file to finds the tangents to a given circle from any external points, and treats that circle like an obstruction so all the circle and points behind it are blocked. It works like this;

--M Code for a single circle obstruction--
format long
Irad=zeros([1000 1000]);
H = 1.75;
P = 100
g = 50;
f = 50;
crad = 10;
gsize=100;

A = P/(2*pi*H);

%Basically, there's a tube emitting at the origin!

X = 0;
Y = 0;

%Tubecent is the distance from origin to centre circle
tubecent = sqrt((g/100. - X/1000)^2 + (f/100 - Y/1000)^2);

%the following angles are important
beta = asin((crad/100)/tubecent);
gamma = atan((g/100-X)/(f/100-Y))
theta = gamma - beta;
phi = gamma + beta;

for j = 1:1000
for k = 1:1000
r = sqrt( ((j/1000)-(X/100)).^2 + ((k/1000)-(Y/100)).^2 );
%r is the distance of the point in question to the tube
w = sqrt((g/100 - j/1000).^2 + (f/100 - k/1000).^2);
delta = tubecent-r;


ang = atan((j-X)./(k-Y)); %Angle between point and tube centre

if ang <= theta || ang >= phi
I = A ./ r;
elseif ang > theta && ang < phi
if w >= crad/100 && delta >= 0
I = A ./ r;
else
I = 0;

end
end


Irad(j,k) = sum(I);
end
end
[XM,YM]=meshgrid(0.1:0.1:gsize , 0.1:0.1:gsize);

figure(3)
pcolor(XM,YM,log(Irad)), title('Log of Intensity versus distance graph')
xlabel('Centimetres'), ylabel('Centimetres')
shading interp

--------------------------

This works fine, and running it in Matlab gives the expected result. Generalizing it to two circles shouldn't be much work, but however, I cannot get it to work; consider this..

--Potential M-File for 2 circular obstructions--

format long
Irad=zeros([1000 1000]);
H = 1.75;
P = 100

%circle co-ordinates (at centre)
g1 = 0.65;
f1 = 0.45;
g2 = 0.45
f2 = 0.65
%crad = input('Enter circle radius in centimetres: ');
crad1 = 0.05;
crad2 = 0.10;
gsize=100;

A = P/(2*pi*H);

%Basically, there's a tube emitting at the origin!

X = 0;
Y = 0;

%Tubecent is the distance from origin to centre circle
D1 = sqrt((g1 - X/1000)^2 + (f1 - Y/1000)^2);
D2 = sqrt((g2 - X/1000)^2 + (f2 - Y/1000)^2);

%the following angles are important-ish
beta1 = asin((crad1)/D1)
gamma1 = atan((f1-X)/(g1 -Y))
theta1 = gamma1 - beta1
phi1 = gamma1 + beta1

beta2 = asin((crad2)/D2)
gamma2 = atan((f2-X)/(g2-Y))
theta2 = gamma2 - beta2
phi2 = gamma2 + beta2


for j = 1:1000
for k = 1:1000
r = sqrt( ((j/1000)-(X)).^2 + ((k/1000)-(Y)).^2 );
%r is the distance of the (j,k) in question to the tube (x,y)
W1 = (sqrt((g1 - j/1000).^2 + (f1 - k/1000).^2)) - crad1/100;
W2 = (sqrt((g2 - j/1000).^2 + (f2 - k/1000).^2)) - crad2/100;
delta1 = D1 - r;
delta2 = D2 -r;

ang = atan((j/1000-X)./(k/1000-Y)); %Angle between point and tube centre

if ang <= theta1 || ang >= phi2 || ang > phi1 && ang < theta2 %tests of angle is less than theta
I = A ./ r;


elseif ang > theta1 && ang < phi1 && delta1 >= 0 && W1 >= 0
I = A ./ r;

elseif ang > theta2 && ang < phi2 && delta2 >= 0 && W2 >= 0
I = A ./ r;
else
I = 0;


end

Irad(j,k) = sum(I);
end
end
[XM,YM]=meshgrid(0.1:0.1:gsize , 0.1:0.1:gsize);

figure(3)
pcolor(XM,YM,log(Irad)), title('Log of Intensity versus distance graph')
xlabel('Centimetres'), ylabel('Centimetres')
shading interp

-----------------------------------------

This code doesn't quite work right; it seems to ignore the condition where the distance from the centre of the respective circles to the test point is less than the respective radius. Any ideas? I've been messing around with this for a week now and having no joy, regardless of what I do....

Thanks for reading. David.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top