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:
sym and solve

Subject: sym and solve

From: Jürgen

Date: 15 Jan, 2012 16:38:08

Message: 1 of 12

hello,

I tried to solve an equation
syms x y;
S = solve('x^2 + y^2 = 240^2','x*(s-x)+y*(t-y)-240*sqrt((s-x)^2+(t-y)^2)*0.64279=0')

in this case I get
Warning: Warning, solutions may have been lost
S =
    x: [4x1 sym]
    y: [4x1 sym]

S.x en S.y contains a very long equation that I can not directly convert to a number,
but by pasting it in the command window I can find the values

the strange thing is if I replace s en t by their values I get other solutions
syms x y;
S = solve('x^2 + y^2 = 240^2','x*(279.8-x)+y*(-528.05 -y)-240*sqrt((279.8-x)^2+(-528.05 -y)^2)*0.64279=0')

and these are the correct solutions,
x=double(S.x)
y=double(S.y)
x =
      -17.428
       207.85
y =
      -239.37
         -120
so why do I get other solutions if a use s and t as a variable?
and even stranger, if I replace 0.64279 by the cosinus of an angle than it thus work at all?
any ideas? probbaly I am making a mistake somewhere?


S = solve('x^2 + y^2 = 240^2','x*(CC(1)-x)+y*(CC(2)-y)-r*sqrt((CC(1)-x)^2+(CC(2)-y)^2)*cos((180-130)/180*pi)=0')

Subject: sym and solve

From: Roger Stafford

Date: 15 Jan, 2012 19:47:07

Message: 2 of 12

"Jürgen" wrote in message <jeuvdg$l2f$1@newscl01ah.mathworks.com>...
> hello,
>
> I tried to solve an equation
> syms x y;
> S = solve('x^2 + y^2 = 240^2','x*(s-x)+y*(t-y)-240*sqrt((s-x)^2+(t-y)^2)*0.64279=0')
>
> in this case I get
> Warning: Warning, solutions may have been lost
> S =
> x: [4x1 sym]
> y: [4x1 sym]
>
> S.x en S.y contains a very long equation that I can not directly convert to a number,
> but by pasting it in the command window I can find the values
>
> the strange thing is if I replace s en t by their values I get other solutions
> syms x y;
> S = solve('x^2 + y^2 = 240^2','x*(279.8-x)+y*(-528.05 -y)-240*sqrt((279.8-x)^2+(-528.05 -y)^2)*0.64279=0')
>
> and these are the correct solutions,
> x=double(S.x)
> y=double(S.y)
> x =
> -17.428
> 207.85
> y =
> -239.37
> -120
> so why do I get other solutions if a use s and t as a variable?
> and even stranger, if I replace 0.64279 by the cosinus of an angle than it thus work at all?
> any ideas? probbaly I am making a mistake somewhere?
>
>
> S = solve('x^2 + y^2 = 240^2','x*(CC(1)-x)+y*(CC(2)-y)-r*sqrt((CC(1)-x)^2+(CC(2)-y)^2)*cos((180-130)/180*pi)=0')
- - - - - - - - - -
  In the second equation if you move the square root expression to the right hand side and square both sides, you arrive at a fourth degree polynomial equation in x and y. If you solve the first equation for y in terms of x, substitute that solution for y in this fourth degree equation, isolate the resulting square root in that to one side, and finally square both sides, you will have arrived at an eighth degree polynomial in the single variable x. In general it will have eight roots. Some of these roots may be complex-valued. Of the real-valued ones some of them will correspond to a reversed sign in the original second equation. The 'solve' function has no way of determining the nature of these various possible outcomes until you assign specific values to the parameters 's' and 't'. Note also that it probably cannot express the solution to the above eighth degree polynomial except
in terms of what is referred to as "roots_of". In other words it puts off giving an explicit solution as a function of 's' and 't' until you give these parameters definite values. Does this help toward explaining what you have observed?

  There are certainly easier ways of approaching that problem. For example you could invoke the cosine law of triangles and use matlab's 'acos' to solve it very easily without all the above fuss. It would show you that for real solutions there are always either two roots, one root, or none, depending on the distance to the point (s,t) from the origin.

Roger Stafford

Subject: sym and solve

From: Jürgen

Date: 15 Jan, 2012 20:08:08

Message: 3 of 12

"Roger Stafford" wrote in message <jevafr$m8f$1@newscl01ah.mathworks.com>...
> "Jürgen" wrote in message <jeuvdg$l2f$1@newscl01ah.mathworks.com>...
> > hello,
> >
> > I tried to solve an equation
> > syms x y;
> > S = solve('x^2 + y^2 = 240^2','x*(s-x)+y*(t-y)-240*sqrt((s-x)^2+(t-y)^2)*0.64279=0')
> >
> > in this case I get
> > Warning: Warning, solutions may have been lost
> > S =
> > x: [4x1 sym]
> > y: [4x1 sym]
> >
> > S.x en S.y contains a very long equation that I can not directly convert to a number,
> > but by pasting it in the command window I can find the values
> >
> > the strange thing is if I replace s en t by their values I get other solutions
> > syms x y;
> > S = solve('x^2 + y^2 = 240^2','x*(279.8-x)+y*(-528.05 -y)-240*sqrt((279.8-x)^2+(-528.05 -y)^2)*0.64279=0')
> >
> > and these are the correct solutions,
> > x=double(S.x)
> > y=double(S.y)
> > x =
> > -17.428
> > 207.85
> > y =
> > -239.37
> > -120
> > so why do I get other solutions if a use s and t as a variable?
> > and even stranger, if I replace 0.64279 by the cosinus of an angle than it thus work at all?
> > any ideas? probbaly I am making a mistake somewhere?
> >
> >
> > S = solve('x^2 + y^2 = 240^2','x*(CC(1)-x)+y*(CC(2)-y)-r*sqrt((CC(1)-x)^2+(CC(2)-y)^2)*cos((180-130)/180*pi)=0')
> - - - - - - - - - -
> In the second equation if you move the square root expression to the right hand side and square both sides, you arrive at a fourth degree polynomial equation in x and y. If you solve the first equation for y in terms of x, substitute that solution for y in this fourth degree equation, isolate the resulting square root in that to one side, and finally square both sides, you will have arrived at an eighth degree polynomial in the single variable x. In general it will have eight roots. Some of these roots may be complex-valued. Of the real-valued ones some of them will correspond to a reversed sign in the original second equation. The 'solve' function has no way of determining the nature of these various possible outcomes until you assign specific values to the parameters 's' and 't'. Note also that it probably cannot express the solution to the above eighth degree polynomial
except
> in terms of what is referred to as "roots_of". In other words it puts off giving an explicit solution as a function of 's' and 't' until you give these parameters definite values. Does this help toward explaining what you have observed?
>
> There are certainly easier ways of approaching that problem. For example you could invoke the cosine law of triangles and use matlab's 'acos' to solve it very easily without all the above fuss. It would show you that for real solutions there are always either two roots, one root, or none, depending on the distance to the point (s,t) from the origin.
>
> Roger Stafford

Thank you for this explanation, I understand that it gives an eight degree polynomial, that is the reason I was not agree to solve it analytically. it explains some but not everything (I also posted it under question, because I was not sure this was the right place) I understand now why there is mentioned that solutions can be lost but
the strange thing is that when I give the value for s and t before , it still gives a symbolic output

 s=279.8;
t= 528.05;
syms x y;

S = solve('x^2 + y^2 = 240^2','x*(s-x)+y*(t-y)-240*sqrt((s-x)^2+(t-y)^2)*0.64279=0')
Warning: Warning, solutions may have been lost
S =
    x: [4x1 sym]
    y: [4x1 sym]

S.x(1) =
.50000000000000000000000000000000*(1562500.*(-.25000000000000000000000000000000*(-330086821443750000.*t*s^2-330086821443750000.*t^3)/(4882812500000.*s^2*t^2+2441406250000.*t^4+2441406250000.*s^4)+.14708014798108410932..... and many more lines,

if a copy this and paste it in the command line I get a number ( s.x(1) is indeed a correct solution

but I'll first take a look at if I can use the cosinus rule, I did not try that yet

thanksl again

Subject: sym and solve

From: Jürgen

Date: 15 Jan, 2012 20:52:08

Message: 4 of 12

cosinus rule is indeed much simpler
r=240;
s=279.8;
t= -528.05;
sym c
 S = solve('s^2+t^2-c^2-r^2+2*r*c*cos(130/180*pi)=0','c')
then
c1 = [0 0 240];
 c2 = [s t 414.35];
 pts = intersectCircles(c1, c2)

but the issue stays that the solution S is not a number , it is expressed as a function of s, t and r although these are known variables

Subject: sym and solve

From: Roger Stafford

Date: 15 Jan, 2012 21:11:08

Message: 5 of 12

"Jürgen" wrote in message <jevbn8$ppj$1@newscl01ah.mathworks.com>...

> "Roger Stafford" wrote in message <jevafr$m8f$1@newscl01ah.mathworks.com>...
> > There are certainly easier ways of approaching that problem. For example you could invoke the cosine law of triangles and use matlab's 'acos' to solve it very easily without all the above fuss. It would show you that for real solutions there are always either two roots, one root, or none, depending on the distance to the point (s,t) from the origin.

> but I'll first take a look at if I can use the cosinus rule, I did not try that yet
- - - - - - - - - -
  I was a little hasty in that last paragraph. I should have said you could use the sine law of triangles, and that, depending on the distance from (s,t) to the origin and the specified angle, there could be anywhere from none to four solutions for (x,y). The sine law permits you to determine the (signed) angle between the vector (s,t) and (x-s,y-t) to one of two (or four) possibilities. Knowing the angle between (s,t) and the x-axis will then let you find x and y.

Roger Stafford

Subject: sym and solve

From: Jürgen

Date: 15 Jan, 2012 23:32:08

Message: 6 of 12

"Roger Stafford" wrote in message <jevfdc$6u2$1@newscl01ah.mathworks.com>...
> "Jürgen" wrote in message <jevbn8$ppj$1@newscl01ah.mathworks.com>...
>
> > "Roger Stafford" wrote in message <jevafr$m8f$1@newscl01ah.mathworks.com>...
> > > There are certainly easier ways of approaching that problem. For example you could invoke the cosine law of triangles and use matlab's 'acos' to solve it very easily without all the above fuss. It would show you that for real solutions there are always either two roots, one root, or none, depending on the distance to the point (s,t) from the origin.
>
> > but I'll first take a look at if I can use the cosinus rule, I did not try that yet
> - - - - - - - - - -
> I was a little hasty in that last paragraph. I should have said you could use the sine law of triangles, and that, depending on the distance from (s,t) to the origin and the specified angle, there could be anywhere from none to four solutions for (x,y). The sine law permits you to determine the (signed) angle between the vector (s,t) and (x-s,y-t) to one of two (or four) possibilities. Knowing the angle between (s,t) and the x-axis will then let you find x and y.
>
> Roger Stafford

I have the impression you have understood more of the problem than I explained
the thing is : I need to find the intersection P(x,y) between a circle and a line. The line goes through the known point (s,t) outside the circle and has a known angle with the radial line through the intersection P(x,y).
anyway it worked out with the cosinus rule , the only problem that I still have:
the solution S is still symbolically expressed in function of r, s and t while to make it automatically ( I need to calculate a lot of points) the solution S needs to me a number.

Subject: sym and solve

From: Roger Stafford

Date: 16 Jan, 2012 03:50:09

Message: 7 of 12

"Jürgen" wrote in message <jevnlo$ef$1@newscl01ah.mathworks.com>...
> I have the impression you have understood more of the problem than I explained
> the thing is : I need to find the intersection P(x,y) between a circle and a line. The line goes through the known point (s,t) outside the circle and has a known angle with the radial line through the intersection P(x,y).
> anyway it worked out with the cosinus rule , the only problem that I still have:
> the solution S is still symbolically expressed in function of r, s and t while to make it automatically ( I need to calculate a lot of points) the solution S needs to me a number.
- - - - - - - -
  Yes, I understood all of that geometry except for your assumption that (s,t) lies outside the circle. In this case you will always get just two solutions (except in the degenerate case of a zero "known angle".) It is with (s,t) inside the circle that you would get four solutions or none.

  I suggest that it is a mistake to depend entirely on the 'solve' function to obtain methods of solving problems of this kind. You are looking for 'solve' to give some convenient formula that you can immediately apply efficiently to handle a large collection of parameter values. However, as you have seen, when 'solve' is faced with a situation where the number of valid solutions may depend on the particular values of these parameters in a complicated way, it can produce very awkward solutions which may involve values that must be discarded such as complex or negative values. The chances of finding an efficient formula then become rather small.

  In your particular problem I would forget about 'solve' altogether and solve the problem using other techniques. One approach would be to first use a coordinate system (u,v) that is rotated about the origin so that the point (s,t) lies on the positive u-axis. The rotation angle would be

 theta = atan2(t,s);

  Relative to the u,v coordinates we have a triangle with vertices A = (u,v), B = (a,0), and C = (0,0), where a = sqrt(s^2+t^2) and where B is the (s,t) point lying on the u-axis. Call the corresponding adjacent angles alpha, beta, and gamma, and call the opposite sides a, b, and c. Thus we are given sides a, and b and angle alpha, and we must find u and v. Using the cosine law we can solve for c as:

 c = b*cos(alpha) + sqrt(a^2-b^2*sin(alpha)^2);

(The other quadratic solution would be negative for a > b, so it is discarded.)

We can then obtain u = b*cos(gamma) which, again using the cosine law, becomes:

 u = b/a*(b*sin(alpha)^2-cos(alpha)*sqrt(a^2-b^2*sin(alpha)^2));

Next v = b*sin(gamma) and using the sine law this gives:

 v = b*c/a*sin(alpha);

A second symmetrically opposite pair (u,-v) must also be a solution.

  The two corresponding (x,y) solutions would then be:

 x1 = u*cos(theta)-v*sin(theta);
 y1 = u*sin(theta)+v*cos(theta);
 x2 = u*cos(theta)+v*sin(theta);
 y2 = u*sin(theta)-v*cos(theta);

  Obviously the sequence of matlab steps can be made more efficient by evaluating sine and cosine of alpha and theta only once and storing the results. The same is true of such expressions as the above square root: "sqrt(a^2-b^2*sin(alpha)^2)"

Roger Stafford

Subject: sym and solve

From: Roger Stafford

Date: 16 Jan, 2012 12:16:09

Message: 8 of 12

"Roger Stafford" wrote in message <jf06ph$cce$1@newscl01ah.mathworks.com>...
> theta = atan2(t,s);
> ........
> x1 = u*cos(theta)-v*sin(theta);
> y1 = u*sin(theta)+v*cos(theta);
> x2 = u*cos(theta)+v*sin(theta);
> y2 = u*sin(theta)-v*cos(theta);
- - - - - - - - - -
  In that suggestion I made to you there really was no need for explicitly evaluating theta with 'atan2' and then finding cos(theta) and sin(theta). I don't know why I did that. It is much simpler to just use (s/a) in place of cos(theta) and (t/a) in place of sin(theta) in transforming (u,v) over to (x1,y1) and (x2,y2).

Roger Stafford

Subject: sym and solve

From: Christopher Creutzig

Date: 16 Jan, 2012 13:43:18

Message: 9 of 12

On 15.01.12 17:38, Jürgen wrote:
> hello,
>
> I tried to solve an equation
> syms x y;
> S = solve('x^2 + y^2 = 240^2','x*(s-x)+y*(t-y)-240*sqrt((s-x)^2+(t-y)^2)*0.64279=0')

Giving strings to solve will *not* use your current values of t and s.
Instead, the input is taken as-is as a symbolic expression, using
symbolic variables s and t (and x and y, even if you did not have the
first line).

Instead, use

S = solve(x^2 + y^2 - 240^2, ...
  x*(s-x)+y*(t-y)-240*sqrt((s-x)^2+(t-y)^2)*0.64279)

I'm not sure I know of a use case where I'd really want to use strings
as inputs to solve (except name/value pairs, of course). Granted, you
have to rewrite equations as expressions, but that's pretty much the
only issue I see right now.

> the strange thing is if I replace s en t by their values I get other solutions
> syms x y;
> S = solve('x^2 + y^2 = 240^2','x*(279.8-x)+y*(-528.05 -y)-240*sqrt((279.8-x)^2+(-528.05 -y)^2)*0.64279=0')
>
> and these are the correct solutions,
> x=double(S.x)
> y=double(S.y)
> x =
> -17.428
> 207.85
> y =
> -239.37
> -120

Well, at least for me, the system *does* emit a warning in the first
case that it may return spurious solutions:

Warning: possibly spurious solutions [solvelib::checkVectorSolutions]


The two solutions you quoted are among the four ones returned:

>> subs(S.x, {s, t}, {279.8, -528.05})

ans =

 -238.7564
  154.2671
  207.8466
  -17.4281

>> subs(S.y, {s, t}, {279.8, -528.05})

ans =

  -24.4007
  183.8523
 -119.9991
 -239.3664


> and even stranger, if I replace 0.64279 by the cosinus of an angle than it thus work at all?
> any ideas? probbaly I am making a mistake somewhere?
>
>
> S = solve('x^2 + y^2 = 240^2','x*(CC(1)-x)+y*(CC(2)-y)-r*sqrt((CC(1)-x)^2+(CC(2)-y)^2)*cos((180-130)/180*pi)=0')

Works fine for me (in 2011b).


Christopher

Subject: sym and solve

From: Jürgen

Date: 20 Feb, 2012 16:26:11

Message: 10 of 12

Hi Roger, Christophe,

thanks for the explanation, I learned new things with your help,
sorry for the late answer, had to do some other things first

regards, Jürgen

Subject: sym and solve

From: Jürgen

Date: 22 Feb, 2012 08:36:11

Message: 11 of 12

I think that
 u = b*cos(gamma)
 v = b*c/a*sin(alpha)
should be the other way around
v = b*cos(gamma)
u = b*c/a*sin(alpha); but that is only I detail

Subject: sym and solve

From: Jürgen

Date: 22 Feb, 2012 09:35:12

Message: 12 of 12

sorry , the above was a mistake u= b cos(gamma) and v= bsin(gamma) just as you said, had my axes wrong

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