×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Unexpected behavior with conditions in if loops....

Unexpected behavior with conditions in if loops....

Unexpected behavior with conditions in if loops....

(OP)
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.

 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login



News


Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close