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:
Coping with asymptotes in tan function

Subject: Coping with asymptotes in tan function

From: Jane

Date: 7 Dec, 2010 17:54:05

Message: 1 of 5

Hi, I have written a function (probably not very efficiently) to measure the distance along a line, defined by the angle from the negative y-axis and passing through a known point.

The problem I am having is coping with instance where the user selects the angle phi = [0, 90, 180, 270,...] the problem becoems ill-conditioned. I'm sure there must be an easy work around for this, but I've become so bogged down in the details I can't see it!

I would appreciate any help.

gridSize = [20 20];
phi = 90;
es = [10 10];

function [virtualx, virtualy] = arrayDistances(gridSize, phi, es)
% arrayDistances - For an (n x m) grid, find the orthogonal distance from
% the centre of each cell to the centre plume line of the emission source.
% Also find the distance along the centre plume line between the emission
% source and the intersection with the orthogonal line.
%
% Uses the principle that for determining the gradient of the orthogonal
% to a straight-line; gradient = -1/m, where the line has a gradient of m.
%
% Input Parameters:
% gridSize - [n m] the size of the grid, it is assumed that each
% cell in the grid is 1m^2
% phi - The angle, in degrees, specifying the direction of
% wind. Taken as the angle between the centre
% plume line and the positive y-axis
% es - [x y] the (x,y) position of the emission source
%
% Output Parameters:
% virtualx - An array of size (n,m) containing the distance
% between the emission source and the
% intersection of the centre plume line and the
% orthogonal line that passes through the centre
% point of the (i,j)th cell of the grid
% virtualy - An array of size (n,m) containing the orthogonal
% distance between the centre of each cell in the
% grid and the centre plume line
%
% Created on: 6 December 2010
% -------------------------------------------------------------------------

% % x = [0 gridSize(1)]; % x-positions of line ends for centre plume
% % % line - only used in plotting centre plume
% % % line

phi = pi/180 * phi; % Convert angle to radians

c = es(2) - es(1)/tan(phi); % Calculate constant in equation of the
                                % centre plume line, knowing it passes
                                % through point es and is at an angle phi
                                % from the positive y-axis

% Initialise array to store virtual distance information
virtualx = nan(gridSize(2), gridSize(1));
virtualy = nan(gridSize(2), gridSize(1));

for i = 1:gridSize(1)
    xpoint = i - 0.5; % x-position to measure distance for
    for j = 1:gridSize(2)
        ypoint = j - 0.5; % y-position to measure ditance for
        
        cprimed = ypoint + xpoint*tan(phi);
                                % Intercept of orthogonal to centre plume
                                % line

        cross = [tan(phi) 1; -1/tan(phi) 1]\[cprimed; c];
                                % (x,y) position of intercept between
                                % centre plume line and its orthogonal

        virtualy(j,i) = sign(ypoint - cross(2))*...
            sqrt( (cross(1) - xpoint)^2 + (cross(2) - ypoint)^2 );
                                % orthogonal distance from point of
                                % interest to centre plume line

        virtualx(j,i) = -1*sign(cross(1) - es(1))*sign(sin(phi))*...
            sqrt( (cross(1) - es(1))^2 + (es(2) - cross(2))^2 );
                                % distance along centre plume line between
                                % emitter and the point the orthogonal line
                                % intersects it plume line
    end
end

Subject: Coping with asymptotes in tan function

From: Sean de

Date: 7 Dec, 2010 18:19:19

Message: 2 of 5

"Jane " <j.l.terry@hotmail.co.uk> wrote in message <idlsbt$9ip$1@fred.mathworks.com>...
> Hi, I have written a function (probably not very efficiently) to measure the distance along a line, defined by the angle from the negative y-axis and passing through a known point.
>
> The problem I am having is coping with instance where the user selects the angle phi = [0, 90, 180, 270,...] the problem becoems ill-conditioned. I'm sure there must be an easy work around for this, but I've become so bogged down in the details I can't see it!
>
> I would appreciate any help.
>
> gridSize = [20 20];
> phi = 90;
> es = [10 10];
>
> function [virtualx, virtualy] = arrayDistances(gridSize, phi, es)
> % arrayDistances - For an (n x m) grid, find the orthogonal distance from
> % the centre of each cell to the centre plume line of the emission source.
> % Also find the distance along the centre plume line between the emission
> % source and the intersection with the orthogonal line.
> %
> % Uses the principle that for determining the gradient of the orthogonal
> % to a straight-line; gradient = -1/m, where the line has a gradient of m.
> %
> % Input Parameters:
> % gridSize - [n m] the size of the grid, it is assumed that each
> % cell in the grid is 1m^2
> % phi - The angle, in degrees, specifying the direction of
> % wind. Taken as the angle between the centre
> % plume line and the positive y-axis
> % es - [x y] the (x,y) position of the emission source
> %
> % Output Parameters:
> % virtualx - An array of size (n,m) containing the distance
> % between the emission source and the
> % intersection of the centre plume line and the
> % orthogonal line that passes through the centre
> % point of the (i,j)th cell of the grid
> % virtualy - An array of size (n,m) containing the orthogonal
> % distance between the centre of each cell in the
> % grid and the centre plume line
> %
> % Created on: 6 December 2010
> % -------------------------------------------------------------------------
>
> % % x = [0 gridSize(1)]; % x-positions of line ends for centre plume
> % % % line - only used in plotting centre plume
> % % % line
>
> phi = pi/180 * phi; % Convert angle to radians
>
> c = es(2) - es(1)/tan(phi); % Calculate constant in equation of the
> % centre plume line, knowing it passes
> % through point es and is at an angle phi
> % from the positive y-axis
>
> % Initialise array to store virtual distance information
> virtualx = nan(gridSize(2), gridSize(1));
> virtualy = nan(gridSize(2), gridSize(1));
>
> for i = 1:gridSize(1)
> xpoint = i - 0.5; % x-position to measure distance for
> for j = 1:gridSize(2)
> ypoint = j - 0.5; % y-position to measure ditance for
>
> cprimed = ypoint + xpoint*tan(phi);
> % Intercept of orthogonal to centre plume
> % line
>
> cross = [tan(phi) 1; -1/tan(phi) 1]\[cprimed; c];
> % (x,y) position of intercept between
> % centre plume line and its orthogonal
>
> virtualy(j,i) = sign(ypoint - cross(2))*...
> sqrt( (cross(1) - xpoint)^2 + (cross(2) - ypoint)^2 );
> % orthogonal distance from point of
> % interest to centre plume line
>
> virtualx(j,i) = -1*sign(cross(1) - es(1))*sign(sin(phi))*...
> sqrt( (cross(1) - es(1))^2 + (es(2) - cross(2))^2 );
> % distance along centre plume line between
> % emitter and the point the orthogonal line
> % intersects it plume line
> end
> end

if any(phi==[0 90 180 270])
   error('Ill conditioned');
end

If they can have numbers outside that range, use MOD or REM to convert. Also, be careful of floating point issues. E.g. What do you want to do if they enter 90.00002?

Subject: Coping with asymptotes in tan function

From: Jane

Date: 7 Dec, 2010 18:44:06

Message: 3 of 5

> if any(phi==[0 90 180 270])
> error('Ill conditioned');
> end
>
> If they can have numbers outside that range, use MOD or REM to convert. Also, be careful of floating point issues. E.g. What do you want to do if they enter 90.00002?

Thanks Sean, I was actually hoping to still get some results, rather than just return an error. One of my thoughts was to add 1e-6 to the value of phi.

I'm not quite sure what your comment about floating points means.

Subject: Coping with asymptotes in tan function

From: Roger Stafford

Date: 7 Dec, 2010 20:35:29

Message: 4 of 5

"Jane " <j.l.terry@hotmail.co.uk> wrote in message <idlsbt$9ip$1@fred.mathworks.com>...
> Hi, I have written a function (probably not very efficiently) to measure the distance along a line, defined by the angle from the negative y-axis and passing through a known point.
>
> The problem I am having is coping with instance where the user selects the angle phi = [0, 90, 180, 270,...] the problem becoems ill-conditioned. I'm sure there must be an easy work around for this, but I've become so bogged down in the details I can't see it!
>
> I would appreciate any help.
>.......
- - - - - - - -
  You are making a hard job out of a very easy problem, Jane. In effect, you are rotating to a new coordinate system with a changed origin. The equations for that are:

 v = (y-ey)*cos(phi) - (x-ex)*sin(phi)
 u = (y-ey)*sin(phi) + (x-ex)*cos(phi)

where (x,y) are the coordinates of the grid point in question, phi is the angle measured counterclockwise from the positive y-axis to the "centre plume line", (ex,ey) are the coordinates of the "emission source", and u and v are the requested orthogonal distance and distance along the plume line, respectively.

  Note that u and v are signed quantities which can be either positive or negative depending on the coordinates x and y. If you wish phi to measured clockwise, just change the signs of the sin(phi) in the above equations. If you wish to switch the directions u or v are to be measured in, just switch their signs in the equations accordingly. You were not specific on this point.

Roger Stafford

Subject: Coping with asymptotes in tan function

From: Jane

Date: 7 Dec, 2010 22:38:04

Message: 5 of 5

Thank you, works great. It seems to be a week for finding the most difficult way of doing things.


> You are making a hard job out of a very easy problem, Jane. In effect, you are rotating to a new coordinate system with a changed origin. The equations for that are:
>
> v = (y-ey)*cos(phi) - (x-ex)*sin(phi)
> u = (y-ey)*sin(phi) + (x-ex)*cos(phi)
>
> where (x,y) are the coordinates of the grid point in question, phi is the angle measured counterclockwise from the positive y-axis to the "centre plume line", (ex,ey) are the coordinates of the "emission source", and u and v are the requested orthogonal distance and distance along the plume line, respectively.
>
> Note that u and v are signed quantities which can be either positive or negative depending on the coordinates x and y. If you wish phi to measured clockwise, just change the signs of the sin(phi) in the above equations. If you wish to switch the directions u or v are to be measured in, just switch their signs in the equations accordingly. You were not specific on this point.
>
> 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