Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

solve 2 equations with parameters

4 views
Skip to first unread message

ewodul v Dulmen

unread,
Dec 11, 2009, 4:59:06 AM12/11/09
to
Hello,

I'm trying to solve the following equation:


X = 1.5088;
Y = 1.266;

[q1,q2]=solve('cos(q1)+cos(q1+q2)=X', 'sin(q1)+sin(q1+q2)=Y');

Matlab gives the following error:

Warning: Explicit solution could not be found.

While there is a solution, because when I type

[q1,q2]=solve('cos(q1)+cos(q1+q2)=1.5088', 'sin(q1)+sin(q1+q2)=1.266');

(so without the X and Y in the equations) it gives me q1 = 0.52 and q2=0.349 which are the correct solutions. Is there a way to implement these X and Y in the solve equation? It's needed because I have to calculate a lot of angles for a trajectory.

ewodul v Dulmen

unread,
Dec 11, 2009, 5:04:03 AM12/11/09
to

Torsten Hennig

unread,
Dec 11, 2009, 6:37:00 AM12/11/09
to

I'm not sure, but maybe this will work:

X = sym('1.5088');
Y = sym('1.266');



[q1,q2]=solve('cos(q1)+cos(q1+q2)=X',
'sin(q1)+sin(q1+q2)=Y');

Best wishes
Torsten.

Steven Lord

unread,
Dec 11, 2009, 10:05:22 AM12/11/09
to

"ewodul v Dulmen" <ewo...@gmail.com> wrote in message
news:hft55a$qau$1...@fred.mathworks.com...

> Hello,
>
> I'm trying to solve the following equation:
>
>
> X = 1.5088;
> Y = 1.266;
>
> [q1,q2]=solve('cos(q1)+cos(q1+q2)=X', 'sin(q1)+sin(q1+q2)=Y');
>
> Matlab gives the following error:
>
> Warning: Explicit solution could not be found.

That's correct. The way you specified your equations, you're trying to
solve using generic variables X and Y, not the values of those variables.
[SOLVE doesn't know that you intended for it to look in the workspace to
determine the value.]

Either write your equations like:

syms q1 q2


X = 1.5088;
Y = 1.266;

S=solve(cos(q1)+cos(q1+q2)-X, sin(q1)+sin(q1+q2)-Y);
q1 = S.q1; q2 = S.q2;

or use SUBS to substitute values into the expression before passing those
expressions into SOLVE.

--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


Bruce

unread,
Jan 2, 2010, 10:21:04 PM1/2/10
to
"ewodul v Dulmen" <ewo...@gmail.com> wrote in message <hft5ej$gm3$1...@fred.mathworks.com>...

Hello Ewodul,

Actually the 'sym' command (Torsten gave) does not work either and gives the same warning msg.

Try the subs command...

>> a=subs('cos(q1)+cos(q1+q2)=X','X',1.5088)

a =

cos(q1 + q2) + cos(q1) = 943/625

>> b=subs('sin(q1)+sin(q1+q2)=Y','Y',1.266)

b =

sin(q1 + q2) + sin(q1) = 633/500

>> Q=solve(a,b);
>> [q1,q2]=solve(a,b);
>> [q1,q2]=solve(a,b)

q1 =

0.52347481128793986479835792628225237


q2 =

0.34928770183974407260094794663641128

These are the results you want. You can now use the subs commands in a loop
and generate angles galore with the 'solve' command.

Regards

Bruce

Greg Heath

unread,
Jan 2, 2010, 11:21:16 PM1/2/10
to
On Dec 11 2009, 6:37 am, Torsten Hennig

It works

>> [q1,q2]=solve('cos(q1)+cos(q1+q2)=X','sin(q1)+sin(q1+q2)=Y')

q1 =
[ atan((-X/(4*X^2+4*Y^2)*(4*X*Y^2+4*X^3+4*(-2*X^2*Y^4-
X^4*Y^2+4*X^2*Y^2-Y^6+4*Y^4)^(1/2))+X^2+Y^2)/Y,1/(4*X^2+4*Y^2)*
(4*X*Y^2+4*X^3+4*(-2*X^2*Y^4-X^4*Y^2+4*X^2*Y^2-Y^6+4*Y^4)^(1/2)))]
[ atan((-X/(4*X^2+4*Y^2)*(4*X*Y^2+4*X^3-4*(-2*X^2*Y^4-
X^4*Y^2+4*X^2*Y^2-Y^6+4*Y^4)^(1/2))+X^2+Y^2)/Y,1/(4*X^2+4*Y^2)*
(4*X*Y^2+4*X^3-4*(-2*X^2*Y^4-X^4*Y^2+4*X^2*Y^2-Y^6+4*Y^4)^(1/2)))]
q2 =
[ atan((1/2/(4*X^2+4*Y^2)*(4*X*Y^2+4*X^3+4*(-2*X^2*Y^4-
X^4*Y^2+4*X^2*Y^2-Y^6+4*Y^4)^(1/2))*X^2+1/2/(4*X^2+4*Y^2)*
(4*X*Y^2+4*X^3+4*(-2*X^2*Y^4-X^4*Y^2+4*X^2*Y^2-Y^6+4*Y^4)^(1/2))
*Y^2-2*X)*Y/(-X/(4*X^2+4*Y^2)*(4*X*Y^2+4*X^3+4*(-2*X^2*Y^4-
X^4*Y^2+4*X^2*Y^2-Y^6+4*Y^4)^(1/2))+X^2+Y^2),1/2*X^2+1/2*Y^2-1)]
[ atan((1/2/(4*X^2+4*Y^2)*(4*X*Y^2+4*X^3-4*(-2*X^2*Y^4-
X^4*Y^2+4*X^2*Y^2-Y^6+4*Y^4)^(1/2))*X^2+1/2/(4*X^2+4*Y^2)*
(4*X*Y^2+4*X^3-4*(-2*X^2*Y^4-X^4*Y^2+4*X^2*Y^2-Y^6+4*Y^4)^(1/2))
*Y^2-2*X)*Y/(-X/(4*X^2+4*Y^2)*(4*X*Y^2+4*X^3-4*(-2*X^2*Y^4-
X^4*Y^2+4*X^2*Y^2-Y^6+4*Y^4)^(1/2))+X^2+Y^2),1/2*X^2+1/2*Y^2-1)]
>>

Hope this helps.

Greg

Bruce

unread,
Jan 4, 2010, 12:13:03 AM1/4/10
to
Greg Heath <he...@alumni.brown.edu> wrote in message <5f26b42c-4604-45e7...@p32g2000vbi.googlegroups.com>...

Hello Greg

No, that is a symbolic answer you have produced, not a numeric one which is what is desired to produce the angles (in radians) for Ewodul's trajectories he mentioned. You have not produced a numeric answer because X and Y are not set to numeric values.
NB: If you had done this with the 'subs' command as I explained you would get a
numeric result.

This code of yours does not work either(to get a numeric result)...


> > X = sym('1.5088');
> > Y = sym('1.266');

it causes a warning message instead when run with your code above. I notice
you did not use it this time around...

In short you need the subs command to get a numeric result.

Regards

Bruce

Greg Heath

unread,
Jan 5, 2010, 7:40:53 PM1/5/10
to
On Jan 4, 12:13 am, "Bruce " <braineor...@gmail.com> wrote:
> Greg Heath <he...@alumni.brown.edu> wrote in message <5f26b42c-4604-45e7-8b06-d8867f4bb...@p32g2000vbi.googlegroups.com>...

> > On Dec 11 2009, 6:37 am, Torsten Hennig
> > <Torsten.Hen...@umsicht.fhg.de> wrote:
> > > > Hello,
>
> > > > I'm trying tosolvethe following equation:
>
> > > >X= 1.5088;
> > > >Y= 1.266;

>
> > > > [q1,q2]=solve('cos(q1)+cos(q1+q2)=X',
> > > > 'sin(q1)+sin(q1+q2)=Y');
>
> > > > Matlab gives the following error:
>
> > > > Warning: Explicit solution could not be found.
>
> > > > While there is a solution, because when I type
>
> > > > [q1,q2]=solve('cos(q1)+cos(q1+q2)=1.5088',
> > > > 'sin(q1)+sin(q1+q2)=1.266');
>
> > > > (so without theXandYin the equations) it gives me
> > > >q1= 0.52 andq2=0.349 which are the correct

> > > > solutions. Is there a way to implement theseXandY
> > > > in thesolveequation? It's needed because I have to

> > > > calculate a lot of angles for a trajectory.
>
> > > I'm not sure, but maybe this will work:
>
> > >X= sym('1.5088');
> > >Y= sym('1.266');
> No, that is a symbolic answer you have produced, not a numeric one which is what is desired to produce the angles (in radians) for Ewodul's trajectories he mentioned. You have not produced a numeric answer becauseXandYare not set to numeric values.

My point is:

Ewodul obtained an error message using the same code that
I used to obtain the symbolic solution. So my guess is that
he did something different than what he posted.

In addition:

In general one would use the explicit symbolic solution to obtain
numerical results instead of solving the equations again and again
for each of the gazillion times you want an answer.

However, considering the number of numerical operations
in the explicit solve solution, repeated solving could be faster.
I'll look into the speed factor when I get some time.

> NB: If you had done this with the 'subs' command as I explained you would get a
> numeric result.  
>
> This code of yours does not work either(to get a numeric result)...
> >> X= sym('1.5088');

> > >Y= sym('1.266');


>
> it causes a warning message instead when run with your code above. I notice
> you did not use it this time around...

> In short you need the subs command to get a numeric result.

You must be directing this at someone else. All I did was show
that the original code does produce an answer instead of an
error message.

Hope I made my self clear. My other point made above, refers
to the relative speed of a gazillion uses of subs or a gazillion
uses of solve.

Hope this helps.

Greg

Greg Heath

unread,
Jan 5, 2010, 9:06:06 PM1/5/10
to
On Dec 11 2009, 10:05 am, "Steven Lord" <sl...@mathworks.com> wrote:
> "ewodul v Dulmen" <ewo...@gmail.com> wrote in
> messagenews:hft55a$qau$1...@fred.mathworks.com...

>
> > Hello,
>
> > I'm trying to solve the following equation:
>
> >X= 1.5088;
> >Y= 1.266;

>
> > [q1,q2]=solve('cos(q1)+cos(q1+q2)=X', 'sin(q1)+sin(q1+q2)=Y');
>
> > Matlab gives the following error:
>
> > Warning: Explicit solution could not be found.
>
> That's correct. The way you specified your equations, you're
> trying to solve using generic variables X and Y, not the values of > those variables.
>
> [SOLVE doesn't know that you intended for it to look in the
> workspace to determine the value.]
>
> Either write your equations like:
>
> syms q1q2
> X= 1.5088;Y= 1.266;
> S=solve(cos(q1)+cos(q1+q2)-X,sin(q1)+sin(q1+q2)-Y);
> q1= S.q1;q2= S.q2;

Removing the semicolons yields

>> syms q1 q2
X = 1.508 , Y = 1.266
S = solve(cos(q1)+cos(q1+q2)-X, sin(q1)+sin(q1+q2)-Y)
q1 = S.q1, q2 = S.q2

X =
1.5080
Y =
1.2660
S =
q1: [2x1 sym]
q2: [2x1 sym]
q1 =
[ atan((633/1000-377/20450225500*53152140196599^(1/2))/
(377/500+3/193841000*53152140196599^(1/2)))]
[ atan((633/1000+377/20450225500*53152140196599^(1/2))/
(377/500-3/193841000*53152140196599^(1/2)))]
q2 =
[ atan(1/19800451*53152140196599^(1/2))]
[ -atan(1/19800451*53152140196599^(1/2))]

whereas using

syms q1 q2
X = sym('1.5088'), Y = sym('1.266')
[q1 q2] = solve(cos(q1)+cos(q1+q2)-X, sin(q1)+sin(q1+q2)-Y)

yields

>> syms q1 q2
X = sym('1.5088'), Y = sym('1.266')
[q1 q2] = solve(cos(q1)+cos(q1+q2)-X, sin(q1)+sin(q1+q2)-Y)

X =
1.5088
Y =
1.266

q1 =
[ .87276251312768393739930587291866]
[ .52347481128793986479835792628225]
q2 =
[ -.34928770183974407260094794663641]
[ .34928770183974407260094794663641]


> or use SUBS to substitute values into the expression before
> passing those expressions into SOLVE.

There still remains the question of the relative
speed of using SOLVE a gazillion times or to
substitute in to the symbolic solutions obtained
by using quotes in the argument of SOLVE.

Hope this helps.

Greg

Greg Heath

unread,
Jan 6, 2010, 9:15:04 AM1/6/10
to
On Jan 5, 9:06 pm, Greg Heath <he...@alumni.brown.edu> wrote:
> On Dec 11 2009, 10:05 am, "Steven Lord" <sl...@mathworks.com> wrote:
> > "ewodul v Dulmen" <ewo...@gmail.com> wrote in
> > messagenews:hft55a$qau$1...@fred.mathworks.com...
>
> > > I'm trying to solve the following equation:
>
> > >X= 1.5088;
> > >Y= 1.266;
>
> > > [q1,q2]=solve('cos(q1)+cos(q1+q2)=X', 'sin(q1)+sin(q1+q2)=Y');
>
> > > Matlab gives the following error:
>
> > > Warning: Explicit solution could not be found.
>
> > That's correct.  The way you specified your equations, you're
> > trying to solve using generic variables X and Y, not the values of > those variables.
>
> > [SOLVE doesn't know that you intended for it to look in the
> > workspace to  determine the value.]
>
> > Either write your equations like:
>
> > syms q1q2
> > X= 1.5088;Y= 1.266;
> > S=solve(cos(q1)+cos(q1+q2)-X,sin(q1)+sin(q1+q2)-Y);
> >  q1= S.q1;q2= S.q2;

-----SNIP

> > or use SUBS to substitute values into the expression before
> > passing those expressions into SOLVE.
>
> There still remains the question of the relative
> speed of using SOLVE a gazillion times or to
> substitute in to the symbolic solutions obtained
> by using quotes in the argument of SOLVE.

Yep. It looks like repetetive uses of SOLVE is a gazillion times
slower. The times for the trials below were

[ 32.2, 39.4, 37.0 ] sec for N = 128 repetitions of SOLVE

vs

[ 33.8, 33.8, 33.9 ] sec for N = 100,00 repetitions using the explicit
solutions

I ran the following codes 3 times each. Note that MAPLE is NOT needed
for
the explicit solution. That may be the reason for the three order of
magnitude
slowdown.

clear all, clc
N = 128;
state0 = 2^31*rand;
rand('state',state0);
p1 = -pi+2*pi*rand(N,1);
p2 = -pi+2*pi*rand(N,1);
X = cos(p1)+cos(p1+p2);
Y = sin(p1)+sin(p1+p2);

tic
for i = 1:N

syms q1 q2
a = cos(q1)+cos(q1+q2)-X(i);
b = sin(q1)+sin(q1+q2)-Y(i);

[q1 q2] = solve(a,b); % numerical expression need sym in loop
Q = subs([q1 q2]) % floating point

end
toc % [32.2, 39.4, 37.0 ] sec for N = 128

clear all, clc
N = 1e5;
state0 = 2^31*rand;
rand('state',state0);
p1 = -pi+2*pi*rand(N,1);
p2 = -pi+2*pi*rand(N,1);
x = cos(p1)+cos(p1+p2);
y = sin(p1)+sin(p1+p2);
tic
for i = 1:N
X = x(i);
Y = y(i);
q1 = ...
[ atan2((Y^2-1/(4*Y^2+4*X^2)*(4*X^3+4*Y^2*X+4*(-X^4*Y^2-2*Y^4*X^2-
Y^6+4*Y^4+4*Y^2*X^2)^(1/2))*X+X^2)/Y,1/(4*Y^2+4*X^2)*(4*X^3+4*Y^2*X+4*
(-X^4*Y^2-2*Y^4*X^2-Y^6+4*Y^4+4*Y^2*X^2)^(1/2)))
atan2((Y^2-1/(4*Y^2+4*X^2)*(4*X^3+4*Y^2*X-4*(-X^4*Y^2-2*Y^4*X^2-
Y^6+4*Y^4+4*Y^2*X^2)^(1/2))*X+X^2)/Y,1/(4*Y^2+4*X^2)*(4*X^3+4*Y^2*X-4*
(-X^4*Y^2-2*Y^4*X^2-Y^6+4*Y^4+4*Y^2*X^2)^(1/2)))]
q2 = ...
[ atan2((1/2/(4*Y^2+4*X^2)*(4*X^3+4*Y^2*X+4*(-X^4*Y^2-2*Y^4*X^2-
Y^6+4*Y^4+4*Y^2*X^2)^(1/2))*X^2+1/2*Y^2/(4*Y^2+4*X^2)*(4*X^3+4*Y^2*X+4*
(-X^4*Y^2-2*Y^4*X^2-Y^6+4*Y^4+4*Y^2*X^2)^(1/2))-2*X)*Y/(Y^2-1/
(4*Y^2+4*X^2)*(4*X^3+4*Y^2*X+4*(-X^4*Y^2-2*Y^4*X^2-Y^6+4*Y^4+4*Y^2*X^2)
^(1/2))*X+X^2),1/2*X^2+1/2*Y^2-1)
atan2((1/2/(4*Y^2+4*X^2)*(4*X^3+4*Y^2*X-4*(-X^4*Y^2-2*Y^4*X^2-
Y^6+4*Y^4+4*Y^2*X^2)^(1/2))*X^2+1/2*Y^2/(4*Y^2+4*X^2)*(4*X^3+4*Y^2*X-4*
(-X^4*Y^2-2*Y^4*X^2-Y^6+4*Y^4+4*Y^2*X^2)^(1/2))-2*X)*Y/(Y^2-1/
(4*Y^2+4*X^2)*(4*X^3+4*Y^2*X-4*(-X^4*Y^2-2*Y^4*X^2-Y^6+4*Y^4+4*Y^2*X^2)
^(1/2))*X+X^2),1/2*X^2+1/2*Y^2-1)]
end
toc % [33.8 33.8 33.9] for 3 runs

The times could be made 25% shorter because the two solutions for q2
are negatives of each other. Or even 50% shorter if, given q2, there
is a compact explicit relationship between the two solutions of q1.

The only thing left is how to determine which of the 4 q1/q2
combinations
is the correct one. If these are sequential physical measurements
perhaps this can be done by knowing a previous correct solution and
choosing the solution the best fits a continuity modulo 2pi
constraint.

Hope this helps.

Greg


Greg Heath

unread,
Jan 6, 2010, 10:24:05 PM1/6/10
to

There is. The equations can be solved for cos(q1) and
sin(q1) . Taking the ratio yields

q1 = atan2( (1+cos(q2) )*Y - sin(q2)*X, ( 1+cos(q2) )*X
+ sin(q2)*Y) ;

> The only thing left is how to determine which of the 4 q1/q2
> combinations is the correct one.

From the above relationship it is clear that there are only two
solutions (q1.1,q2) and (q1.2,-q2).

Tom

unread,
Jan 13, 2010, 5:22:03 PM1/13/10
to
"Bruce " <brain...@gmail.com> wrote in message <hhrtcv$pj0$1...@fred.mathworks.com>...

Can someone please explain the notation for me:
"atan((-X/(4*X^2+4*Y^2)*(4*X*Y^2+4*X^3+4*(-2*X^2*Y^4-X^4*Y^2+4*X^2*Y^2-Y^6+4*Y^4)^(1/2))+X^2+Y^2)/Y,1/(4*X^2+4*Y^2)*
(4*X*Y^2+4*X^3+4*(-2*X^2*Y^4-X^4*Y^2+4*X^2*Y^2-Y^6+4*Y^4)^(1/2)))" contains a comma about half-way through. I can't for the life of me figure out what that means.

Thanks in advance.

Greg Heath

unread,
Jan 13, 2010, 11:25:17 PM1/13/10
to
On Jan 13, 5:22 pm, "Tom " <tommacREM...@gmail.com> wrote:
> "Bruce " <braineor...@gmail.com> wrote in message <hhrtcv$pj...@fred.mathworks.com>...
> > Greg Heath <he...@alumni.brown.edu> wrote in message <5f26b42c-4604-45e7-8b06-d8867f4bb...@p32g2000vbi.googlegroups.com>...
> "atan((-X/(4*X^2+4*Y^2)*(4*X*Y^2+4*X^3+4*(-2*X^2*Y^4-X^4*Y^2+4*X^2*Y^2-Y^6+­4*Y^4)^(1/2))+X^2+Y^2)/Y,1/(4*X^2+4*Y^2)*

> (4*X*Y^2+4*X^3+4*(-2*X^2*Y^4-X^4*Y^2+4*X^2*Y^2-Y^6+4*Y^4)^(1/2)))"
> contains a comma about half-way through.  I can't for the life of me figure out what
> that means.

MAPLE'S ATAN(Y,X)
is equivalent to
MATLAB'S ATAN2(Y,X)

doc atan2
help atan2

Hope this helps,

Greg

Miroslav Balda

unread,
Jan 14, 2010, 5:22:06 PM1/14/10
to
"ewodul v Dulmen" <ewo...@gmail.com> wrote in message <hft5ej$gm3$1...@fred.mathworks.com>...

Hi,
If you are intersted only in mumerical solution, you do not need to use symbolic toolbox. I tried to solve your problem by my function LMFnlsq
http://www.mathworks.com/matlabcentral/fileexchange/17534
applied in the following script:
% Ewodul
while 1
X = inp('X',1.5088);
if isnan(X), break, end
Y = inp('Y',1.266);
res = @(q) [cos(q(1)) + cos(q(1)+q(2)) - X
sin(q(1)) + sin(q(1)+q(2)) - Y];
q = LMFnlsq(res,[.5;.5])
end

The function inp for keyboard input with default value can be found at
http://www.mathworks.com/matlabcentral/fileexchange/9033
It has been inserted here in order to have an easy way how to change X and Y. The while cycle ends when NaN is input for X. It is obvious from the following results:

>> Ewodul
X = 1.5088 =>
Y = 1.2660 =>
q =
0.5235
0.3493
X = 1.5088 => 1.3
Y = 1.2660 => .9
q =
-0.0535
1.3181
X = 1.5088 => nan
I hope that it could help you.

Mira

Qinrui Tang

unread,
Apr 8, 2016, 5:29:08 AM4/8/16
to
"Jan De Vries" wrote in message <hft5ej$gm3$1...@fred.mathworks.com>...
You could use vpasolve rather than solve.
The codes are:

X = 1.5088;
Y = 1.266;
syms q1 q2;
[q1,q2]=vpasolve(cos(q1)+cos(q1+q2)==X, sin(q1)+sin(q1+q2)==Y, q1, q2)

you would get the answers:

q1 =

0.52347481128793986479835792628225


q2 =

0.34928770183974407260094794663641

I have tried the codes and they work pretty well.
Although this question was asked seven years ago, someone may have the same question. Then I hope, this reply help someone else.

Regards,

Qinrui
0 new messages