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:
problem with solving symbolic equation

Subject: problem with solving symbolic equation

From: MICHAEL

Date: 28 Aug, 2010 05:46:12

Message: 1 of 4

i wanna find the cross points of 2 circles with center point and radius which changed each run.
i define the following
-----------------------------------------------------------
syms x y x_center y_center r
eq=(x-x_center)^2+(y-y_center)^2-r^2;
x_center=X(1);
y_center=Y(1);
r=R(1);
eq1=subs(eq);

x_center=X(2);
y_center=Y(2);
r=R(2);
eq2=subs(eq);
-----------------------------------------------------------
and to solve i use
-----------------------------------------------------------
[x12,y12]=solve(eq1,eq2);
x12=eval(x12); y12=eval(y12);
-----------------------------------------------------------
unfortunately, the run time unpredicteble - it can run very fast, very slow, in the middle and even stuck!!

how to solve it so it will run normally?

Subject: problem with solving symbolic equation

From: Roger Stafford

Date: 28 Aug, 2010 07:01:24

Message: 2 of 4

"michael" <bezenchu@gmail.com> wrote in message <i5a7r4$ocl$1@fred.mathworks.com>...
> i wanna find the cross points of 2 circles with center point and radius which changed each run.
> i define the following
> -----------------------------------------------------------
> syms x y x_center y_center r
> eq=(x-x_center)^2+(y-y_center)^2-r^2;
> x_center=X(1);
> y_center=Y(1);
> r=R(1);
> eq1=subs(eq);
>
> x_center=X(2);
> y_center=Y(2);
> r=R(2);
> eq2=subs(eq);
> -----------------------------------------------------------
> and to solve i use
> -----------------------------------------------------------
> [x12,y12]=solve(eq1,eq2);
> x12=eval(x12); y12=eval(y12);
> -----------------------------------------------------------
> unfortunately, the run time unpredicteble - it can run very fast, very slow, in the middle and even stuck!!
>
> how to solve it so it will run normally?
- - - - - - - - - - -
  To obtain single expressions for the two intersection points, as occurs when you use solve as you have, produces some rather complicated formulas. That complexity, together with the possibility that in some cases you might have no intersection points, may be responsible for your troubles. It is not efficient to force the symbolic toolbox to repeatedly solve the same messy equations over and over again with each new set of data.

  In my opinion it it much better to solve this problem in two steps. The first step is to find the single intersection point between the line connecting the two circle intersection points and the perpendicular line connecting the two centers. You can obtain the equation of the first of these two lines by subtracting the two circle equations.

  The next step is to use the Pythagoras theorem to move from this intersection point in either direction along the line which connects the two circle intersections by an amount which is compatible with the two given radii.

  This technique should allow you to put your computations in a reasonably simple form and give you a more reliable performance. Once you have developed the appropriate formulas, you won't need the services of solve any more.

  To make sure there are points of intersection of the circles you need to determine that the sum of the two radii is not less than the distance between the centers.

Roger Stafford

Subject: problem with solving symbolic equation

From: Roger Stafford

Date: 28 Aug, 2010 07:26:06

Message: 3 of 4

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i5ac84$ppl$1@fred.mathworks.com>...
> ..........
> To make sure there are points of intersection of the circles you need to determine that the sum of the two radii is not less than the distance between the centers.
>
- - - - - - - -
  Also add to that last paragraph the requirement that the distance between the centers should not be less than the absolute difference between the radii.

Roger Stafford

Subject: problem with solving symbolic equation

From: Roger Stafford

Date: 28 Aug, 2010 23:33:04

Message: 4 of 4

"michael" <bezenchu@gmail.com> wrote in message <i5a7r4$ocl$1@fred.mathworks.com>...
> i wanna find the cross points of 2 circles with center point and radius which changed each run.
> .........
- - - - - - - - - - - -
  I've decided to show you some matlab code that finds the two points of intersection of two circles. It should be more efficient and more easily understood than solutions generated automatically by 'solve'.

  Assume that you are given the centers (x1,y1) and (x2,y2) with radii r1 and r2, respectively. Then do this:

 dx = x2 - x1; dy = y2 - y1;
 sr = r2 + r1; dr = r2 - r1;
 d2 = dx^2 + dy^2;
 t = (sr^2 - d2) * (d2 - dr^2);
 if t < 0, error('The circles do not intersect.'), end
 t = sqrt(t)/(2*d2);
 dx0 = -dy * t; dy0 = dx * t;
 t = -dr * sr / d2;
 x0 = (x1 + x2 + dx*t)/2;
 y0 = (y1 + y2 + dy*t)/2;
 x3 = x0 + dx0; y3 = y0 + dy0;
 x4 = x0 - dx0; y4 = y0 - dy0;

The points (x3,y3) and (x4,y4) are the two intersections of the circles. As you move from center (x1,y1) to (x2,y2), the intersection (x3,y3) will be to the left and (x4,y4) to the right.

  If the 't' in the line

 t = (sr^2 - d2) * (d2 - dr^2);

is negative, that means the circles don't intersect and you will have to figure out how you want to proceed. In such a case the above solutions will be complex-valued.

Roger Stafford

Tags for 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