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:
Problems with symbolic solve solutions

Subject: Problems with symbolic solve solutions

From: Andrew

Date: 3 Jun, 2008 09:12:02

Message: 1 of 6

Hi all,

I am having trouble using the solve function on symbolic
equations. I have managed to abuse it somehow so the
solutions I get aren't always correct.

Below I have attached code for solving 6 equations in 6
unknowns and testing the solution satisfies one of the
equations. 2 of the equations are trivial, and two are non-
linear (though apparantly there is only one solution). The
gist of it is that one of the equations doesn't hold after
the numeric subsitution.

Any thoughts on what I have done to misuse this would be
great.

Cheers,
Andrew

%constants
syms xs ys xe ye lxs lys lxe lye real

%To test solution
ConstList =
{'xs', 'ys', 'xe', 'ye', 'lxs', 'lys', 'lxe', 'lye'};
ConstVals = [190.7481, 507.0450, 197.2201 483.1775,
0.1197, -0.9928, 0.1887, -0.9820];
clear i; %don't confuse it with the function i() if its
been intialised

%declare symbols of variables to solve for
syms ax bx cx ay b_y cy unreal

%These form a parametric quadratic of s
%X = ax * s^2 + bx * s + cx
%Y = ay * s^2 + b_y * s + cy
%dXds = 2*ax*s + bx;
%dYds = 2*ay*s + b_y
 
%Solve such that two ends have points [xs, ys] and [xe, ye]
%and have derivaties in the same directions as [lxs, lys]
and [lxe, lye]
%Choose the end points at s = 0 and s = 1

%Form the equations relating to positions
EquXs = cx - xs; %at s = 0, trivial
EquYs = cy - ys; %at s = 0, trivial
EquXe = ax + bx + cx - xe; %at s = 1
EquYe = ay + b_y + cy - ye; %at s = 1

%Form the equations relating to directions
%Use dot(a,b) = norm(a) * norm(b) if a and b point the same
direction
dXs = bx; dYs = b_y; %at s = 0
EquDs = dXs*lxs + dYs*lys - sqrt(dXs^2 + dYs^2)*sqrt(lxs^2
+ lys^2);

dXe = 2*ax + bx; dYe = 2*ay + b_y; %at s = 1
EquDe = dXe*lxe + dYe*lye - sqrt(dXe^2 + dYe^2)*sqrt(lxe^2
+ lye^2);

%Solve them
Sol = solve(EquXs, EquYs, EquXe, EquYe, EquDs, EquDe, ax,
bx, cx, ay, b_y, cy);
VarNames = fieldnames(Sol);

%Insert numeric values for constants
for k = 1:numel(ConstList)
    eval([ConstList{k}, ' = ', num2str(ConstVals(k)), ';']);
end

%Evalulate the variables
for k = 1:numel(VarNames)
    Temp = eval(Sol.(VarNames{k}));
    eval([VarNames{k}, ' = ', num2str(Temp, '%
10.20f'), ';']);
end

%and test against the equations
Result = eval(EquDs)

%normalising gives some insight
Mag = sqrt(bx^2 + b_y^2);
bx = bx/Mag; b_y = b_y/Mag;
[bx, b_y; lxs, lys]

%%%%%%%% End %%%%%%%

If you've made it this far the last line shows that the
derivative at the start points in the wrong direction,
implying the "-" infront of the sqrt() in EquDs has been
lost...

Subject: Problems with symbolic solve solutions

From: Andrew

Date: 4 Jun, 2008 04:38:01

Message: 2 of 6

P.S. A real solution does not exist for the values
provided in the code (not by my rationale anyway...).

"Andrew " <awbsmith@itee.uq.edu.au> wrote in message <g231t2
$20i$1@fred.mathworks.com>...
> Hi all,
>
> I am having trouble using the solve function on symbolic
> equations. I have managed to abuse it somehow so the
> solutions I get aren't always correct.
>
> Below I have attached code for solving 6 equations in 6
> unknowns and testing the solution satisfies one of the
> equations. 2 of the equations are trivial, and two are
non-
> linear (though apparantly there is only one solution).
The
> gist of it is that one of the equations doesn't hold
after
> the numeric subsitution.
>
> Any thoughts on what I have done to misuse this would be
> great.
>
> Cheers,
> Andrew
>
> %constants
> syms xs ys xe ye lxs lys lxe lye real
>
> %To test solution
> ConstList =
> {'xs', 'ys', 'xe', 'ye', 'lxs', 'lys', 'lxe', 'lye'};
> ConstVals = [190.7481, 507.0450, 197.2201 483.1775,
> 0.1197, -0.9928, 0.1887, -0.9820];
> clear i; %don't confuse it with the function i() if its
> been intialised
>
> %declare symbols of variables to solve for
> syms ax bx cx ay b_y cy unreal
>
> %These form a parametric quadratic of s
> %X = ax * s^2 + bx * s + cx
> %Y = ay * s^2 + b_y * s + cy
> %dXds = 2*ax*s + bx;
> %dYds = 2*ay*s + b_y
>
> %Solve such that two ends have points [xs, ys] and [xe,
ye]
> %and have derivaties in the same directions as [lxs, lys]
> and [lxe, lye]
> %Choose the end points at s = 0 and s = 1
>
> %Form the equations relating to positions
> EquXs = cx - xs; %at s = 0, trivial
> EquYs = cy - ys; %at s = 0, trivial
> EquXe = ax + bx + cx - xe; %at s = 1
> EquYe = ay + b_y + cy - ye; %at s = 1
>
> %Form the equations relating to directions
> %Use dot(a,b) = norm(a) * norm(b) if a and b point the
same
> direction
> dXs = bx; dYs = b_y; %at s = 0
> EquDs = dXs*lxs + dYs*lys - sqrt(dXs^2 + dYs^2)*sqrt
(lxs^2
> + lys^2);
>
> dXe = 2*ax + bx; dYe = 2*ay + b_y; %at s = 1
> EquDe = dXe*lxe + dYe*lye - sqrt(dXe^2 + dYe^2)*sqrt
(lxe^2
> + lye^2);
>
> %Solve them
> Sol = solve(EquXs, EquYs, EquXe, EquYe, EquDs, EquDe, ax,
> bx, cx, ay, b_y, cy);
> VarNames = fieldnames(Sol);
>
> %Insert numeric values for constants
> for k = 1:numel(ConstList)
> eval([ConstList{k}, ' = ', num2str(ConstVals
(k)), ';']);
> end
>
> %Evalulate the variables
> for k = 1:numel(VarNames)
> Temp = eval(Sol.(VarNames{k}));
> eval([VarNames{k}, ' = ', num2str(Temp, '%
> 10.20f'), ';']);
> end
>
> %and test against the equations
> Result = eval(EquDs)
>
> %normalising gives some insight
> Mag = sqrt(bx^2 + b_y^2);
> bx = bx/Mag; b_y = b_y/Mag;
> [bx, b_y; lxs, lys]
>
> %%%%%%%% End %%%%%%%
>
> If you've made it this far the last line shows that the
> derivative at the start points in the wrong direction,
> implying the "-" infront of the sqrt() in EquDs has been
> lost...

Subject: Problems with symbolic solve solutions

From: Roger Stafford

Date: 4 Jun, 2008 06:12:04

Message: 3 of 6

"Andrew " <awbsmith@itee.uq.edu.au> wrote in message <g231t2$20i
$1@fred.mathworks.com>...
> I am having trouble using the solve function on symbolic .....
------------
  You have managed to fool 'solve' with your problem, Andrew. I tried it out
on my primitive Symbolic Toolbox (4a) and got similar results to yours. In
some cases the 'EquDs' equation failed, in some 'EquDe' failed, in others they
both failed, and in some they both succeeded. Whenever either failed, if the
sign of its square root were reversed, the equation would then hold true.

  It seems obvious that in both 'EquDs' and 'EquDe' equations, 'solve'
transposed the square root to the other side of the equality sign and then
squared both sides, thereby eliminating the square roots. Somewhere along
the line 'solve' seems to have forgotten that the squaring operation could
produce extra roots which are not solutions to the original form of the
equations but only to the altered ones, and ended up presenting a formula
that would fail in most cases. Very likely there were four possible solutions
to the altered equations which would have come from a quartic equation, and
the one 'solve' selected works only part of the time. By that line of reasoning
there may actually exist valid solutions for all cases, even though solve's
particular solution sometimes fails.

  One surprising aspect is that the selected formula is rational in all the eight
parameters, whereas one would expect the roots of a quartic equation to
yield irrational solutions. However there is no doubt that it is a valid solution
in the roughly one-fourth of the cases, because in these cases it is accurate
out to the full extent of matlab's capability.

  Here's a simpler example where my own 'solve' succeeded but where a
'simple' operation made it fail. (Note: My Symbolic Toolbox uses somewhat
different syntax than later versions.)

 x = solve('a*x-sqrt(x^2+1)','x');

It came up with the correct answer:

 x = sqrt(a^2/(a^2-1))/a,

but when the 'simple' function is applied to this, it becomes the incorrect

 x = 1/sqrt(a^2-1).

(If 'a' were negative, this would not satisfy the equation.)

  There are a great many pitfalls like this that 'solve' could have fallen into in
deriving its erroneous answer. Writing a procedure that is smart enough to
handle all the complexities that a human could is a very difficult task indeed.
Perhaps we humans can at least derive some kind of satisfaction out of
knowing that we are not completely redundant (as yet.)

Roger Stafford

Subject: Problems with symbolic solve solutions

From: Andrew

Date: 4 Jun, 2008 06:48:02

Message: 4 of 6

Thanks for the insight Roger. Sadly when I solved it by
hand I fell into the same pitfall 'solve' did, so maybe I
am redundant already :-)

"Roger Stafford"
<ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message <g25bnk$nv3$1@fred.mathworks.com>...
> "Andrew " <awbsmith@itee.uq.edu.au> wrote in message
<g231t2$20i
> $1@fred.mathworks.com>...
> > I am having trouble using the solve function on
symbolic .....
> ------------
> You have managed to fool 'solve' with your problem,
Andrew. I tried it out
> on my primitive Symbolic Toolbox (4a) and got similar
results to yours. In
> some cases the 'EquDs' equation failed, in some 'EquDe'
failed, in others they
> both failed, and in some they both succeeded. Whenever
either failed, if the
> sign of its square root were reversed, the equation would
then hold true.
>
> It seems obvious that in both 'EquDs' and 'EquDe'
equations, 'solve'
> transposed the square root to the other side of the
equality sign and then
> squared both sides, thereby eliminating the square
roots. Somewhere along
> the line 'solve' seems to have forgotten that the
squaring operation could
> produce extra roots which are not solutions to the
original form of the
> equations but only to the altered ones, and ended up
presenting a formula
> that would fail in most cases. Very likely there were
four possible solutions
> to the altered equations which would have come from a
quartic equation, and
> the one 'solve' selected works only part of the time. By
that line of reasoning
> there may actually exist valid solutions for all cases,
even though solve's
> particular solution sometimes fails.
>
> One surprising aspect is that the selected formula is
rational in all the eight
> parameters, whereas one would expect the roots of a
quartic equation to
> yield irrational solutions. However there is no doubt
that it is a valid solution
> in the roughly one-fourth of the cases, because in these
cases it is accurate
> out to the full extent of matlab's capability.
>
> Here's a simpler example where my own 'solve' succeeded
but where a
> 'simple' operation made it fail. (Note: My Symbolic
Toolbox uses somewhat
> different syntax than later versions.)
>
> x = solve('a*x-sqrt(x^2+1)','x');
>
> It came up with the correct answer:
>
> x = sqrt(a^2/(a^2-1))/a,
>
> but when the 'simple' function is applied to this, it
becomes the incorrect
>
> x = 1/sqrt(a^2-1).
>
> (If 'a' were negative, this would not satisfy the
equation.)
>
> There are a great many pitfalls like this that 'solve'
could have fallen into in
> deriving its erroneous answer. Writing a procedure that
is smart enough to
> handle all the complexities that a human could is a very
difficult task indeed.
> Perhaps we humans can at least derive some kind of
satisfaction out of
> knowing that we are not completely redundant (as yet.)
>
> Roger Stafford
>

Subject: Problems with symbolic solve solutions

From: Roger Stafford

Date: 4 Jun, 2008 11:14:04

Message: 5 of 6

"Andrew " <awbsmith@itee.uq.edu.au> wrote in message <g25dr2$dps
$1@fred.mathworks.com>...
> Thanks for the insight Roger. Sadly when I solved it by
> hand I fell into the same pitfall 'solve' did, so maybe I
> am redundant already :-)
-----------
  A few additional comments on your parameterized parabolas, Andrew.
Earlier I speculated that your equations, after the squaring operations I
referred to, would lead to a quartic equation with four roots. In a sense that
is true. 'solve' undoubtedly is forced to go that route because of the nature
of the equations given it. However, it turns out that the quartic in this case is
a very special one which is the perfect fourth power of a linear expression, so
the four roots are all identically equal, and indeed the one and only solution
is necessarily rational in the given parameters. I marvel that 'solve' was able
to wade through all of this to come to the conclusion it did.

  However, it remains true that this single possible solution is only a true
solution to the original equations in a fraction of possible cases. This can be
seen by posing the problem differently. Instead of the form you gave with
dot(a,b) = norm(a)*norm(b), it can be expressed as

 cross(a,b) = 0

producing

 EquDs = 'bx*lys-by*lxs = 0'

which is linear in bx and by. Similarly EquDe can be expressed linearly in
terms of the coefficients. The entire problem then becomes finding the
solution to a set of six linear equations in six unknowns, and of course the
answer will be a rational function of the parameters, and most importantly it
will be unique. The only difficulty is that the direction of the vector dx/ds
and dy/ds may turn out to be the opposite sense of what is desired. It is
clear that in these cases there can be no possible solution to the original
problem (contrary to what I postulated earlier.)

  I can understand why you used the more difficult form of equation. You
hoped that you could force 'solve' to give you solutions with derivatives in the
desired direction or else tell you it was not always possible. I'm afraid 'solve',
though smart, is not THAT smart. I guess it's a case of "caviat emptor" in
problems like this.

Roger Stafford

Subject: Problems with symbolic solve solutions

From: Andrew

Date: 5 Jun, 2008 04:49:02

Message: 6 of 6

So I guess I switch to cubic equations,

X(s) = ax * s^3 + bx * s^2 + cx*s + dx
Y(s) = ay * s^3 + by * s^2 + cy*s + dx

use the 4 (modified) position equations and 2 cross product
equations to solve the other 6 coefficients in terms of ax
and ay (a linear system). Then minimise(ax^2 + ay^2)
subject to the dot product of the derivative and line
directions being > 0 at the ends (s = 0, s = 1), which
should give me a curve with as close as possible to
constant curvature (ax = 0 and ay = 0 in some cases)

Thanks for your help!

"Roger Stafford"
<ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message <g25tds$4vd$1@fred.mathworks.com>...
> "Andrew " <awbsmith@itee.uq.edu.au> wrote in message
<g25dr2$dps
> $1@fred.mathworks.com>...
> > Thanks for the insight Roger. Sadly when I solved it
by
> > hand I fell into the same pitfall 'solve' did, so maybe
I
> > am redundant already :-)
> -----------
> A few additional comments on your parameterized
parabolas, Andrew.
> Earlier I speculated that your equations, after the
squaring operations I
> referred to, would lead to a quartic equation with four
roots. In a sense that
> is true. 'solve' undoubtedly is forced to go that route
because of the nature
> of the equations given it. However, it turns out that
the quartic in this case is
> a very special one which is the perfect fourth power of a
linear expression, so
> the four roots are all identically equal, and indeed the
one and only solution
> is necessarily rational in the given parameters. I
marvel that 'solve' was able
> to wade through all of this to come to the conclusion it
did.
>
> However, it remains true that this single possible
solution is only a true
> solution to the original equations in a fraction of
possible cases. This can be
> seen by posing the problem differently. Instead of the
form you gave with
> dot(a,b) = norm(a)*norm(b), it can be expressed as
>
> cross(a,b) = 0
>
> producing
>
> EquDs = 'bx*lys-by*lxs = 0'
>
> which is linear in bx and by. Similarly EquDe can be
expressed linearly in
> terms of the coefficients. The entire problem then
becomes finding the
> solution to a set of six linear equations in six
unknowns, and of course the
> answer will be a rational function of the parameters, and
most importantly it
> will be unique. The only difficulty is that the
direction of the vector dx/ds
> and dy/ds may turn out to be the opposite sense of what
is desired. It is
> clear that in these cases there can be no possible
solution to the original
> problem (contrary to what I postulated earlier.)
>
> I can understand why you used the more difficult form
of equation. You
> hoped that you could force 'solve' to give you solutions
with derivatives in the
> desired direction or else tell you it was not always
possible. I'm afraid 'solve',
> though smart, is not THAT smart. I guess it's a case
of "caviat emptor" in
> problems like this.
>
> 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