(41C) Area of Triangle (SSS)

11102018, 12:20 PM
(This post was last modified: 11112018 12:18 PM by Gamo.)
Post: #1




(41C) Area of Triangle (SSS)
Program to calculate the "Area of Triangle" with "given 3 sides"
To find the Area this program used the Heron's formula. This program tell what kind of Triangle then give answer of the area. Type of Triangle. 1. Equilateral  All sides is the same lengths. 2. Scalene  All sides of different lengths. 3. Isosceles  Two sides of equal length. 4. Right Angle  One angle is a right angle. For A ≤ B ≤ C Example: Assign program to [X<>Y] [X<>Y] A 2 [R/S] B 3 [R/S] C 4 [R/S] > SCALENE [R/S] 2.9047 [X<>Y] A 8 [R/S] B 8 [R/S] C 3 [R/S] > ISOSCELES [R/S] 11.7872 [X<>Y] A 3 [R/S] B 4 [R/S] C 5 [R/S] > RIGHT ANGLE [R/S] 6 [X<>Y] A 5 [R/S] B 5 [R/S] C 5 [R/S] > EQUILATERAL [R/S] 10.8253  Program: Code:
Assign LBL SSS to [X<>Y] ASN ALPHA SSS ALPHA [X<>Y] Remark: For two sides of equal length (Isosceles) must start two same sides at A and B otherwise result will be (Scalene) Gamo 

11102018, 03:22 PM
Post: #2




RE: (41C) Area of Triangle (SSS)
(11102018 12:20 PM)Gamo Wrote: Program to calculate the "Area of Triangle" with "given 3 sides" If the sides are sorted, a >= b >= c, Kahan's formula is more accurate. Area Δ = 1/4 √((b+c+a)(ba+c)(ab+c)(bc+a)) Try Kahan's example: a,b,c = 100000, 99999.99979, 0.00029 True Area Δ ~ 9.9999999895 

11102018, 08:53 PM
(This post was last modified: 11102018 09:33 PM by Dieter.)
Post: #3




RE: (41C) Area of Triangle (SSS)
(11102018 12:20 PM)Gamo Wrote: This program tell what kind of Triangle then give answer of the area. Sorry, but this doesn't work. Try a=5, b=4 and c=3 and it returns "SCALENE". The three sides must be entered in a specific order. For instance, for rightangled triangles the largest side has to be c. Maybe you should add this in the instructions. Also the LBL 02 part can be removed – the test whether R01=R02 has already been done before. So you may remove that section and replace "GTO 02" with "GTO 03". (11102018 03:22 PM)Albert Chan Wrote: If the sides are sorted, a >= b >= c, Kahan's formula is more accurate. A sorted order also faciliates the tests for the different kinds of triangles.Here is a program that puts the largest side in R01 and the smallest in R03. It should work for any input order, and it also uses the Kahan formula. Code: 01 LBL "AREA" The program does its tests in the following order, assuming a ≥ b ≥ c. 1. Check if a=c. This means that a=b=c => equilateral triangle 2. Check if a=b or b=c. If true, it's a isosceles triangle Note: line 49...55 are a kind of poor man's "OR" test A conventional approach would even be slightly shorter, but it looks more fancy this way ;) 3. Check if a²=b²+c². In this case it's a rightangled triangle 4. If none of the tests was true it's a scalene triangle Examples: Code: [XEQ] "AREA" ENTER A↑B↑C Press backspace [←] to see the area. (11102018 03:22 PM)Albert Chan Wrote: Try Kahan's example: For this case the result with the above program is correctly rounded to 9,999999990. Well, almost – the true result is 9,9999 99989 49999 99... so that the tendigit value should better be rounded down to ...989. ;) Dieter 

11112018, 05:04 AM
Post: #4




RE: (41C) Area of Triangle (SSS)
Thanks to Dieter and Albert.
Even better accuracy. My program only work when A ≤ B < C A = B = C A ≠ B ≠ C Gamo 

11112018, 07:53 AM
(This post was last modified: 11112018 08:51 AM by Dieter.)
Post: #5




RE: (41C) Area of Triangle (SSS)
(11112018 05:04 AM)Gamo Wrote: My program only work when This means that your example A=8, B=8, C=3 would not work. In general, according to these rules no isosceles triangle can be entered where the two equal sides are larger than the third one. You can modify the program so that it will always work when A ≤ B ≤ C. Still the LBL 02 part should be removed. Take a look at the code: The program first checks if A=B... Code: RCL 01 ...and if true, it jumps to LBL 02 where the same test is done once again: Code: LBL 02 Since this always tests true again, the program always continues at LBL 03 (where the program checks if B and C are equal as well): Code: LBL 03 That's why "ISOSCELES" appears twice in the program. So you should remove the complete LBL 02 part and replace the "GTO 02" line with "GTO 03". BTW, in an HP41 program quotation marks are used for text. You you should not use them for commands, and vice versa. So instead of ISOSCELES "PROMPT" you better write "ISOSCELES" PROMPT Dieter 

11112018, 12:23 PM
(This post was last modified: 11112018 12:24 PM by Gamo.)
Post: #6




RE: (41C) Area of Triangle (SSS)
Dieter Thank You for the corrections.
I have test by deleted the LBL 02 and changed GTO 02 to GTO 03 so it work ok now. Gamo 

11112018, 04:25 PM
(This post was last modified: 11112018 06:08 PM by Dieter.)
Post: #7




RE: (41C) Area of Triangle (SSS)
(11112018 12:23 PM)Gamo Wrote: I have test by deleted the LBL 02 and changed GTO 02 to GTO 03 so it work ok now. It works exactly the same as before, just with less steps. ;) But what about the input order now? How do A, B and C have to be entered so that the program always returns correct results? For rightangled triangles C must be the largest side, while on the other hand for the isosceles example C is the smallest side. ?!? You should assume that the user does not know which kind of triangle enters. After all, that's what the program is supposed to display. So the only rule can be something like "enter sides in ascending order" (or similar). But this requires a modification of the program. Edit: here is a possible solution. Enter the three sides in ascending order, i.e. A≤B≤C. I tried to preserve as much of your original program as possible. The output routine has been changed, you may modify it according to your likings. The program now ends in one common endpoint for all cases at LBL 00 so that you can restart it with a simple [R/S]. Code: LBL "SSS" Dieter 

11122018, 01:32 AM
(This post was last modified: 10042022 04:48 PM by Albert Chan.)
Post: #8




RE: (41C) Area of Triangle (SSS)
I derived triangle area (SSS) formula from Laws of Cosine.
Assuming c is the shortest side, this is the formula: Let y = (c  (ab))*(c + (ab)) / 4, then Area Δ = √((aby)*y) Prove: c² = a² + b²  2ab cos(C) = (ab)² + 2ab*(1  cos(C)) = (ab)² + 4ab*sin(C/2)² let x = sin(C/2)², so 4abx = c²  (ab)² let y = abx, so y = (c  (ab))*(c + (ab)) / 4 sin(C) = √(1  cos(C)²) = √(1  (12x)²) = √(4x  4x²) = 2√(x  x²) Area Δ = ½ ab sin(C) = ab √(x  x²) = √((ab)(abx)  (abx)²) = √((aby)*y) Above also proved Heron's formula, since y=(sa)(sb): aby = ab  (s²  (a+b)s + ab) = s² + (2sc)s = s(sc) Area Δ = √((aby)*y) = √(s(sa)(sb)(sc)) Update Oct 4,2022: perhaps a simpler proof Area Δ = ½ ab sin(C) = y cot(C/2) = y √(csc(C/2)^21) = y √(ab/y1) = √((aby)*y) 

11122018, 02:55 PM
(This post was last modified: 11132018 03:49 AM by Albert Chan.)
Post: #9




RE: (41C) Area of Triangle (SSS)
(11102018 03:22 PM)Albert Chan Wrote: If the sides are sorted, a >= b >= c, Kahan's formula is more accurate. (11122018 01:32 AM)Albert Chan Wrote: Let y = (c  (ab))*(c + (ab)) / 4, then Area Δ = √((aby)*y) Tried all Kahan's example (Table 1) with above Y formula (SSS => area): (10, 10, 10) => 43.30127019 (100000, 99999.99979. 0.00029) => 9.9999 99989 (100000, 100000, 1.00005) => 52002.50002 (15000, 10000, 5000.000001) => 612.37 24357 (99999.99996, 99999.99994, 0.00003) => 1.1180 33988 (200000, 99999.99999, 99999.99999) => Error (99999.99996, 94721.35941, 5278.64055) => 0 (200004, 100002, 100002) => 0 (31622.77662, 31622.77661, 0.000023) => 0.3274 90458 (31622.77662, 31622.77661, 0.0155555) => 245.95 40000 Edit: this is the HP12C code for above tests (order does not matter): Example: 2 Enter 3 Enter 4 R/S => 2.904737510 Code: X≤Y 

11122018, 08:28 PM
Post: #10




RE: (41C) Area of Triangle (SSS)
(11122018 02:55 PM)Albert Chan Wrote:(11102018 03:22 PM)Albert Chan Wrote: If the sides are sorted, a >= b >= c, Kahan's formula is more accurate.(11122018 01:32 AM)Albert Chan Wrote: Let y = (c  (ab))*(c + (ab)) / 4, then Area Δ = √((aby)*y)... Both the formula and the program look good. But is there a proof that the formula is (at least) as exact as Kahan's? Testing some examples does not count. ;) Dieter 

11122018, 10:33 PM
(This post was last modified: 11122018 11:31 PM by Albert Chan.)
Post: #11




RE: (41C) Area of Triangle (SSS)
(11122018 08:28 PM)Dieter Wrote: Both the formula and the program look good. I had picked the more accurate version of Law of Cosine for a reason: c² = (ab)² + 4ab*sin(C/2)² If c is the shortest side, b/a > 50%. Thus, ab is exact. (even if exponents don't match, say a 2 decimals setup: 1.2  0.72 = 0.48) c²  (ab)² may suffered from subtraction cancellation. Thus, y = (c  (ab))*(c + (ab)) / 4 Bad errors occurred when triangle is needlelike, possibly even flattened. Since c and ab about the same size, for needlelike triangle, y is still relatively accurate. Area Δ = √((aby)*y), with two terms growing opposite way. So, slight errors in y tended to cancel each other somewhat (if y is big) ... Examples can't validate formula, but if carried out calculations by hand, precision is preserved. Example: (a,b,c) = (100000, 99999.99979, 0.00029) ab = 0.00021 (exact) y = 0.00008 * 0.00050 / 4 = 1e8 (exact) Area Δ = √((aby)*y) = √((99999 99979  1e8) * 1e8) ~ √(99.999 99979) = 9.9999 99989 

11162018, 03:33 AM
(This post was last modified: 12042019 03:05 PM by Albert Chan.)
Post: #12




RE: (41C) Area of Triangle (SSS)
Trivia: Area Δ = √((aby)*y), max(y/(ab)) = 1/4
Prove: gap = ab, y = (c + gap)*(c  gap)/4 dy/dc = 2c > 0, so max(y) when c is also maximize, thus c = b In other words, Δ is isosceles, maybe equilateral (a = b) Let k = a/b, thus 2 > k >= 1: gap = kb  b = b*(k1) y = (b + gap)*(b  gap) / 4 = k*(2k) b² / 4 y/(ab) = y/(kb²) = (2k)/4 > max(y/(ab)) = max(0+ to 1/4) = 1/4 Isosceles Δ, b=c: Area Δ = √((aby)*y) = ab/4 * √((2+k)*(2k)) Equilateral Δ, a=b=c, thus k=1: Area Δ = a²/4 * √3 ~ (√3/4) a² 

12042019, 03:03 PM
Post: #13




RE: (41C) Area of Triangle (SSS)
(11162018 03:33 AM)Albert Chan Wrote: Trivia: Area Δ = √((aby)*y), max(y/(ab)) = 1/4 Simpler proof, using Law of Cosine: c² = (ab)² + 4ab sin(C/2)² Angle A ≥ B ≥ C → max(C) = 60° → y/(ab) = sin(C/2)² ≤ ¼ (11122018 08:28 PM)Dieter Wrote: Both the formula and the program look good. This is Kahan's formula: Area Δ = ¼ √((a+(b+c)) (c(ab)) (c+(ab)) (a+(bc)) Note that 4y = (c(ab)) (c+(ab)) = middle 2 terms inside Kahan's √ All is needed is to show 4(aby) also as accurate as (a+(b+c)) (a+(bc)) With 0 < y/(ab) ≤ ¼, (aby) terms are too far apart to hit by catastrophic cancellation. 

« Next Oldest  Next Newest »

User(s) browsing this thread: 2 Guest(s)