0
$\begingroup$

I am trying to nest some functions together so that I have one final equation I can call (this is herons formula for triangles), which utilizes those previous functions I created. I've done this before but I'm having some trouble here. Here is what I have so far:

 sum[a_, b_, c_] := (a + b + c)/2;
sideA[b_, c_, s_] := 2 s - b - c;
sideB[a_, c_, s_] := 2 s - c - a;
sideC[a_, b_, s_] := 2 s - a - b;
area[a_, b_, c_, s_] := Sqrt[sum[a, b, 
c]*(sum[a, b, c] - sideA[b, c, s])*(sum[a, b, c] - 
 sideB[a, c, s])*(sum[a, b, c] - sideC[a, b, s])];

N[area[2, 4, 4, 5]]
$\endgroup$
8
  • $\begingroup$ Find different names for your functions and their arguments, this should fix your issues... $\endgroup$
    – ktm
    Oct 18, 2016 at 20:14
  • $\begingroup$ you mean for a, b and c?? I tried using sideA, sideB, etc and no luck. But I will mess around with it. Thanks @user6014 $\endgroup$
    – Brandon
    Oct 18, 2016 at 20:18
  • $\begingroup$ Currently in your area function Mathematica thinks that the function names are the same variables as the function's input, i.e. 1,2,3,4, explaining the current output you get. $\endgroup$
    – ktm
    Oct 18, 2016 at 20:20
  • $\begingroup$ s, a, b, c are functions on the LHS of the equations. They are variables on the RHS. this is causing the problems. $\endgroup$
    – dan7geo
    Oct 18, 2016 at 20:21
  • $\begingroup$ @dan7geo I edited my code above, how does that look? $\endgroup$
    – Brandon
    Oct 18, 2016 at 20:24

1 Answer 1

3
$\begingroup$

I think this is what you want to do:

s[a_, b_, c_] := (a + b + c)/2;
area[a_, b_, c_] := Sqrt[s[a, b, c]*(s[a, b, c] - a)*(s[a, b, c] - b)*(s[a, b, c] - c)];

area[3, 4, 5]
(*6*)

Or all in one:

Clear[area]

area[a_, b_, c_] :=
  With[{s = (a + b + c)/2},
    Sqrt[ s*(s - a)*(s - b)*(s - c) ]
  ]

Or all in one line :)

area := Sqrt[Plus@##/2 Times @@ (Plus@##/2 - List@##)] &
$\endgroup$
4
  • $\begingroup$ Oh....Yeah I thought I might need all those sideA, sideB, sideC equations because how would Mathematica know the formulas for those values based off of just the sum equation? I guess it solves them somewhere...Thank you $\endgroup$
    – Brandon
    Oct 18, 2016 at 20:31
  • $\begingroup$ No upvotes/accept? :) $\endgroup$
    – dan7geo
    Oct 18, 2016 at 20:36
  • $\begingroup$ gotcha :P my fault! $\endgroup$
    – Brandon
    Oct 18, 2016 at 20:37
  • $\begingroup$ and yeah I have no clue why I had all those functions because side A would just be the length of "a" itself...duh. silly misunderstanding $\endgroup$
    – Brandon
    Oct 18, 2016 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.