# writing a function using multiple variables

I am fairly new to Mathematica and am trying to write a function for the equation:

xx = [x/(Sqrt[h^2 + d^2 + y^2]))*(Sqrt[h^2 + (d + yy)^2])


where h, d are known and x, y and yy are functions that give lists of numbers of equal list length. Is there a way to write a function that encompasses all the variables x, y, yy? I'm assuming it would start with

xx[x_, y_, yy_] := .........


or is the best method to create a loop?

• Welcome to Mathematica.SE! I formatted your code for you. See this page to learn how that is done. By the way, your brackets don't match. The first one should be a round parenthesis. You should check out the answers to this question and the links from this part of the documentation. – Verbeia Jan 29 '13 at 0:38

No loop construct is necessary. Assuming that h, and d are constants defined at toplevel, and that you have the functions x, y and yy defined, you could proceed as follows:

hsq = h^2

xlist = x[...]

ylist = y[...]

yylist = yy[...]

xx[x_, y_, yy_] := x/(Sqrt[hsq + d^2 + y^2])*(Sqrt[hsq + (d + yy)^2])

xxlist = MapThread[xx, {xlist, ylist, yylist}]


The MapThread function will pluck successive items from xlist, ylist, and yylist, pass them to xx and gather up the computed values in a list which it returns.

This isn't the most sophisticated way to solve your problem with Mathematica, but at this point of your Mathematica experience, it may be the quickest and easiest way for you to do the calculations you want to carry out.

Your expression for xx isn't syntactically correct. I made a guess about where the parentheses go. Another interpretation I could have made is

xx[x_, y_, yy_] := x/((Sqrt[hsq + d^2 + y^2])*(Sqrt[hsq + (d + yy)^2]))


Use whichever is correct.