Exempting imaginary numbers from the output of an expression using an if statement.

Coding Heron's formula: If triangle T has sides a,b,c, given that a semiperimeter is s = (a+b+c)/2 then the area of any triangle is .
I am working out the best way(s) to say "Matlab! if any of (s-a), (s-b), (s-c) is < 0, multiply inside the square root by a -1." This way I avoid imaginary numbers in the output and keep the geometry accurate. 3 things came to mind right away, in order of priority: use an if statement, a for loop, or something new entirely!
Here is the brainstorming-
v = [1:25];
a = randi([length(v)])
a = 6
b = randi([length(v)])
b = 9
c = randi([length(v)])
c = 6
P = a + b + c
P = 21
s = P/2
s = 10.5000
% how to s > a,b,c so that imaginary numbers do not happen
check1 = (s-a)
check1 = 4.5000
check2 = (s-b)
check2 = 1.5000
check3 = (s-c)
check3 = 4.5000
if sqrt(s.*(s-a).*(s-b).*(s-c))
check1 <0
check2 <0
check3 <0
AT = sqrt(s.*(-1.*((s-a).*(s-b).*(s-c))))
end

1 Comment

Upon further inspection, I noticed that I do not need to break them into cases, and can just refer to the part under the square root within the if statement like so:
AT = sqrt(s.*(s-a).*(s-b).*(s-c))
if s.*(s-a).*(s-b).*(s-c) < 0
AT = sqrt(s.*(-1.*((s-a).*(s-b).*(s-c))))
end
%The new issue is that sometimes I get back a triangle with an area of 0.
%hmm.. Ok - the 0 case is when a = 20, b = 24, and c = 4. Matlab does this
%math: sqrt(24.*(4).*(0).*(16)) = 0

Sign in to comment.

 Accepted Answer

You can take the absolute value of each difference:
AT = sqrt(s.*abs(s-a).*abs(s-b).*abs(s-c));

6 Comments

or take abs() of the whole expression inside sqrt()
AT = sqrt(abs(s.*(s-a).*(s-b).*(s-c)));
Yes of course! This would do the trick!
Curiously though, I'm talking to my brother about this - Heron's formula "appears" to have a blindspot. Given a triangle of lengths 24,20, and 4, results in an area of 0 using this equation. I'm looking into the maths of it online at any chance I get but thought I'd share that here. It is an interesting issue, but nothing to do with Matlab itself.
Thank you Chris/ Dyuman Joshi!
Area of 0 is correct for that case.
Start at origin. Go right 24. Now staying on that line go directly left 4. How far back is it to the origin? Answer is 20. You never went above or below y axis so area is 0.
In order for there to be a non-zero area, the distance back would have to exceed 20.
Because that triangle does not exist.
For a triangle, sum of two sides must be greater than that of the 3rd, for all combinations.
Here, 20+4 is not greater than 24.
And since that triangle does not exist, it's area is 0.
However, some mathematicians will argue that the constraint is greater than or equalto. Though in that case, the equality is considered to be a degenerate case where the area is zero, as all 3 points lie on a straight line.
P.S - You can tell that I am a member of the team strict equality (but I am not a mathematician).
Taking the absolute value of the radicand will lead to incorrect results if the inputs for the "sides" can't be the sides of an actual triangle. The much better approach would be to error check the inputs first using the criterion in @Dyuman Joshi's comment, and then use Heron's fomula only if the inputs define a valid triangle.
Also, there may be interest in this Wikipedia link.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Release

R2023b

Asked:

on 1 Dec 2023

Commented:

on 4 Dec 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!