Exempting imaginary numbers from the output of an expression using an if statement.
Show older comments
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)])
b = randi([length(v)])
c = randi([length(v)])
P = a + b + c
s = P/2
% how to s > a,b,c so that imaginary numbers do not happen
check1 = (s-a)
check2 = (s-b)
check3 = (s-c)
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
Accepted Answer
More Answers (0)
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!