Solving two trigonometric equations, two unknowns

조회 수: 11 (최근 30일)
Mike Scott
Mike Scott 2013년 4월 4일
댓글: Walter Roberson 2020년 3월 21일
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

채택된 답변

Roger Stafford
Roger Stafford 2013년 4월 4일
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
Mike Scott
Mike Scott 2013년 4월 14일
편집: Mike Scott 2013년 4월 14일
This is the linkage I am trying to solve using Matlab it is makes the problem a bit clearly. Thanks in advance
Walter Roberson
Walter Roberson 2013년 4월 15일
A login is required to view that.

댓글을 달려면 로그인하십시오.

추가 답변 (5개)

Sean de Wolski
Sean de Wolski 2013년 4월 4일
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 2013년 4월 4일
편집: Walter Roberson 2013년 4월 6일
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
Sean de Wolski
Sean de Wolski 2013년 4월 4일
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.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2013년 4월 15일
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 2013년 4월 15일
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 2020년 3월 17일
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
ABHISHEK SARATH
ABHISHEK SARATH 2020년 3월 18일
could u explain what does that z mean and what step should i take next
Walter Roberson
Walter Roberson 2020년 3월 21일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by