Trigonometry

I am attempting to find the angle of a triangle based on the length of the sides. I have no knowledge of any angles of the triangle.

I am using the equation: b^2 = a^2 + c^2  - 2ac cos B

where ^ denotes power of
B is the angle I am trying to find
b is the length of the side opposite B
a and c are the lengths of th other two sides.

So, if I have the data; b = 5, c = 4, a = 6

25 = 16 + 36 - 2*4*6 cos B
=> 25 = 52 - 48 cos B
=> 48 cos B = 27
=> cos B = 27/48
=> cos B = 0.5625

Is there a method to find B on its own? i.e. inverse of cos B?

krispinAsked:
Who is Participating?
 
zzynxSoftware engineerCommented:
B = Math.acos(0.5625);
0
 
krispinAuthor Commented:
I thought that myself but it doesn't seem to work. I know the arc cosine should be the inverse of cosine, but java doesn't seem to calculate it correctly. For example:

double angle = 45;
double cos;
            
cos = Math.cos(angle);
            
angle = Math.acos(cos);
            
System.out.println(cos);
System.out.println(angle);


The println on angle at the end does not print out 45

0
 
CEHJCommented:
double B = Math.acos(a*a + b*b + c*c) / 2 * a * c);
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
CEHJCommented:
>>The println on angle at the end does not print out 45

The result is given in radians. You need Math.toDegrees
0
 
zzynxSoftware engineerCommented:
>> The result is given in radians. You need Math.toDegrees
Exactly.
From the docs:
acos returns the arc cosine of an angle, in the range of 0.0 through pi.
0
 
CEHJCommented:
Typo - missing bracket, should be

 double B = Math.acos((a*a + b*b + c*c) / 2 * a * c);
0
 
zzynxSoftware engineerCommented:
>> You need Math.toDegrees
But as the doc says:
The conversion from radians to degrees is generally inexact;
Users should not expect cos(toRadians(90.0)) to exactly equal 0.0.
0
 
CEHJCommented:
Another typo ;-)


 double B = Math.acos((a*a - b*b + c*c) / 2 * a * c);
0
 
zzynxSoftware engineerCommented:
System.out.println(Math.toDegrees(Math.acos(Math.cos(Math.toRadians(45.0)))));
returns
    45.0

So, your code should be:

double angle = 45;
double cos;
         
cos = Math.cos(Math.toRadians(angle));
angle = Math.acos(cos);
         
System.out.println(cos);
System.out.println(Math.toDegress(angle));
0
 
CEHJCommented:
Or

double Bdegrees = Math.toDegrees(Math.acos((a*a - b*b + c*c) / 2 * a * c));
0
 
krispinAuthor Commented:
>> cos = Math.cos(Math.toRadians(angle));

thanks. That's what I was missing
0
 
zzynxSoftware engineerCommented:
>> thanks. That's what I was missing
Yep ;°)
0
 
zzynxSoftware engineerCommented:
thanks
0
 
CEHJCommented:
:-)

>>thanks. That's what I was missing

Well not really ;-) My last comment shows you don't need to introduce any call to toRadians. You don't even need to call toDegrees if you don't want a degree result
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.