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.
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.
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
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
It works
>> [q1,q2]=solve('cos(q1)+cos(q1+q2)=X','sin(q1)+sin(q1+q2)=Y')
q1 =
[ atan((-X/(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/2*Y^2-1)]
>>
Hope this helps.
Greg
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
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
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
-----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/(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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
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).
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.
MAPLE'S ATAN(Y,X)
is equivalent to
MATLAB'S ATAN2(Y,X)
doc atan2
help atan2
Hope this helps,
Greg
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