Cosine rule program

Yes. Sin A/a = Sin B/b = Sin C/c, right? or a/Sin A = b/Sin B = c/Sin C

Okay, so now we can start to hammer some draft functions to solve for the three cases,

A-S-A
S-S-A
A-A-S

Given two angles, A and B, and the included side, c, how will we go about solving for the three unknowns, a, b and C?

Solving for C is simple enough since we know that the three angles add to 180 degrees or PI radians, so,

C = 180 degrees - (A + B)

where A and B are in degrees. In terms of radians,

C = pi - (A + B)

where A and B are in radians, which they are moving forwardā€¦

Now we can use the Law of Sines to find the missing sides.

a / sin(A) = b / sin(B) = c / sin(C)

Since c is the known side we set up two equations, one for a, and one for b.

a / sin(A) = c / sin(C)    # multiply both sides by sin(A)

a = c * sin(A) / sin(C)    # but C = pi - (A + B)

a = c * sin(A) / sin(pi - (A + B))    # now solve for a

b = c * sin(B) / sin(pi - (A + B))    # now solve for b
from math import pi

def solve_C(P, Q):
    return pi - (P + Q)

def solve_ASA(P, k, Q):
    return k * sin(P) / sin(solve_C(P,Q))

k is known side which is sandwiched between the two known angles.

Usage,

A = get_angle('A:)
B = get_angle('B')
c = get_side('c')

a = solve_ASA(A, c, B)
b = solve_ASA(B, c, A)
C = solve_C(A, B)

Recall that computed angles are in radians.

See if you can add to this for the remaining scenarios.

Hereā€™s what we have so far of a raw draft. Remember we are always looking for ways to reduce code so the big picture is easier to see. Because this usually means abstraction, it is important to keep the minute details in mind. Recall we started with a dozen methods earlier that were reduced to four.

from math import pi, sqrt, sin, asin, cos, acos, radians, degrees

def solve_C(P, Q):
    return pi - (P + Q)

def get_side(side):
    return float(input("What value is side %s? " % side))

def get_angle(angle):
    return radians(float(input("What value is angle %s? " % angle)))

def solve_SSS(a, b, u):
    return acos((a ** 2 + b ** 2 - u ** 2) / (2 * a * b))

def solve_SAS(a, K, c):
    return sqrt(a ** 2 + c ** 2 - 2 * a * c * cos(K))

def solve_ASA(P, k, Q):
    return k * sin(P) / sin(solve_C(P, Q))
b = get_side('b')
c = get_side('c')
A = get_angle('A')

print ("\nS-A-S problem.")
print ("\nGivens,\nSide b: %.1f\nSide c: %.1f\nAngle A: %.1f\n" \
       % (b, c, degrees(A)))

a = solve_SAS(b, A, c)
B = solve_SSS(a, c, b)
C = solve_SSS(a, b, c)

print ("To find,\nSide a: %.1f\nAngle B: %.1f\nAngle C: %.1f\n" \
       % (a, degrees(B), degrees(C)))
print (round(sum([degrees(A), degrees(B), degrees(C)]),1))
print ("\nS-S-S problem.")
print ("\nGivens,\nSide a: %.1f\nSide b: %.1f\nSide c: %.1f\n" \
       % (a, b, c))

A = solve_SSS(b, c, a)
B = solve_SSS(a, c, b)
C = solve_SSS(a, b, c)

print ("To find,\nAngle A: %.1f\nAngle B: %.1f\nAngle C: %.1f\n" \
       % (degrees(A), degrees(B), degrees(C)))
print (round(sum([degrees(A), degrees(B), degrees(C)]),1))
print ("\nA-S-A problem.")
print ("\nGivens,\nAngle A: %.1f\nSide c: %.1f\nAngle B: %.1f\n" \
       % (degrees(A), c, degrees(B)))

a = solve_ASA(A, c, B)
b = solve_ASA(B, c, A)
C = solve_C(A, B)

print ("To find,\nSide a: %.1f\nSide b: %.1f\nAngle C: %.1f\n" \
       % (a, b, degrees(C)))
print (round(sum([degrees(A), degrees(B), degrees(C)]),1))

Output

What value is side b? 11
What value is side c? 7
What value is angle A? 40
S-A-S problem.

Givens,
Side b: 11.0
Side c: 7.0
Angle A: 40.0

To find,
Side a: 7.2
Angle B: 101.4
Angle C: 38.6

180.0
S-S-S problem.

Givens,
Side a: 7.2
Side b: 11.0
Side c: 7.0

To find,
Angle A: 40.0
Angle B: 101.4
Angle C: 38.6

180.0
A-S-A problem.

Givens,
Angle A: 40.0
Side c: 7.0
Angle B: 101.4

To find,
Side a: 7.2
Side b: 11.0
Angle C: 38.6

180.0

We have but the three initial inputs, with the missing three computed. The examples draw from that set for desired knowns, then replace with computed values. That we end up with correct values is evidence that the math is on spec.

>>> A = get_angle('A')
What value is angle A? 30
>>> B = get_angle('B')
What value is angle B? 60
>>> c = get_side('c')
What value is side c? 2
>>> solve_ASA(A, c, B)
0.9999999999999999    # more exactly, 1
>>> solve_ASA(B, c, A)
1.7320508075688772    # square root of 3

Oh I see you have given all three possibilities. I understand thank you!

1 Like

We still have the A-A-S and S-S-A scenarios:

Given

A, B, a

or,

A, B, b

we see two angles, one side. How would we solve for the missing two sides?

Given,

a, b, A

or,

a, b, B

How will we solve for the missing side, and the missing angles (finding one is enough, as we know)?

I have no idea. But do we do this by congrency?

Do you mean, congruency? Thatā€™s a different concept relating to two triangles being similar or the same.

Similar triangles have the same angles, but not the same sides. One is a scale up or down of the other.

Congruent triangles have both the same angles and the same sides, regardless how they are oriented. We can rotate or flip them to match up to one another.

Unfortunately, it does not apply in this case. We are seeking the missing sides or angles in these problems. The Law of Sines will apply in both of the above scenarios, with just a little twist from our earlier A-S-A example. We wonā€™t be computing the missing angle in our equation, as before.

Letā€™s add one more method (function) to the heap.

def solve_AAS(P, Q, k):
    return k * sin(Q) / sin(P)

Usage

The angle opposite the side we are solving for is always the second argument.

Given that we know from the earlier example that angle A is 40, side b is 11, and angle B is ~ 101.4, letā€™s find a.

>>> A = get_angle("A")
What value is angle A? 40
>>> B = get_angle("B")
What value is angle B? 101.4
>>> b = 11
>>> a = solve_AAS(B, A, b)
>>> a
7.212967074422482

which agrees with our earlier computation. Letā€™s solve for b using this value to checkā€¦

>>> b = solve_AAS(A, B, a)
>>> b
11.0
>>> 

That leaves just S-S-A, which Iā€™ll leave with you for a time to see if you can work it out on your own.

For SSA we can also use the Sine rule if the angle A is with the side a

We can arbitrarily assign labels. The important concepts here are opposite and adjacent. In an S-S-A problem one of the sides is adjacent to the angle, the other will be oppositeā€¦ The unknown angle will be opposite that side. The first objective will be to find the other adjacent angle, opposite the other known side.

Given known sides arbitrarily named, a and b, and one angle, opposite of side a, A, we would first set out to find angle B.

 sin(B) / b = sin(A) / a  # multiply both sides by `b`

 sin(B) = b * sin(A) / a

so that,

 B = asin(b * sin(A) / a)

B will be in radians, so to represent it in degrees,

print "angle B = %.1f degrees" % degrees(B)

From here we have a number of options for solving c and C.

C = solve_C(A, B)

will give us the missing angle.

c / sin(C) = a / sin(A) = b / sin(B)    # Known angle A and side a
c / sin(C) = a / sin(A)                 # multiply by sin(C)
c = a * sin(C) / sin(A)                 # but C = pi - (A + B)
c = a * sin(solve_C(A, B)) / sin(A)     # since B is also known

Gives us c, in terms of A, B, and a.

Now we just need to write up the method to perform this task (and work out the wrinklesā€¦). There may be errors in the above.

Did you spot the error (or omission) in the above? The SSA problem comes with a gotchaā€¦ There are two (possibly) solutions.

B = asin(b * sin(A) / a)

and,

B = pi - asin(b * sin(A) / a)

Consider the following methodsā€¦

def solve_B(h, k, P):
    return asin(k / h * sin(P)), pi - asin(k / h * sin(P))

def solve_SSAs(h, P, Q):
    return h * sin(solve_C(P, Q)) / sin(P)
A = get_angle('A')
a = get_side('a')
b = get_side('b')

y = solve_B(a, b, A)
print ("angle B = %.1f degrees, %.1f degrees" % (degrees(y[0]), degrees(y[1])))
c1 = solve_SSAs(a, A, y[0])
c2 = solve_SSAs(a, A, y[1])
print ("side c  = %.1f units,    %.1f units" % (c1, c2))
What value is angle A? 40
What value is side a? 7.2
What value is side b? 11.0
angle B = 79.1 degrees, 100.9 degrees
side c  = 9.8 units,    7.1 units

This comes close to agreeing with the earlier expected results, but differs slightly because of rounding. Notice that angle B has two possible values, as does side c?

To get a handle on this scenario we will need to do some extra work to narrow down to the solution that fits the exact triangle in our problem. Will come back to this after youā€™ve had some time to explore the problem.

The ambiguous case of the Law of Sines

Triangle calculator - SSA

Ok. Take your time. It will take some time for me to comprehend this information, Thank you!

1 Like

Perhaps this might help in disseminating the aforementioned.

def solve_SAA(h, P, Q):
    return h * sin(solve_C(P, Q)) / sin(P)

def solve_SSA(h, k, P):
    return asin(k / h * sin(P)), pi - asin(k / h * sin(P))

non_included_angle = get_angle('non-included')
side_opposite_angle = get_side('opposing angle')
other_known_side = get_side('other known')

y = solve_SSA(side_opposite_angle, other_known_side, non_included_angle)
c1 = solve_SAA(side_opposite_angle, non_included_angle, y[0])
c2 = solve_SAA(side_opposite_angle, non_included_angle, y[1])
if not c2 < 0:
    print ("angle B = %.1f degrees, %.1f degrees" % (degrees(y[0]), degrees(y[1])))
    print ("side c  = %.1f units,    %.1f units" % (c1, c2))
else:
    print ("angle B = %.1f degrees" % (degrees(y[0])))
    print ("side c  = %.1f units" % (c1))

Methods for solving triangles primer - Replit (Expect exceptions, as apply)

What value is angle non-included?  40
What value is side opposing angle?  7.2
What value is side other known?  11
angle B = 79.1 degrees, 100.9 degrees
side c  = 9.8 units,    7.1 units
What value is angle non-included?  75
What value is side opposing angle?  5
What value is side other known?  4
angle B = 50.6 degrees
side c  = 4.2 units

Thanks. Sorry for my lateness. Thanks for your help. Also is there any sites that are useful when teaching python which are simple and easy to understand?. Thanks

Sorry, I cannot answer that. I learned Python here, and turn to online documentation when I need to know more.

Pick a problem, and set out to solve it, much like we have been doing in this thread. Solutions rarely fall out of thin air, and often turn up in failure. Success is usually preceded by many failed attempts. Thatā€™s why it should be expected that we will take many turns of the wheel.

Tackling a big problem means approaching it from all sides, one at a time. We chip away and slowly watch as the big picture comes into view. What we have above are some of the working parts of a yet to be conceived Triangle class, in their proving phase.

No matter what language one chooses, it all falls out in much the same way. Learn to program, and any language environment will soon become a good fit with study and practice.

Agreed. I too learnt a lot from this site and am grateful for your contribution and I too want to learn how to code as I know it will make my life easier and open more opportunities in the long run. I was just looking for some ā€œbeginners guideā€ as my school teacher isnā€™t that great and I think ill it will be far better if I learn solo from experts from this site but I want to know all the basics first.

There a site thatā€™s been around a long time and is often referred

learnpythonthehardway.org

Every teacher is the embodiment of apt pupils. We must never put it down to the teacher. We are responsible for our own learning, and must motivate ourselves to be the apt pupil in any case, regardless the subject.

I guess you are right. But my teacher doesnt even has a degree in programming. He is a cover teacher so I donā€™t expect much. But yeah other than that your right.

1 Like

But he/she does have a teacherā€™s degree. I have only a small degree of knowledge about programming and yet here we are.

When the problem motivates you, you get with it and learn whatever it takes to work it out. At first itā€™s a bore because of all the rudimentary concepts that make no sense, but eventually your tool chest starts to take on some weight and you reach another level of understanding and wherewithal.

A teacher, coach, mentor, tutor, whatever or whomever you look up to, can only get you so far. You are the one who takes the ball and goes with it.

Along with all of them, I too am on the sidelines, watching, cheering, applauding. Once you see your grandkids play hockey, it all means something.

Little successes add up, starting with this oneā€¦

from math import pi, sqrt, sin, asin, cos, acos, radians, degrees

class Shape(object):
  numsides = None
  def validate(self):
    return len(self.sides.keys()) <= self.num_sides

class Triangle(Shape):
  num_sides = 3
  def __init__(self):
    self.sides = {}
    self.angles = {}
      
  def get_side(self, side):
    self.sides[side] = float(input("What value is side %s? " % side))

  def get_angle(self, angle):
    self.angles[angle] = radians(float(input("What value is angle %s? " % angle)))

Console run

>>> t = Triangle()
>>> t.get_side('a')
What value is side a? 7.2
>>> t.get_side('b')
What value is side b? 11
>>> t.get_angle('A')
What value is angle A? 40
>>> t.sides
{'b': 11.0, 'a': 7.2}
>>> t.angles
{'A': 0.6981317007977318}
>>> t.num_sides
3
>>> t.validate()
True
>>> 

It is not a big accomplishment, I know, but itā€™s a start. So too is it with programming of any sort. We start with single steps and let them take us on our jounney.

Yes, I got it. Lets take the single steps.Is there a way to write on this topic. Sorry for my in activity.