0
|
2940 Views
|
6 Replies
|
0 Total Likes
View groups...
Share
GROUPS:

# Defining a function with a sum

Posted 10 years ago
 I am a new user of mathematica and I am struggling to code the following. I am given a positive integer $s$ and a series of points $P_n \in R^3$ with $n= 1, ..., s$ . I want to define the function $U$ from $R^3 \setminus {P_n}$ to $R$ given by : $U= \sum_{n=1}^s \frac{1}{|x-P_n|}$. After that, I can define a metric given by $g = \frac{1}{U}(d\tau +\phi\sin?d?)^2 +U(dr^2 +r^2(d?^2 +sin?^2d?^2)) U$. Defining the metric should be ok but I am stuck with how to define $U$. I read some tutorials but that didn't help much ... thanks for any advice !
6 Replies
Sort By:
Posted 10 years ago
 Both definitions take the same arguments. The first argument is the list of the cartesian coordinates of your point x. The second argument is a list of points, each of which is a list of its three cartesian coordinates. From your question (and what you said in your original post) I gather that you are very new to Mathematica. Given this, I'd recommend that you spend some time learning its syntax and programming methods before diving in to a project like this. This is a very fast introduction for folks who have a reasonable amount of programming experience in other languages:http://www.wolfram.com/language/fast-introduction-for-programmers/but it is certainly not sufficient to get up to speed. A good way to do that is to read and work though the following recent book:http://www.amazon.com/Programming-Mathematica-Introduction-Paul-Wellin/dp/1107009464/An additional comment about your project. In your specification you use the expression $|x-P_i|$. In this I assumed that all points were expressed in 3-dimensional cartesian coordinates and that your norm was the euclidian norm. But this is not consistent with the metric that you are designing which is generally non-euclidian and expressed in (for the spatial part) spherical polar coordinates. So what I gave you may not be what you actually want. But once you learn some more programming in Mathematica then you will be able to use the constructs that I gave as a starting point for creating the correct expressions for your project.
Posted 10 years ago
 I gave an example where you have two points {a,b,c} and {d,e,f} in symbolic form so you could see the action of the function.I mentioned that I make use of pure functions and the short form for Map. Here is the documentation for Pure functions:http://reference.wolfram.com/language/ref/Function.htmland here is the documentation for Map:http://reference.wolfram.com/language/ref/Map.htmlHere is a different, roughly equivalent, approach: uFunction[x : {_, _, _}, points : {{_, _, _} ..}] := Module[{nPoints}, nPoints = Length[points]; Sum[1/(Norm[x - points[[i]]]), {i, 1, nPoints}] ] (it differs from the original one in that it ends up having explicit absolute value functions in the result because Norm makes no assumptions as to whether the vector supplied to it has real components).
Posted 10 years ago
 Hi, Thanks again ! Just to be sure, in your second definition of $uFunction$, i'd have to put in the coordinates of the points in the brackets right ?
Posted 10 years ago
 Note that I make use of Pure functions to make the code compact as well as the Map function in its shorthand form.
Posted 10 years ago
 I assume that your x is a triple (i.e., a 3-vector) and that each of your points are themselves triples. If this is the case then your function (which I will call uFunction -- you shold avoid giving functions single capital letter names in Mathematica) could be uFunction[x : {_, _, _}, points : {{_, _, _} ..}] := Tr[1/(Sqrt[Tr[(x - #)^2]] & /@ points)] Then try it out: In[40]:= uFunction[{x, y, z}, {{a, b, c}, {d, e, f}}] Out[40]= 1/Sqrt[(-a + x)^2 + (-b + y)^2 + (-c + z)^2] + 1/Sqrt[(-d + x)^2 + (-e + y)^2 + (-f + z)^2] 
Posted 10 years ago
 Thanks for the answer ! I just don't understand why you define {a,b,c} and also {d,e,f}? Also, what does & /@ mean (in the definition of ufunction)? Thanks again