MATLAB Answers

# solving equations with trigonometric functions

2 views (last 30 days)
Harshith Mahesh on 5 Apr 2021
Commented: Harshith Mahesh on 7 Apr 2021
Hi, I have been trying to solve the following equations for the variables theta3 and theta4. So I wrote the code as seen below.
syms x1 x2 x3 x4 x5 x6 theta2 theta3 theta4 Ad Dd
theta2 = 119.92
x1 = 135
x2 = 25
x5 = 120
x6 = 35
Ad = 110.49467458
Dd = 9.5869687
eqn1 = x1*cos(theta2) + x2*sin(theta2) + x5*cos(theta3) -x6*cos(theta4) - Ad == 0
eqn2 = x1*sin(theta2) -x2*cos(theta2) - x5*sin(theta3) -x6*sin(theta4) - Dd ==0
S1 = solve(eqn1,eqn2)
But my output looks like this
S1 =
theta3: [2×1 sym]
theta4: [2×1 sym]
Does anyone know how do I get the answer in degrees?
I'm fairly new to Matlab, so I'm sorry if this an extremely basic question.
Regards, Harshith
##### 4 CommentsShowHide 3 older comments
Bruno Luong on 6 Apr 2021
theta2 = 119.92
degree or radian?
Do you know sin/cos argument is radian?

Sign in to comment.

### Accepted Answer

John D'Errico on 6 Apr 2021
Note that x1, x2, x5, x6 are just NUMBERS. Defining them as syms in advance does nothing. MATLAB is a language with dynamic variable definitions. So if you do this:
syms x
x = 7;
whos x
Name Size Bytes Class Attributes x 1x1 8 double
then x is now a double precision number. You have achieved NOTHING with the syms call. x is not a symbolic version of the number 7.
Now let me look at your code. First, learn to use semi-colons at the end of your lines. That prevents MATLAB from dumping loads of crap to the command window.
syms theta2 theta3 theta4 Ad Dd
theta2 = 119.92;
x1 = 135;
x2 = 25;
x5 = 120;
x6 = 35;
Ad = 110.49467458;
Dd = 9.5869687;
eqn1 = x1*cos(theta2) + x2*sin(theta2) + x5*cos(theta3) -x6*cos(theta4) - Ad == 0;
eqn2 = x1*sin(theta2) -x2*cos(theta2) - x5*sin(theta3) -x6*sin(theta4) - Dd ==0;
So we have two equations in the two unknowns, theta3 & theta4.
Remember though, you used sin and cos. Trig functions in MATLAB use RADIANS, NOT degrees. So your solution will be in radians. If you want degrees, then you want to use the functions sind and cosd. So we really need to re-write your equations in terms of degrees. That seems clear because theta2 is 119.92, which must be a value in degrees, not radians.
eqn1 = x1*cosd(theta2) + x2*sind(theta2) + x5*cosd(theta3) -x6*cosd(theta4) - Ad == 0
eqn1 = eqn2 = x1*sind(theta2) -x2*cosd(theta2) - x5*sind(theta3) -x6*sind(theta4) - Dd ==0
eqn2 = Next, we can use solve. Will this problem have multiple solutions? Of course. We should be able to add integrer multiples of 2*pi radians (or 360 degrees) to any solution.
thetasol = solve(eqn1,eqn2,[theta3,theta4])
thetasol = struct with fields:
theta3: [2×1 sym] theta4: [2×1 sym]
thetasol.theta3
ans = thetasol.theta4
ans = Now you convert those solutions to floating point numbers using either vpa or double.
double(thetasol.theta3)
ans =
37.5143 -25.3087i 37.5143 +25.3087i
double(thetasol.theta4)
ans =
1.0e+02 * 1.4249 - 0.7047i 1.4249 + 0.7047i
What does that tell me? This says your problem has no solution in real numbers. So if you have the correct values for your problem, then you wrote your equations incorrectly, or vice-versa.
##### 1 CommentShowHide None
Harshith Mahesh on 7 Apr 2021
I see, thank you for your answer! I'm sorry about the basic errors, I just recently picked up Matlab.
I don't think the equations are a problem as I used the ones given in the paper that I had mentioned in an earlier comment. I believe there might be a problem with my values then. I got these values from the 3D CAD model I was working on. I'll just have to re-design a few parts.
Thank you so much for your help!

Sign in to comment.

### More Answers (2)

J Chen on 5 Apr 2021
format long % show more digits
double(S1.theta3)
double(S1.theta4)
format % back to the default format
##### 1 CommentShowHide None
John D'Errico on 6 Apr 2021
While this is correct as an answer, the problem itself has some major caveats that would need to be addressed. See my answer for a discussion of the problems.

Sign in to comment.

darova on 6 Apr 2021
Here is the similar example. Maybe you will be interested
• , , and length are given. angle is changing
• Calculate length and • Calculate vector
• Rotate by degree, make it of length See code attached
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!