MATLAB: Solving two trigonometric equations, two unknowns

#trigonometric equationsymbolicunknowns

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

Best 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.
Related Question