# solving equations with trigonometric functions

조회 수: 2(최근 30일)
Harshith Mahesh 2021년 4월 5일
댓글: Harshith Mahesh 2021년 4월 7일
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
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표시숨기기 이전 댓글 수: 3
Bruno Luong 2021년 4월 6일
theta2 = 119.92
Do you know sin/cos argument is radian?

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

### 채택된 답변

John D'Errico 2021년 4월 6일
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;
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표시숨기기 없음
Harshith Mahesh 2021년 4월 7일
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!

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

### 추가 답변(2개)

J Chen 2021년 4월 5일
double(S1.theta3)
double(S1.theta4)
format % back to the default format
##### 댓글 수: 1표시숨기기 없음
John D'Errico 2021년 4월 6일
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.

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

darova 2021년 4월 6일
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

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

### Community Treasure Hunt

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

Start Hunting!