2 views (last 30 days)

Show older comments

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

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

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

eqn2 = x1*sind(theta2) -x2*cosd(theta2) - x5*sind(theta3) -x6*sind(theta4) - Dd ==0

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.theta3

thetasol.theta4

Now you convert those solutions to floating point numbers using either vpa or double.

double(thetasol.theta3)

double(thetasol.theta4)

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.

J Chen
on 5 Apr 2021

format long % show more digits

double(S1.theta3)

double(S1.theta4)

format % back to the default format

John D'Errico
on 6 Apr 2021

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

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

Start Hunting!