MATLAB Answers

# Help on trigonometric equation solution. Thanks a lot!

1 view (last 30 days)
Jack Dong on 21 Jul 2021
Commented: Jack Dong on 22 Jul 2021
Hi Matlab Experts,
• I built the "forward script" to calculate R1, R2 from initial inputs: X = 0.9425; Z = sin(X) + 2 = 2.809; beta = 0.531 and results for R1, R2 are showed as below.
ans =
7.7733501156970570278303966915701
ans =
11.261695645672471499665334704332
• Now, I want to use calculated R1, R2 values to input to the "inverse script" for calculating backwards to initial values of X, Z, beta. Expectation is X = 0.9425; Z = sin(X)+2 = 2.809; beta = 0.531 as initial. But the returned beta value is 50.796485621 and even though this is true result in terms of trigonometry but it's not my wanted value (0.531). I think we should assign some conditions for beta, so that it can return the expected value 0.531 but I don't know how, plese help me. Thanks a lot!
ans =
0.94245504646393978078105437144795
ans =
2.8090036222785815735410394206806
ans =
50.796485621630240525665530786852
------------------------------------------------------------------------------------------
FORWARD SCRIPT TO CALCULATE R1, R2:
syms X Y Z alpha beta gama
deg2rad=pi/180;
theta_b=15*deg2rad;
theta_p=15*deg2rad;
R_b=1;
R_p=0.9;
Q0=1; %initial leg length
p=[X;Y;Z];
B1=[R_b*cos(15*deg2rad);R_b*sin(15*deg2rad);0];
B2=[R_b*cos(105*deg2rad);R_b*sin(105*deg2rad);0];
B3=[R_b*cos(135*deg2rad);R_b*sin(135*deg2rad);0];
B4=[R_b*cos(225*deg2rad);R_b*sin(225*deg2rad);0];
B5=[R_b*cos(255*deg2rad);R_b*sin(255*deg2rad);0];
B6=[R_b*cos(345*deg2rad);R_b*sin(345*deg2rad);0];
Bx1=B1(1,:);
Bx2=B2(1,:);
Bx3=B3(1,:);
Bx4=B4(1,:);
Bx5=B5(1,:);
Bx6=B6(1,:);
By1=B1(2,:);
By2=B2(2,:);
By3=B3(2,:);
By4=B4(2,:);
By5=B5(2,:);
By6=B6(2,:);
Bz1=B1(3,:);
Bz2=B2(3,:);
Bz3=B3(3,:);
Bz4=B4(3,:);
Bz5=B5(3,:);
Bz6=B6(3,:);
P1=[R_p*cos(75*deg2rad);R_b*sin(75*deg2rad);0];
P2=[R_p*cos(165*deg2rad);R_b*sin(165*deg2rad);0];
P3=[R_p*cos(195*deg2rad);R_b*sin(195*deg2rad);0];
P4=[R_p*cos(285*deg2rad);R_b*sin(285*deg2rad);0];
P5=[R_p*cos(315*deg2rad);R_b*sin(315*deg2rad);0];
P6=[R_p*cos(405*deg2rad);R_b*sin(405*deg2rad);0];
Px1=P1(1,:);
Px2=P2(1,:);
Px3=P3(1,:);
Px4=P4(1,:);
Px5=P5(1,:);
Px6=P6(1,:);
Py1=P1(2,:);
Py2=P2(2,:);
Py3=P3(2,:);
Py4=P4(2,:);
Py5=P5(2,:);
Py6=P6(2,:);
Pz1=P1(3,:);
Pz2=P2(3,:);
Pz3=P3(3,:);
Pz4=P4(3,:);
Pz5=P5(3,:);
Pz6=P6(3,:);
X=0.9425;
beta=0.531;
Y=0;
gama=0;
alpha=0;
Z=sin(X)+2;
R1=(X+((cos(gama)*cos(beta))*Px1)+(((cos(gama)*sin(beta)*sin(alpha))-(sin(gama)*cos(alpha)))*Py1)+(((cos(gama)*sin(beta)*cos(alpha))+(sin(gama)*sin(alpha)))*Pz1)-Bx1)^2+(Y+((sin(gama)*cos(beta))*Px1)+(((sin(gama)*sin(beta)*sin(alpha))+(cos(gama)*cos(alpha)))*Py1)+(((sin(gama)*sin(beta)*cos(alpha))-(cos(gama)*sin(alpha)))*Pz1)-By1)^2+(Z+((-sin(beta))*Px1)+((cos(beta)*sin(alpha))*Py1)+((cos(beta)*cos(alpha))*Pz1)-Bz1)^2;
L1=R1^0.5-Q0;
R2=(X+((cos(gama)*cos(beta))*Px2)+(((cos(gama)*sin(beta)*sin(alpha))-(sin(gama)*cos(alpha)))*Py2)+(((cos(gama)*sin(beta)*cos(alpha))+(sin(gama)*sin(alpha)))*Pz2)-Bx2)^2+(Y+((sin(gama)*cos(beta))*Px2)+(((sin(gama)*sin(beta)*sin(alpha))+(cos(gama)*cos(alpha)))*Py2)+(((sin(gama)*sin(beta)*cos(alpha))-(cos(gama)*sin(alpha)))*Pz2)-By2)^2+(Z+((-sin(beta))*Px2)+((cos(beta)*sin(alpha))*Py2)+((cos(beta)*cos(alpha))*Pz2)-Bz2)^2;
L2=R2^0.5-Q0;
vpa(R1.')
vpa(R2.')
INVERSE SCRIPT
syms X Y Z alpha beta gama
deg2rad=pi/180;
theta_b=15*deg2rad;
theta_p=15*deg2rad;
R_b=1;
R_p=0.9;
Q0=1; %initial leg length
p=[X;Y;Z];
B1=[R_b*cos(15*deg2rad);R_b*sin(15*deg2rad);0];
B2=[R_b*cos(105*deg2rad);R_b*sin(105*deg2rad);0];
B3=[R_b*cos(135*deg2rad);R_b*sin(135*deg2rad);0];
B4=[R_b*cos(225*deg2rad);R_b*sin(225*deg2rad);0];
B5=[R_b*cos(255*deg2rad);R_b*sin(255*deg2rad);0];
B6=[R_b*cos(345*deg2rad);R_b*sin(345*deg2rad);0];
Bx1=B1(1,:);
Bx2=B2(1,:);
Bx3=B3(1,:);
Bx4=B4(1,:);
Bx5=B5(1,:);
Bx6=B6(1,:);
By1=B1(2,:);
By2=B2(2,:);
By3=B3(2,:);
By4=B4(2,:);
By5=B5(2,:);
By6=B6(2,:);
Bz1=B1(3,:);
Bz2=B2(3,:);
Bz3=B3(3,:);
Bz4=B4(3,:);
Bz5=B5(3,:);
Bz6=B6(3,:);
P1=[R_p*cos(75*deg2rad);R_b*sin(75*deg2rad);0];
P2=[R_p*cos(165*deg2rad);R_b*sin(165*deg2rad);0];
P3=[R_p*cos(195*deg2rad);R_b*sin(195*deg2rad);0];
P4=[R_p*cos(285*deg2rad);R_b*sin(285*deg2rad);0];
P5=[R_p*cos(315*deg2rad);R_b*sin(315*deg2rad);0];
P6=[R_p*cos(405*deg2rad);R_b*sin(405*deg2rad);0];
Px1=P1(1,:);
Px2=P2(1,:);
Px3=P3(1,:);
Px4=P4(1,:);
Px5=P5(1,:);
Px6=P6(1,:);
Py1=P1(2,:);
Py2=P2(2,:);
Py3=P3(2,:);
Py4=P4(2,:);
Py5=P5(2,:);
Py6=P6(2,:);
Pz1=P1(3,:);
Pz2=P2(3,:);
Pz3=P3(3,:);
Pz4=P4(3,:);
Pz5=P5(3,:);
Pz6=P6(3,:);
Y=0;
gama=0;
alpha=0;
R1=(X+((cos(gama)*cos(beta))*Px1)+(((cos(gama)*sin(beta)*sin(alpha))-(sin(gama)*cos(alpha)))*Py1)+(((cos(gama)*sin(beta)*cos(alpha))+(sin(gama)*sin(alpha)))*Pz1)-Bx1)^2+(Y+((sin(gama)*cos(beta))*Px1)+(((sin(gama)*sin(beta)*sin(alpha))+(cos(gama)*cos(alpha)))*Py1)+(((sin(gama)*sin(beta)*cos(alpha))-(cos(gama)*sin(alpha)))*Pz1)-By1)^2+(Z+((-sin(beta))*Px1)+((cos(beta)*sin(alpha))*Py1)+((cos(beta)*cos(alpha))*Pz1)-Bz1)^2;
L1=R1^0.5-Q0;
R2=(X+((cos(gama)*cos(beta))*Px2)+(((cos(gama)*sin(beta)*sin(alpha))-(sin(gama)*cos(alpha)))*Py2)+(((cos(gama)*sin(beta)*cos(alpha))+(sin(gama)*sin(alpha)))*Pz2)-Bx2)^2+(Y+((sin(gama)*cos(beta))*Px2)+(((sin(gama)*sin(beta)*sin(alpha))+(cos(gama)*cos(alpha)))*Py2)+(((sin(gama)*sin(beta)*cos(alpha))-(cos(gama)*sin(alpha)))*Pz2)-By2)^2+(Z+((-sin(beta))*Px2)+((cos(beta)*sin(alpha))*Py2)+((cos(beta)*cos(alpha))*Pz2)-Bz2)^2;
L2=R2^0.5-Q0;
A=vpasolve(R1==7.7731884,R2==11.2615,Z==sin(X)+2);
vpa(A.X')
vpa(A.Z')
vpa(A.beta')
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

David Goodmanson on 21 Jul 2021
Edited: David Goodmanson on 21 Jul 2021
Hi Jack,
Fortunately the solver is coming up with the correct angle, except for the 2*pi ambiguity. So, after the solver does its job you can use
mod(50.796485621,2*pi)
ans = 0.5310
##### 1 CommentShowHide None
Jack Dong on 22 Jul 2021
Thanks David for your great answer but can we embed it into my script so that after running, I get the correct beta immediately. Thanks!
I tried this but failed!
vpa(A.X')
vpa(A.Z')
vpa(A.beta')
beta1=mod(A.beta,2*pi)
-----> Result is
ans =
0.94245504646393978078105437144795
ans =
2.8090036222785815735410394206806
ans =
50.796485621630240525665530786852
beta1 =
mod(50.796485621630240525665530786852, 2*pi)
------------------------------------------------------
Script
syms X Y Z alpha beta gama
deg2rad=pi/180;
theta_b=15*deg2rad;
theta_p=15*deg2rad;
R_b=1;
R_p=0.9;
Q0=1; %initial leg length
p=[X;Y;Z];
B1=[R_b*cos(15*deg2rad);R_b*sin(15*deg2rad);0];
B2=[R_b*cos(105*deg2rad);R_b*sin(105*deg2rad);0];
B3=[R_b*cos(135*deg2rad);R_b*sin(135*deg2rad);0];
B4=[R_b*cos(225*deg2rad);R_b*sin(225*deg2rad);0];
B5=[R_b*cos(255*deg2rad);R_b*sin(255*deg2rad);0];
B6=[R_b*cos(345*deg2rad);R_b*sin(345*deg2rad);0];
Bx1=B1(1,:);
Bx2=B2(1,:);
Bx3=B3(1,:);
Bx4=B4(1,:);
Bx5=B5(1,:);
Bx6=B6(1,:);
By1=B1(2,:);
By2=B2(2,:);
By3=B3(2,:);
By4=B4(2,:);
By5=B5(2,:);
By6=B6(2,:);
Bz1=B1(3,:);
Bz2=B2(3,:);
Bz3=B3(3,:);
Bz4=B4(3,:);
Bz5=B5(3,:);
Bz6=B6(3,:);
P1=[R_p*cos(75*deg2rad);R_b*sin(75*deg2rad);0];
P2=[R_p*cos(165*deg2rad);R_b*sin(165*deg2rad);0];
P3=[R_p*cos(195*deg2rad);R_b*sin(195*deg2rad);0];
P4=[R_p*cos(285*deg2rad);R_b*sin(285*deg2rad);0];
P5=[R_p*cos(315*deg2rad);R_b*sin(315*deg2rad);0];
P6=[R_p*cos(405*deg2rad);R_b*sin(405*deg2rad);0];
Px1=P1(1,:);
Px2=P2(1,:);
Px3=P3(1,:);
Px4=P4(1,:);
Px5=P5(1,:);
Px6=P6(1,:);
Py1=P1(2,:);
Py2=P2(2,:);
Py3=P3(2,:);
Py4=P4(2,:);
Py5=P5(2,:);
Py6=P6(2,:);
Pz1=P1(3,:);
Pz2=P2(3,:);
Pz3=P3(3,:);
Pz4=P4(3,:);
Pz5=P5(3,:);
Pz6=P6(3,:);
Y=0;
gama=0;
alpha=0;
R1=(X+((cos(gama)*cos(beta))*Px1)+(((cos(gama)*sin(beta)*sin(alpha))-(sin(gama)*cos(alpha)))*Py1)+(((cos(gama)*sin(beta)*cos(alpha))+(sin(gama)*sin(alpha)))*Pz1)-Bx1)^2+(Y+((sin(gama)*cos(beta))*Px1)+(((sin(gama)*sin(beta)*sin(alpha))+(cos(gama)*cos(alpha)))*Py1)+(((sin(gama)*sin(beta)*cos(alpha))-(cos(gama)*sin(alpha)))*Pz1)-By1)^2+(Z+((-sin(beta))*Px1)+((cos(beta)*sin(alpha))*Py1)+((cos(beta)*cos(alpha))*Pz1)-Bz1)^2;
L1=R1^0.5-Q0;
R2=(X+((cos(gama)*cos(beta))*Px2)+(((cos(gama)*sin(beta)*sin(alpha))-(sin(gama)*cos(alpha)))*Py2)+(((cos(gama)*sin(beta)*cos(alpha))+(sin(gama)*sin(alpha)))*Pz2)-Bx2)^2+(Y+((sin(gama)*cos(beta))*Px2)+(((sin(gama)*sin(beta)*sin(alpha))+(cos(gama)*cos(alpha)))*Py2)+(((sin(gama)*sin(beta)*cos(alpha))-(cos(gama)*sin(alpha)))*Pz2)-By2)^2+(Z+((-sin(beta))*Px2)+((cos(beta)*sin(alpha))*Py2)+((cos(beta)*cos(alpha))*Pz2)-Bz2)^2;
L2=R2^0.5-Q0;
A=vpasolve(R1==7.7731884,R2==11.2615,Z==sin(X)+2);
vpa(A.X')
vpa(A.Z')
vpa(A.beta')
beta1=mod(A.beta,2*pi)

Sign in to comment.

R2019b

### Community Treasure Hunt

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

Start Hunting!