Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Need help with complicated IF loop conditions - please advise!

Subject: Need help with complicated IF loop conditions - please advise!

From: DRG

Date: 7 Jan, 2009 12:49:43

Message: 1 of 2

I have a code that allows the user to specify the position of a light
source and an ellipse-shaped obstruction. To keep this post concise,
I've omitted details about how all the equations are solved and merely
put in the results that lead to the IF loop; To summate, the code
specifies the angular extent between the light at XY and the tangent
points of the ellipse, and all points outside this angular extent are
illuminated (This is shown in figure 2 in the code).

To compute the irradiances INSIDE the angular extent (while all points
in and beyond the ellipse are not illuminated, the points before the
ellipse are and this makes the question tricky) the code finds a
circumcentre to the tangent points and XY, and the angular extent of
these points. It then works out the radius of this circumcircle. It
draws a modified arc between the tangent points, with the
circumcentre as the centre and the arc modified to with a cosine
condition to be a straightline (shown in Figure 3 in the code)

These conditions are combined in Figure I, but as you can see, the
most right tangent line is no longer in agreement with the beta
condition..in fact, light "bends" around the ellipse at one point and
I wish to eradicate this error. Can anyone help? I know this is messy
coding but I really need an answer soon. Thanks in advance, David.


---------------------------------------------------------------------------------------
format long
Irad=zeros([1000 1000]);

warning off all

clear
H = 1.75;
P = 100;
gsize=100;

a = .15; %condition for ellipse
b = .05; %condition for ellipse
h = .5; %condition for ellipse
k = .5; %condition for ellipse

A = P/(2*pi*H); %Power rule

X = 0.1; %position of tube X co-ord
Y = 0.1; %position of tube Y-co-ord


%Circumcentre of two Tangent points and tube point
w = 0.45750000000000
v = 0.16250000000000


%Radius of circle
Rcirc = 0.36292216796443;


%Lambda, the relative angle between the circumcentre and tangent
points
cosinelambda = 0.92007972078891;
lambda = 0.40251238126841;

%Gamma, the angle between XY and the bisector of the tangent line
gamma = 0.81660756839543;

%The angle beta, between XY and the tangent points
beta = 0.20125619063420;

%The real angle between the midpoint of the tangent lines and
circumcentre
gammae = 1.46013910562100;


%Loop 1

for n = 1:1000
    for m = 1:1000
rp = sqrt( ((n/1000)-(X)).^2 + ((m/1000)-(Y)).^2 ); % distance from
XY to any point
D = sqrt( ((n/1000)-(w)).^2 + ((m/1000)-(v)).^2 ); % distance from wv
to any point circumcentre

eq = (a^-2)*((n/1000)^2) - (a^-2)*(2*h*(n/1000)) + (b^-2)*((m/1000)^2)
- (b^-2)*(2*k*(m/1000)) + ((a^-2)*(h^2) + (b^-2)*(k^2) - 1);

ang = atan2(((m/1000) - Y),((n/1000) - X)); %angle between XY and
point
delta = atan2(((m/1000) - v),((n/1000) - w)); % angle between WV and
point

delta_eff = abs(gammae - atan2(((m/1000) - v),((n/1000) - w))); %
angle between WV and point relative lambda

cosdel = abs(cos(delta_eff));
ratio = cosinelambda / cosdel;
R_e = Rcirc*ratio;


if abs(mod(gamma -ang +pi,2*pi)-pi) < beta && (abs(mod(gammae -delta
+pi,2*pi)-pi) < lambda && D > R_e) || eq <= 0

    I = 0;


    else
    I = A ./ rp;


end


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

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





%Loop 2 Demonstrating the beta confines

for n = 1:1000
    for m = 1:1000
rp = sqrt( ((n/1000)-(X)).^2 + ((m/1000)-(Y)).^2 ); % distance from
XY to any point
D = sqrt( ((n/1000)-(w)).^2 + ((m/1000)-(v)).^2 ); % distance from wv
to any point circumcentre

eq = (a^-2)*((n/1000)^2) - (a^-2)*(2*h*(n/1000)) + (b^-2)*((m/1000)^2)
- (b^-2)*(2*k*(m/1000)) + ((a^-2)*(h^2) + (b^-2)*(k^2) - 1);

ang = atan2(((m/1000) - Y),((n/1000) - X)); %angle between XY and
point
delta = atan2(((m/1000) - v),((n/1000) - w)); % angle between WV and
point

cosdel = abs(cos(atan2((m/1000) - v,(n/1000) - w)));
cosdel2 = abs(cos(delta));
R_e = Rcirc*(cosinelambda/cosdel2);


if abs(mod(gamma -ang +pi,2*pi)-pi) < beta

    I = 0;


    else
    I = A ./ rp;


end


 Irad2(n,m) = sum(I);
     end
 end
 [XM,YM]=meshgrid(0.1:0.1:gsize , 0.1:0.1:gsize);

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

%Loop 3 The extent of lambda

for n = 1:1000
    for m = 1:1000
rp = sqrt( ((n/1000)-(X)).^2 + ((m/1000)-(Y)).^2 ); % distance from
XY to any point
D = sqrt( ((n/1000)-(w)).^2 + ((m/1000)-(v)).^2 ); % distance from wv
to any point circumcentre

eq = (a^-2)*((n/1000)^2) - (a^-2)*(2*h*(n/1000)) + (b^-2)*((m/1000)^2)
- (b^-2)*(2*k*(m/1000)) + ((a^-2)*(h^2) + (b^-2)*(k^2) - 1);

ang = atan2(((m/1000) - Y),((n/1000) - X)); %angle between XY and
point
delta = atan2(((m/1000) - v),((n/1000) - w)); % angle between WV and
point

delta_eff = abs(gammae - atan2(((m/1000) - v),((n/1000) - w))); %
angle between WV and point relative lambda

cosdel = abs(cos(delta_eff));
ratio = cosinelambda / cosdel;
R_e = Rcirc*ratio;


if abs(mod(gammae -delta +pi,2*pi)-pi) < lambda && D > R_e

    I = 0;


    else
    I = A ./ rp;


end


 Irad3(n,m) = sum(I);
     end
 end
 [XM,YM]=meshgrid(0.1:0.1:gsize , 0.1:0.1:gsize);

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

Subject: Need help with complicated IF loop conditions - please advise!

From: Roger Stafford

Date: 8 Jan, 2009 04:27:02

Message: 2 of 2

DRG <grimesd2@gmail.com> wrote in message <180148c8-dd7d-42c5-bf61-de2c945a05fd@g3g2000pre.googlegroups.com>...
> I have a code that allows the user to specify the position of a light
> source and an ellipse-shaped obstruction. To keep this post concise,
> I've omitted details about how all the equations are solved and merely
> put in the results that lead to the IF loop; To summate, the code
> specifies the angular extent between the light at XY and the tangent
> points of the ellipse, and all points outside this angular extent are
> illuminated (This is shown in figure 2 in the code).
>
> To compute the irradiances INSIDE the angular extent (while all points
> in and beyond the ellipse are not illuminated, the points before the
> ellipse are and this makes the question tricky) the code finds a
> circumcentre to the tangent points and XY, and the angular extent of
> these points. It then works out the radius of this circumcircle. It
> draws a modified arc between the tangent points, with the
> circumcentre as the centre and the arc modified to with a cosine
> condition to be a straightline (shown in Figure 3 in the code)
>
> These conditions are combined in Figure I, but as you can see, the
> most right tangent line is no longer in agreement with the beta
> condition..in fact, light "bends" around the ellipse at one point and
> I wish to eradicate this error. Can anyone help? I know this is messy
> coding but I really need an answer soon. Thanks in advance, David.
> ........

  DRG, I don't have time to struggle through your code. However, I question the necessity of dealing with the circumscribed circle through the points of tangency and XY. It seems to me that once you have calculated the two tangent points, all you need do is determine the equation of the line through them. Then an arbitrary point (x,y) is illuminated if either 1) it lies outside your "angular extent" or 2) it lies inside, also lies outside the ellipse, and in addition lies on the same side of the above line as the light source XY does. These are all simple calculations which ought to require much less code than I see here.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us