Reputation: 85
In my math class we are in a Trigonometry unit, learning Law of Sines/Law of Cosines. I write programs that take known values from math problems and spit out answers when I am bored.
I am struggling to write an algorithm that will solve for Side/Angle/Side (SAS) problems. I am using this problem for reference (apologies for quality):
When worked out on paper, it equals roughly 10.7 units.
Here is my code so far:
import math
# Law of Cosines
# SAS
print("Cosines: SAS")
x = float(input("Known Side Length #1: "))
y = float(input("Known Side Length #2: "))
z = float(input("Known Angle Degrees: "))
a = ((x*x)+(y*y))-(2*x*y*math.radians(math.cos(z)))
b = math.sqrt(a)
print(str(b))
And here is the result when using the code:
Cosines: SAS
Known Side Length #1: 5
Known Side Length #2: 9
Known Angle Degrees: 95
10.23977763686627
Obviously, these answers are not the same. I separated a
and b
to try and discern where the problem was, and it seems to be in a
, as it gives a value that is not equal to c^2 on paper (I previously was printing the value of a
as well).
Is the code wrong, or did I work it out on paper wrong? If the code is wrong, any help is appreciated.
Upvotes: 0
Views: 221
Reputation: 85
Never mind everyone, I was dumb. I had math.cos
and math.radians
reversed. Thanks for checking it out though!
New code:
print("Cosines: SAS")
x = float(input("Known Side Length #1: "))
y = float(input("Known Side Length #2: "))
z = float(input("Known Angle Degrees: "))
a = ((x*x)+(y*y))-(2*x*y*math.cos(math.radians(z)))
b = math.sqrt(a)
print(str(b))
Upvotes: 2