Solving two trigonometric equations, two unknowns

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

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

Ok I've first defined the constants and then followed the your steps as best I could understand. Are you able to let me know if what I have done it correct:
The angle P is in degrees, is this ok or should it be radians?
>> L1=0.184
L1 =
0.1840
>> L2=0.277
L2 =
0.2770
>> p=82.5
p =
82.5000
>> R=0.12
R =
0.1200
>> cos(x-p) = (R^2+L2^2-L1^2)/(2*R*L2) Undefined function or variable 'x'.
>> x=acos((R^2+L2^2-L1^2)/(2*R*L2))+p
x =
83.0326
>> y = atan2((R*sin(x)-L2*sin(p))/L1,(R*cos(x)-L2*cos(p))/L1)
y =
-2.6605
>>
Also, I don't quite understand the following line:
y = atan2((R*sin(x)-L2*sin(p))/L1,(R*cos(x)-L2*cos(p))/L1)
Is this essentially meant to produce a value for each part seperated by the comma?
Thanks again
When you use matlab's functions 'sin', 'cos', 'tan', etc. their arguments must always be in radians. The functions 'atan2' and 'acos' return their results in radians. It is very easy to convert angles in degrees over to radian measure; just multiply by pi/180. You should therefore convert your p to radians before doing those computations and x and y results will also then be in radians. To get them back to degrees just multiply them by 180/pi.
You should read the documentation for 'atan2' carefully. It requires two arguments and returns a single angle result in radians which lies between -pi and +pi. The computation
A = atan2(Y,X);
is the unique solution for 'A' lying between -pi and +pi to the equations
X = R*cos(A)
Y = R*sin(A)
for given 'X' and 'Y' where R = sqrt(X^2+Y^2), thus ranging 'A' over four full quadrants.
It possesses the two advantages over 'acos' and 'asin' functions that 1) its answer can range over four instead of just two quadrants and that 2) it maintains full accuracy throughout its entire angular range, whereas 'acos' encounters accuracy difficulties for angles near 0 and pi and 'asin' near pi/2 and -pi/2. A disadvantage is that it usually requires more computation to determine both its two arguments.
Angles need to be in radians unless you use cosd(), sind(), tand()
If I run the following code:
Error in new (line 10)
sin(y) = (R*sin(x)-L2*sin(p))/L1;
L1 = 0.184;
L2 = 0.277;
p = 1.4399;
R = 0.12;
x = (acos(R^2+L2^2-L1^2)/(2*R*L2))+p;
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);
I get this error message:
Subscript indices must either be real positive integers or
logicals.
Therefore I changed this to:
L1 = 0.184;
L2 = 0.277;
p = 1.4399;
R = 0.12;
x = (acos(R^2+L2^2-L1^2)/(2*R*L2))+p;
%sin(y) = (R*sin(x)-L2*sin(p))/L1;
y = asin((R*sin(x)-L2*sin(p))/L1);
%cos(y) = (R*cos(x)-L2*cos(p))/L1;
y = acos((R*cos(x)-L2*cos(p))/L1);
y = atan2((R*sin(x)-L2*sin(p))/L1,(R*cos(x)-L2*cos(p))/L1);
This now seems to run although the answers I am getting just make no sense as I know how the results should change with a change in r, however the answers seem to have no correlation. Also do you have any idea why the answer for x given these inputs would be 24.206 radians?
This is the linkage I am trying to solve using Matlab it is makes the problem a bit clearly. Thanks in advance
A login is required to view that.

Sign in to comment.

More Answers (5)

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
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

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.

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.
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
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

Please don't add an answer with just a "me too" question, that is not an answer at all.
You did NOT get an error! You did get a warning. It alerted you to the problem that in some cases, the solution posed could be invalid, because it does not know enough about the unknown parameters.
For example, suppose I gave you the problem to solve the equation
a*x = 0
here a is an unknown scalar variables. Is the solution just this?
x = 0/a = 0
But what if a is zero? Then the solution is 0/0, something undefined. As such, I might want to warn you that the solution 0/a=0 is invalid in some circumstances. Actually, I'm somewhat surprised that MATLAB does not complain in this simple case: solve(x*a == 0,x), since a can indeed be zero.
Regardless, MATLAB allows you the ability to blanketly assume what it needs to assume to provide a solution. It tells you to do this:
To include parameters and conditions in the solution, specify the 'ReturnConditions' value as 'true'.
could u explain what does that z mean and what step should i take next
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

Community Treasure Hunt

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

Start Hunting!