(41C) Area of Triangle (SSS)
|
11-10-2018, 12:20 PM
(This post was last modified: 11-11-2018 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 |
|||
11-10-2018, 03:22 PM
Post: #2
|
|||
|
|||
RE: (41C) Area of Triangle (SSS)
(11-10-2018 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)(b-a+c)(a-b+c)(b-c+a)) Try Kahan's example: a,b,c = 100000, 99999.99979, 0.00029 True Area Δ ~ 9.9999999895 |
|||
11-10-2018, 08:53 PM
(This post was last modified: 11-10-2018 09:33 PM by Dieter.)
Post: #3
|
|||
|
|||
RE: (41C) Area of Triangle (SSS)
(11-10-2018 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 right-angled 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". (11-10-2018 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 right-angled 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. (11-10-2018 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 ten-digit value should better be rounded down to ...989. ;-) Dieter |
|||
11-11-2018, 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 |
|||
11-11-2018, 07:53 AM
(This post was last modified: 11-11-2018 08:51 AM by Dieter.)
Post: #5
|
|||
|
|||
RE: (41C) Area of Triangle (SSS)
(11-11-2018 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 HP-41 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 |
|||
11-11-2018, 12:23 PM
(This post was last modified: 11-11-2018 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 |
|||
11-11-2018, 04:25 PM
(This post was last modified: 11-11-2018 06:08 PM by Dieter.)
Post: #7
|
|||
|
|||
RE: (41C) Area of Triangle (SSS)
(11-11-2018 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 right-angled 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 |
|||
11-12-2018, 01:32 AM
(This post was last modified: 10-04-2022 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 - (a-b))*(c + (a-b)) / 4, then Area Δ = √((ab-y)*y) Prove: c² = a² + b² - 2ab cos(C) = (a-b)² + 2ab*(1 - cos(C)) = (a-b)² + 4ab*sin(C/2)² let x = sin(C/2)², so 4abx = c² - (a-b)² let y = abx, so y = (c - (a-b))*(c + (a-b)) / 4 sin(C) = √(1 - cos(C)²) = √(1 - (1-2x)²) = √(4x - 4x²) = 2√(x - x²) Area Δ = ½ ab sin(C) = ab √(x - x²) = √((ab)(abx) - (abx)²) = √((ab-y)*y) Above also proved Heron's formula, since y=(s-a)(s-b): ab-y = ab - (s² - (a+b)s + ab) = -s² + (2s-c)s = s(s-c) Area Δ = √((ab-y)*y) = √(s(s-a)(s-b)(s-c)) Update Oct 4,2022: perhaps a simpler proof Area Δ = ½ ab sin(C) = y cot(C/2) = y √(csc(C/2)^2-1) = y √(ab/y-1) = √((ab-y)*y) |
|||
11-12-2018, 02:55 PM
(This post was last modified: 11-13-2018 03:49 AM by Albert Chan.)
Post: #9
|
|||
|
|||
RE: (41C) Area of Triangle (SSS)
(11-10-2018 03:22 PM)Albert Chan Wrote: If the sides are sorted, a >= b >= c, Kahan's formula is more accurate. (11-12-2018 01:32 AM)Albert Chan Wrote: Let y = (c - (a-b))*(c + (a-b)) / 4, then Area Δ = √((ab-y)*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 HP-12C code for above tests (order does not matter): Example: 2 Enter 3 Enter 4 R/S => 2.904737510 Code: X≤Y |
|||
11-12-2018, 08:28 PM
Post: #10
|
|||
|
|||
RE: (41C) Area of Triangle (SSS)
(11-12-2018 02:55 PM)Albert Chan Wrote:(11-10-2018 03:22 PM)Albert Chan Wrote: If the sides are sorted, a >= b >= c, Kahan's formula is more accurate.(11-12-2018 01:32 AM)Albert Chan Wrote: Let y = (c - (a-b))*(c + (a-b)) / 4, then Area Δ = √((ab-y)*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 |
|||
11-12-2018, 10:33 PM
(This post was last modified: 11-12-2018 11:31 PM by Albert Chan.)
Post: #11
|
|||
|
|||
RE: (41C) Area of Triangle (SSS)
(11-12-2018 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² = (a-b)² + 4ab*sin(C/2)² If c is the shortest side, b/a > 50%. Thus, a-b is exact. (even if exponents don't match, say a 2 decimals setup: 1.2 - 0.72 = 0.48) c² - (a-b)² may suffered from subtraction cancellation. Thus, y = (c - (a-b))*(c + (a-b)) / 4 Bad errors occurred when triangle is needle-like, possibly even flattened. Since c and a-b about the same size, for needle-like triangle, y is still relatively accurate. Area Δ = √((ab-y)*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) a-b = 0.00021 (exact) y = 0.00008 * 0.00050 / 4 = 1e-8 (exact) Area Δ = √((ab-y)*y) = √((99999 99979 - 1e-8) * 1e-8) ~ √(99.999 99979) = 9.9999 99989 |
|||
11-16-2018, 03:33 AM
(This post was last modified: 12-04-2019 03:05 PM by Albert Chan.)
Post: #12
|
|||
|
|||
RE: (41C) Area of Triangle (SSS)
Trivia: Area Δ = √((ab-y)*y), max(y/(ab)) = 1/4
Prove: gap = a-b, 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*(k-1) y = (b + gap)*(b - gap) / 4 = k*(2-k) b² / 4 y/(ab) = y/(kb²) = (2-k)/4 --> max(y/(ab)) = max(0+ to 1/4) = 1/4 Isosceles Δ, b=c: Area Δ = √((ab-y)*y) = ab/4 * √((2+k)*(2-k)) Equilateral Δ, a=b=c, thus k=1: Area Δ = a²/4 * √3 ~ (√3/4) a² |
|||
12-04-2019, 03:03 PM
Post: #13
|
|||
|
|||
RE: (41C) Area of Triangle (SSS)
(11-16-2018 03:33 AM)Albert Chan Wrote: Trivia: Area Δ = √((ab-y)*y), max(y/(ab)) = 1/4 Simpler proof, using Law of Cosine: c² = (a-b)² + 4ab sin(C/2)² Angle A ≥ B ≥ C → max(C) = 60° → y/(ab) = sin(C/2)² ≤ ¼ (11-12-2018 08:28 PM)Dieter Wrote: Both the formula and the program look good. This is Kahan's formula: Area Δ = ¼ √((a+(b+c)) (c-(a-b)) (c+(a-b)) (a+(b-c)) Note that 4y = (c-(a-b)) (c+(a-b)) = middle 2 terms inside Kahan's √ All is needed is to show 4(ab-y) also as accurate as (a+(b+c)) (a+(b-c)) With 0 < y/(ab) ≤ ¼, (ab-y) terms are too far apart to hit by catastrophic cancellation. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 5 Guest(s)