Solving two trigonometric equations, two unknowns

12 views (last 30 days)
Hello,
It's been a while since using Matlab. I've been trying to solve the following equations with no luck, lots of error messages. I've heard a lot of mention about Symbolic which I have not come across before either. I've had a search through the existing questions but the problems I've found haven''t related to the type of problem I have.
The equations are the loop equations for a simple linkage mechanism.
R*cos(x)-L1*cos(y)=L2*cos(p)
R*sin(x)-L1*sin(y)=L2*sin(p)
Where L1, L2, p are constants.
R is a known input
x and y are the angles I would like to find.
Does it start something like this after defining variables:
>> syms L1 L2 p R x y
The=solve(R*sin(x)-L1*sin(y)=L2*sin(p))
Any help would be greatly appreciated,
Mike

Accepted Answer

Roger Stafford
Roger Stafford on 4 Apr 2013
There is no need for the answer to be that complicated. Here is a method entirely independent of 'solve' and 'fsolve'.
R*cos(x)-L1*cos(y)=L2*cos(p)
R*sin(x)-L1*sin(y)=L2*sin(p)
L1*cos(y) = R*cos(x)-L2*cos(p)
L1*sin(y) = R*sin(x)-L2*sin(p)
Now square both sides of both equations and add them to get:
(L1*cos(y))^2+(L1*sin(y))^2 = (R*cos(x)-L2*cos(p))^2+(R*sin(x)-L2*sin(p))^2
L1^2 = R^2+L2^2-2*R*L2*(cos(x)*cos(p)+sin(x)*sin(p)) = R^2+L2^2-2*R*L2*cos(x-p)
cos(x-p) = (R^2+L2^2-L1^2)/(2*R*L2)
There are two solutions for x in the interval between -pi and +pi which can be obtained using matlab's 'acos' function (assuming the above argument is between -1 and +1). For each of these x values, a corresponding y can be found using matlab's 'atan2' function:
sin(y) = (R*sin(x)-L2*sin(p))/L1
cos(y) = (R*cos(x)-L2*cos(p))/L1
y = atan2((R*sin(x)-L2*sin(p))/L1,(R*cos(x)-L2*cos(p))/L1)
Thus there are two solutions for x and y lying within -pi to +pi. Any multiple of 2*pi can obviously be added or subtracted from either x or y for an infinitude of other possible solution pairs.
  6 Comments
Mike Scott
Mike Scott on 14 Apr 2013
Edited: Mike Scott on 14 Apr 2013
This is the linkage I am trying to solve using Matlab it is makes the problem a bit clearly. Thanks in advance

Sign in to comment.

More Answers (5)

Sean de Wolski
Sean de Wolski on 4 Apr 2013
syms L1 L2 p R x y
Result=solve(R*cos(x)-L1*cos(y)==L2*cos(p),...
R*sin(x)-L1*sin(y)==L2*sin(p),x,y)
Result.x
Result.y

Mike Scott
Mike Scott on 4 Apr 2013
Edited: Walter Roberson on 6 Apr 2013
Hello,
Thanks for your response.
I have tried that and get this error message below. Is there something I can change to allow me to get a numerical answer which would be the two angles x and y?
>> syms L1 L2 p R x y
Result=solve(R*cos(x)-L1*cos(y)==L2*cos(p),...
R*sin(x)-L1*sin(y)==L2*sin(p),x,y)
Result.x
Result.y
Warning: The solutions are parametrized by the
symbols:
z = (Dom::ImageSet(PI + arcsin((L2*sin(p))/L1) +
2*PI*k, k, Z_) union Dom::ImageSet(-
arcsin((L2*sin(p))/L1) + 2*PI*k, k, Z_)) intersect
(Dom::ImageSet(PI + arccos((R + L2*cos(p))/L1) +
2*PI*k, k, Z_) union Dom::ImageSet(PI - arccos((R +
L2*cos(p))/L1) + 2*PI*k, k, Z_))
z4 = (Dom::ImageSet(arcsin((L2*sin(p))/R) + 2*PI*k,
k, Z_) union Dom::ImageSet(PI -
arcsin((L2*sin(p))/R) + 2*PI*k, k, Z_)) intersect
(Dom::ImageSet(PI + arccos((L1 - L2*cos(p))/R) +
2*PI*k, k, Z_) union Dom::ImageSet(PI - arccos((L1
- L2*cos(p))/R) + 2*PI*k, k, Z_))
> In solve at 180
Result =
x: [4x1 sym]
y: [4x1 sym]
ans =
pi
z4
2*atan((4*L2*R*tan(p/2) - 4*L1*L2*tan(p/2) + (L1^2*(((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + tan(p/2)^2*((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + 4*L1*L2*tan(p/2)))/(L1^2 + L2^2 + L1^2*tan(p/2)^2 + L2^2*tan(p/2)^2 - R^2 - R^2*tan(p/2)^2 - 2*L1*L2 + 2*L1*L2*tan(p/2)^2) + (L2^2*(((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + tan(p/2)^2*((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + 4*L1*L2*tan(p/2)))/(L1^2 + L2^2 + L1^2*tan(p/2)^2 + L2^2*tan(p/2)^2 - R^2 - R^2*tan(p/2)^2 - 2*L1*L2 + 2*L1*L2*tan(p/2)^2) - (R^2*(((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + tan(p/2)^2*((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + 4*L1*L2*tan(p/2)))/(L1^2 + L2^2 + L1^2*tan(p/2)^2 + L2^2*tan(p/2)^2 - R^2 - R^2*tan(p/2)^2 - 2*L1*L2 + 2*L1*L2*tan(p/2)^2) + (L1^2*tan(p/2)^2*(((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + tan(p/2)^2*((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + 4*L1*L2*tan(p/2)))/(L1^2 + L2^2 + L1^2*tan(p/2)^2 + L2^2*tan(p/2)^2 - R^2 - R^2*tan(p/2)^2 - 2*L1*L2 + 2*L1*L2*tan(p/2)^2) + (L2^2*tan(p/2)^2*(((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + tan(p/2)^2*((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + 4*L1*L2*tan(p/2)))/(L1^2 + L2^2 + L1^2*tan(p/2)^2 + L2^2*tan(p/2)^2 - R^2 - R^2*tan(p/2)^2 - 2*L1*L2 + 2*L1*L2*tan(p/2)^2) - (R^2*tan(p/2)^2*(((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + tan(p/2)^2*((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + 4*L1*L2*tan(p/2)))/(L1^2 + L2^2 + L1^2*tan(p/2)^2 + L2^2*tan(p/2)^2 - R^2 - R^2*tan(p/2)^2 - 2*L1*L2 + 2*L1*L2*tan(p/2)^2) - (2*L1*L2*(((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + tan(p/2)^2*((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + 4*L1*L2*tan(p/2)))/(L1^2 + L2^2 + L1^2*tan(p/2)^2 + L2^2*tan(p/2)^2 - R^2 - R^2*tan(p/2)^2 - 2*L1*L2 + 2*L1*L2*tan(p/2)^2) + (2*L1*L2*tan(p/2)^2*(((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + tan(p/2)^2*((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + 4*L1*L2*tan(p/2)))/(L1^2 + L2^2 + L1^2*tan(p/2)^2 + L2^2*tan(p/2)^2 - R^2 - R^2*tan(p/2)^2 - 2*L1*L2 + 2*L1*L2*tan(p/2)^2))/(L2^2 - L1^2 - L1^2*tan(p/2)^2 + L2^2*tan(p/2)^2 + R^2 + R^2*tan(p/2)^2 + 2*L2*R - 2*L2*R*tan(p/2)^2))
-2*atan((4*L1*L2*tan(p/2) - 4*L2*R*tan(p/2) + (L1^2*(((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + tan(p/2)^2*((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) - 4*L1*L2*tan(p/2)))/(L1^2 + L2^2 + L1^2*tan(p/2)^2 + L2^2*tan(p/2)^2 - R^2 - R^2*tan(p/2)^2 - 2*L1*L2 + 2*L1*L2*tan(p/2)^2) + (L2^2*(((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + tan(p/2)^2*((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) - 4*L1*L2*tan(p/2)))/(L1^2 + L2^2 + L1^2*tan(p/2)^2 + L2^2*tan(p/2)^2 - R^2 - R^2*tan(p/2)^2 - 2*L1*L2 + 2*L1*L2*tan(p/2)^2) - (R^2*(((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + tan(p/2)^2*((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) - 4*L1*L2*tan(p/2)))/(L1^2 + L2^2 + L1^2*tan(p/2)^2 + L2^2*tan(p/2)^2 - R^2 - R^2*tan(p/2)^2 - 2*L1*L2 + 2*L1*L2*tan(p/2)^2) + (L1^2*tan(p/2)^2*(((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + tan(p/2)^2*((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) - 4*L1*L2*tan(p/2)))/(L1^2 + L2^2 + L1^2*tan(p/2)^2 + L2^2*tan(p/2)^2 - R^2 - R^2*tan(p/2)^2 - 2*L1*L2 + 2*L1*L2*tan(p/2)^2) + (L2^2*tan(p/2)^2*(((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + tan(p/2)^2*((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) - 4*L1*L2*tan(p/2)))/(L1^2 + L2^2 + L1^2*tan(p/2)^2 + L2^2*tan(p/2)^2 - R^2 - R^2*tan(p/2)^2 - 2*L1*L2 + 2*L1*L2*tan(p/2)^2) - (R^2*tan(p/2)^2*(((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + tan(p/2)^2*((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) - 4*L1*L2*tan(p/2)))/(L1^2 + L2^2 + L1^2*tan(p/2)^2 + L2^2*tan(p/2)^2 - R^2 - R^2*tan(p/2)^2 - 2*L1*L2 + 2*L1*L2*tan(p/2)^2) - (2*L1*L2*(((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + tan(p/2)^2*((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) - 4*L1*L2*tan(p/2)))/(L1^2 + L2^2 + L1^2*tan(p/2)^2 + L2^2*tan(p/2)^2 - R^2 - R^2*tan(p/2)^2 - 2*L1*L2 + 2*L1*L2*tan(p/2)^2) + (2*L1*L2*tan(p/2)^2*(((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + tan(p/2)^2*((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) - 4*L1*L2*tan(p/2)))/(L1^2 + L2^2 + L1^2*tan(p/2)^2 + L2^2*tan(p/2)^2 - R^2 - R^2*tan(p/2)^2 - 2*L1*L2 + 2*L1*L2*tan(p/2)^2))/(L2^2 - L1^2 - L1^2*tan(p/2)^2 + L2^2*tan(p/2)^2 + R^2 + R^2*tan(p/2)^2 + 2*L2*R - 2*L2*R*tan(p/2)^2))
ans =
z
pi
-2*atan((((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + tan(p/2)^2*((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + 4*L1*L2*tan(p/2))/(L1^2 + L2^2 + L1^2*tan(p/2)^2 + L2^2*tan(p/2)^2 - R^2 - R^2*tan(p/2)^2 - 2*L1*L2 + 2*L1*L2*tan(p/2)^2))
2*atan((((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) + tan(p/2)^2*((L1 + L2 + R)*(L1 + L2 - R)*(L1 - L2 + R)*(L2 - L1 + R))^(1/2) - 4*L1*L2*tan(p/2))/(L1^2 + L2^2 + L1^2*tan(p/2)^2 + L2^2*tan(p/2)^2 - R^2 - R^2*tan(p/2)^2 - 2*L1*L2 + 2*L1*L2*tan(p/2)^2))
>>
  1 Comment
Sean de Wolski
Sean de Wolski on 4 Apr 2013
So it's working, the closed form solution is just ugly...
Try fsolve in the Optimization Toolbox to get a numerical answer instead. Or use subs() to plug in your values into the above expression and get you the values for x and y.

Sign in to comment.


Walter Roberson
Walter Roberson on 15 Apr 2013
Two solutions, based upon two roots of a polynomial of degree 2. Angles are in radians.
x = arctan((-cos(p)*2^(1/2)*((L2+L1+R)*(R+L1-L2)*(R-L2-L1)*(L2-L1+R)*(-1+cos(2*p)))^(1/2)-(R^2-L1^2+L2^2)*(-1+cos(2*p)))/(R*L2*sin(p)), (2^(1/2)*((L2+L1+R)*(R+L1-L2)*(R-L2-L1)*(L2-L1+R)*(-1+cos(2*p)))^(1/2)+(2*R^2+2*L2^2-2*L1^2)*cos(p))/(R*L2)) y = arctan((-cos(p)*2^(1/2)*((L2+L1+R)*(R+L1-L2)*(R-L2-L1)*(L2-L1+R)*(-1+cos(2*p)))^(1/2)-(R^2-L1^2-L2^2)*(-1+cos(2*p)))/(L2*sin(p)*L1), (2^(1/2)*((L2+L1+R)*(R+L1-L2)*(R-L2-L1)*(L2-L1+R)*(-1+cos(2*p)))^(1/2)+(2*R^2-2*L2^2-2*L1^2)*cos(p))/(L1*L2))
x = arctan((cos(p)*2^(1/2)*((L2+L1+R)*(R+L1-L2)*(R-L2-L1)*(L2-L1+R)*(-1+cos(2*p)))^(1/2)-(R^2-L1^2+L2^2)*(-1+cos(2*p)))/(R*L2*sin(p)), (-2^(1/2)*((L2+L1+R)*(R+L1-L2)*(R-L2-L1)*(L2-L1+R)*(-1+cos(2*p)))^(1/2)+(2*R^2+2*L2^2-2*L1^2)*cos(p))/(R*L2)) y = arctan((cos(p)*2^(1/2)*((L2+L1+R)*(R+L1-L2)*(R-L2-L1)*(L2-L1+R)*(-1+cos(2*p)))^(1/2)-(R^2-L1^2-L2^2)*(-1+cos(2*p)))/(L2*sin(p)*L1), (-2^(1/2)*((L2+L1+R)*(R+L1-L2)*(R-L2-L1)*(L2-L1+R)*(-1+cos(2*p)))^(1/2)+(2*R^2-2*L2^2-2*L1^2)*cos(p))/(L1*L2))
The two pairs can be written more compactly using some temporary variables, as there are long common sub-expressions.

Mike Scott
Mike Scott on 15 Apr 2013
I'm sorry but I really struggled trying to put your answer into context. Is that a solution to the overall problem or a part of it?
I appreciate your help but it would be useful to have it a little more explained if possible.
Thank you

ABHISHEK SARATH
ABHISHEK SARATH on 17 Mar 2020
syms a b c d phi thi beta
eq1=a*cos(thi)+b*cos(phi)-c*cos(phi)==d;%% equating real part
eq2=a*sin(thi)+b*sin(beta)-c*sin(phi)==0;%% equating imaginary part
s=solve([eq1,eq2],phi);
i am getting error like this
warning: Solutions are valid under the following conditions: (in((z - pi + asin((b*sin(beta) +
a*sin(thi))/c))/(2*pi), 'integer') | in((z - asin((b*sin(beta) + a*sin(thi))/c))/(2*pi), 'integer')) &
(in((z + acos((d - a*cos(thi))/(b - c)))/(2*pi), 'integer') | in((z - acos((d - a*cos(thi))/(b -
c)))/(2*pi), 'integer')). To include parameters and conditions in the solution, specify the
'ReturnConditions' value as 'true'.
  3 Comments
ABHISHEK SARATH
ABHISHEK SARATH on 18 Mar 2020
could u explain what does that z mean and what step should i take next
Walter Roberson
Walter Roberson on 21 Mar 2020
The process is similar to your Question
s=solve([eq1,eq2],phi, 'returnconditions', true);
>> pretty(s.conditions)
/ / d - a cos(thi) \ / d - a cos(thi) \ \
| z + acos| -------------- | z - acos| -------------- | |
/ z - pi + #1 z - #1 \ | \ b - c / \ b - c / |
| ----------- in integer or ------ in integer | and | -------------------------- in integer or -------------------------- in integer |
\ 2 pi 2 pi / \ 2 pi 2 pi /
where
/ b sin(beta) + a sin(thi) \
#1 == asin| ------------------------ |
\ c /
This tells you that you need to calculate that asin(), and that when you do, the solution you are looking for, phi, is the set of all values that are either an integer multiple of 2*pi more than the asin() or else an integer multiple of 2*pi, plus pi, more than the asin(), provided that the value also happens to be an integer multiple of 2*pi greater than +/- the acos() .
Those values are not guaranteed to exist at all.
You will probably not find this to be a very useful solution. You are trying to solve equations that have an infinite number of solutions if they have any solutions at all, and there is seldom explicit formulas for expressing an infinite number of solutions.

Sign in to comment.

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!