Solving systems of trig equations

1 view (last 30 days)
Jon
Jon on 24 Mar 2023
Edited: David Goodmanson on 26 Mar 2023
Hi, I am new to MATLAB. I want to solve the following system of trig equations:
I want to get solutions of in terms of (which are unknown real value bounded by ).
What would be the correct matlab code for this?

Answers (2)

David Goodmanson
David Goodmanson on 25 Mar 2023
Edited: David Goodmanson on 26 Mar 2023
Hi Jon,
Here is one method. To come up with values of r and g that work, it appears to be easier to think in terms of R = r^2 and G = g^2. The code below computes four sets of angle pairs theta1, theta2 . Since there was some squaring involved to obtain the solution I thought that two of those sets were not going to work, but instead all four pairs worked. That's because every term in both equations is the product of a sine and a cosine. So if theta1, theta2 are a solution, then for the three cases
theta1--> -theta1 theta1--> pi-theta1 theta1--> pi+theta1 (same for theta2)
either all the sines or all the cosines, or both, pick up a factor of -1. Therefore the two equations are unaffected.
For a real solution, the set of allowed values for r and g are those for which the argument in acos(sqrt(...)) is bounded by
0 <= (a2-R)/(a2*(1-R))) <= 1.
with a2 defined below.
% sample values
r = sqrt(1/2)
g = sqrt(2/3)
R = r^2;
G = g^2;
a2 = G*(1-R)^2/R^2;
theta20 = acos(sqrt((a2-R)/(a2*(1-R)))); % basic solution for theta2
% find four solutions for theta2, assuming sqrt above can be of either sign
theta2 = [theta20 -theta20 (pi-theta20) -(pi-theta20)]
theta1 = atan2(sin(theta2)/(-r),cos(theta2))
% check, should be small
r^2*sin(theta1).*cos(theta1) -r*g*cos(theta1).*sin(theta2) -g*sin(theta1).*cos(theta2)
r*sin(theta1).*cos(theta2) + cos(theta1).*sin(theta2)
theta2 = 0.7854 -0.7854 2.3562 -2.3562
theta1 = -0.9553 0.9553 -2.1863 2.1863
% checks
ans = 1.0e-15 *
0.1110 -0.1110 -0.1110 0.1110
ans =
0 0 0 0

John D'Errico
John D'Errico on 24 Mar 2023
Edited: John D'Errico on 24 Mar 2023
My gut says no symbolic solution will exist. But that does not mean my gut is accurate here. First, I would throw pencil and paper at it.
Divide the second equation by cos(theta1)*cos(theta2). That leaves you with
r*tan(theta1) = -tan(theta2)
As such, the variables can be separated.
Do the same thing for the first equation. Now, we see
r^2*tan(theta1)*cos(theta1)/cos(theta2) - r*(1-g)*tan(theta2) - (1-g)*tan(theta1) = 0
unfortunately, the first term does not simplify, so paper and pencil seems to leave me stuck. Ok. So throw it into solve. Hey, it might work. (Actually, the online MATLAB in Ansers will not do the trick I think, so I had to do this offline.)
syms r g real positive
syms t1 t2 real
Eq(1) = r*sin(t1)*cos(t2) + cos(t1)*sin(t2) == 0
Eq(2) = r^2*sin(t1)*cos(t1) - r*(1-g)*cos(t1)*sin(t2) - (1-g)*sin(t1)*cos(t2) == 0
sol = solve(Eq,[t1,t2],'returnconditions',true)
And solve tells me:
sol =
struct with fields:
t1: [9×1 sym]
t2: [9×1 sym]
parameters: [k l m z z1]
conditions: [9×1 sym]
ans =
z
-pi*(2*l - 1)
pi*k
2*pi*k - 2*atan(((-(4*g - 2*(r + 1)^(3/2)*((r - 1)^3*(g - g*r^2 + r^2 + r^3 - 1)*(g*r^2 - g - r^2 + r^3 + 1))^(1/2) - 10*g*r^2 + 8*g*r^4 - 2*g*r^6 - 2*g^2 + 5*r^2 - 4*r^4 + 2*r^6 + 5*g^2*r^2 - 4*g^2*r^4 + g^2*r^6 + 2*g*(r + 1)^(3/2)*((r - 1)^3*(g - g*r^2 + r^2 + r^3 - 1)*(g*r^2 - g - r^2 + r^3 + 1))^(1/2) - 2)/(- g^2*r^6 + 2*g^2*r^4 - g^2*r^2 + 2*g*r^6 - 4*g*r^4 + 2*g*r^2 + 2*r^4 - r^2))^(3/2)*(r - g*r + g*r^3 - 2*r^3))/(2*(g - g*r^2 + r^2 - 1)) - ((-(4*g - 2*(r + 1)^(3/2)*((r - 1)^3*(g - g*r^2 + r^2 + r^3 - 1)*(g*r^2 - g - r^2 + r^3 + 1))^(1/2) - 10*g*r^2 + 8*g*r^4 - 2*g*r^6 - 2*g^2 + 5*r^2 - 4*r^4 + 2*r^6 + 5*g^2*r^2 - 4*g^2*r^4 + g^2*r^6 + 2*g*(r + 1)^(3/2)*((r - 1)^3*(g - g*r^2 + r^2 + r^3 - 1)*(g*r^2 - g - r^2 + r^3 + 1))^(1/2) - 2)/(- g^2*r^6 + 2*g^2*r^4 - g^2*r^2 + 2*g*r^6 - 4*g*r^4 + 2*g*r^2 + 2*r^4 - r^2))^(1/2)*(g^2*r^6 - 6*g^2*r^4 + 9*g^2*r^2 - 4*g^2 - 4*g*r^6 + 14*g*r^4 - 18*g*r^2 + 8*g + 4*r^6 - 8*r^4 + 9*r^2 - 4))/(2*(r - g*r + g*r^3)*(g - g*r^2 + r^2 - 1)))
2*atan(((-(4*g - 2*(r + 1)^(3/2)*((r - 1)^3*(g - g*r^2 + r^2 + r^3 - 1)*(g*r^2 - g - r^2 + r^3 + 1))^(1/2) - 10*g*r^2 + 8*g*r^4 - 2*g*r^6 - 2*g^2 + 5*r^2 - 4*r^4 + 2*r^6 + 5*g^2*r^2 - 4*g^2*r^4 + g^2*r^6 + 2*g*(r + 1)^(3/2)*((r - 1)^3*(g - g*r^2 + r^2 + r^3 - 1)*(g*r^2 - g - r^2 + r^3 + 1))^(1/2) - 2)/(- g^2*r^6 + 2*g^2*r^4 - g^2*r^2 + 2*g*r^6 - 4*g*r^4 + 2*g*r^2 + 2*r^4 - r^2))^(3/2)*(r - g*r + g*r^3 - 2*r^3))/(2*(g - g*r^2 + r^2 - 1)) - ((-(4*g - 2*(r + 1)^(3/2)*((r - 1)^3*(g - g*r^2 + r^2 + r^3 - 1)*(g*r^2 - g - r^2 + r^3 + 1))^(1/2) - 10*g*r^2 + 8*g*r^4 - 2*g*r^6 - 2*g^2 + 5*r^2 - 4*r^4 + 2*r^6 + 5*g^2*r^2 - 4*g^2*r^4 + g^2*r^6 + 2*g*(r + 1)^(3/2)*((r - 1)^3*(g - g*r^2 + r^2 + r^3 - 1)*(g*r^2 - g - r^2 + r^3 + 1))^(1/2) - 2)/(- g^2*r^6 + 2*g^2*r^4 - g^2*r^2 + 2*g*r^6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r^6 - 8*r^4 + 9*r^2 - 4))/(2*(r - g*r + g*r^3)*(g - g*r^2 + r^2 - 1)))
2*atan(((-(4*g + 2*(r + 1)^(3/2)*((r - 1)^3*(g - g*r^2 + r^2 + r^3 - 1)*(g*r^2 - g - r^2 + r^3 + 1))^(1/2) - 10*g*r^2 + 8*g*r^4 - 2*g*r^6 - 2*g^2 + 5*r^2 - 4*r^4 + 2*r^6 + 5*g^2*r^2 - 4*g^2*r^4 + g^2*r^6 - 2*g*(r + 1)^(3/2)*((r - 1)^3*(g - g*r^2 + r^2 + r^3 - 1)*(g*r^2 - g - r^2 + r^3 + 1))^(1/2) - 2)/(- g^2*r^6 + 2*g^2*r^4 - g^2*r^2 + 2*g*r^6 - 4*g*r^4 + 2*g*r^2 + 2*r^4 - r^2))^(3/2)*(r - g*r + g*r^3 - 2*r^3))/(2*(g - g*r^2 + r^2 - 1)) - ((-(4*g + 2*(r + 1)^(3/2)*((r - 1)^3*(g - g*r^2 + r^2 + r^3 - 1)*(g*r^2 - g - r^2 + r^3 + 1))^(1/2) - 10*g*r^2 + 8*g*r^4 - 2*g*r^6 - 2*g^2 + 5*r^2 - 4*r^4 + 2*r^6 + 5*g^2*r^2 - 4*g^2*r^4 + g^2*r^6 - 2*g*(r + 1)^(3/2)*((r - 1)^3*(g - g*r^2 + r^2 + r^3 - 1)*(g*r^2 - g - r^2 + r^3 + 1))^(1/2) - 2)/(- g^2*r^6 + 2*g^2*r^4 - g^2*r^2 + 2*g*r^6 - 4*g*r^4 + 2*g*r^2 + 2*r^4 - r^2))^(1/2)*(g^2*r^6 - 6*g^2*r^4 + 9*g^2*r^2 - 4*g^2 - 4*g*r^6 + 14*g*r^4 - 18*g*r^2 + 8*g + 4*r^6 - 8*r^4 + 9*r^2 - 4))/(2*(r - g*r + g*r^3)*(g - g*r^2 + r^2 - 1))) + 2*pi*k
pi/2 + 2*pi*k
2*pi*k - pi/2
>> sol.t2
ans =
z1
pi*k
pi*(2*m + 1)
2*pi*l - 2*atan((-(4*g - 2*(r + 1)^(3/2)*((r - 1)^3*(g - g*r^2 + r^2 + r^3 - 1)*(g*r^2 - g - r^2 + r^3 + 1))^(1/2) - 10*g*r^2 + 8*g*r^4 - 2*g*r^6 - 2*g^2 + 5*r^2 - 4*r^4 + 2*r^6 + 5*g^2*r^2 - 4*g^2*r^4 + g^2*r^6 + 2*g*(r + 1)^(3/2)*((r - 1)^3*(g - g*r^2 + r^2 + r^3 - 1)*(g*r^2 - g - r^2 + r^3 + 1))^(1/2) - 2)/(- g^2*r^6 + 2*g^2*r^4 - g^2*r^2 + 2*g*r^6 - 4*g*r^4 + 2*g*r^2 + 2*r^4 - r^2))^(1/2))
2*atan((-(4*g - 2*(r + 1)^(3/2)*((r - 1)^3*(g - g*r^2 + r^2 + r^3 - 1)*(g*r^2 - g - r^2 + r^3 + 1))^(1/2) - 10*g*r^2 + 8*g*r^4 - 2*g*r^6 - 2*g^2 + 5*r^2 - 4*r^4 + 2*r^6 + 5*g^2*r^2 - 4*g^2*r^4 + g^2*r^6 + 2*g*(r + 1)^(3/2)*((r - 1)^3*(g - g*r^2 + r^2 + r^3 - 1)*(g*r^2 - g - r^2 + r^3 + 1))^(1/2) - 2)/(- g^2*r^6 + 2*g^2*r^4 - g^2*r^2 + 2*g*r^6 - 4*g*r^4 + 2*g*r^2 + 2*r^4 - r^2))^(1/2)) + 2*pi*l
2*pi*l - 2*atan((-(4*g + 2*(r + 1)^(3/2)*((r - 1)^3*(g - g*r^2 + r^2 + r^3 - 1)*(g*r^2 - g - r^2 + r^3 + 1))^(1/2) - 10*g*r^2 + 8*g*r^4 - 2*g*r^6 - 2*g^2 + 5*r^2 - 4*r^4 + 2*r^6 + 5*g^2*r^2 - 4*g^2*r^4 + g^2*r^6 - 2*g*(r + 1)^(3/2)*((r - 1)^3*(g - g*r^2 + r^2 + r^3 - 1)*(g*r^2 - g - r^2 + r^3 + 1))^(1/2) - 2)/(- g^2*r^6 + 2*g^2*r^4 - g^2*r^2 + 2*g*r^6 - 4*g*r^4 + 2*g*r^2 + 2*r^4 - r^2))^(1/2))
2*atan((-(4*g + 2*(r + 1)^(3/2)*((r - 1)^3*(g - g*r^2 + r^2 + r^3 - 1)*(g*r^2 - g - r^2 + r^3 + 1))^(1/2) - 10*g*r^2 + 8*g*r^4 - 2*g*r^6 - 2*g^2 + 5*r^2 - 4*r^4 + 2*r^6 + 5*g^2*r^2 - 4*g^2*r^4 + g^2*r^6 - 2*g*(r + 1)^(3/2)*((r - 1)^3*(g - g*r^2 + r^2 + r^3 - 1)*(g*r^2 - g - r^2 + r^3 + 1))^(1/2) - 2)/(- g^2*r^6 + 2*g^2*r^4 - g^2*r^2 + 2*g*r^6 - 4*g*r^4 + 2*g*r^2 + 2*r^4 - r^2))^(1/2)) + 2*pi*l
2*pi*l - pi/2
So a rather nasty mess. You don't even want to look at the conditions under which those solutions exist.
  1 Comment
Dyuman Joshi
Dyuman Joshi on 25 Mar 2023
I think there should be assumptions on r and g as mentioned in the problem
syms r g th1 th2
assume(r>=0 & r<=1)
assume(g>=0 & g<=1)

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!