Mathematica Mathematica - Evaluating a function for different pairs of variables

AI Thread Summary
To evaluate a function f(x,y) for multiple pairs of variables in Mathematica, users can create two lists of values for x and y using the Table function. Instead of using Outer, which was found to be less effective, a nested Table function can be employed: Table[f(a,b), {a,x}, {b,y}], resulting in a comprehensive evaluation of 100 combinations. To extract only the real parts of the evaluations, the Re function can be applied within the Table command. Additionally, DeleteCases can be utilized to filter out any complex results after the evaluations. This approach efficiently handles larger datasets without manual calculations.
jimmy1066
Messages
3
Reaction score
0
I have a function f(x,y) that I wish to evaluate for different values of x and y.

I created two lists for x and y using table:

x = Table[x,{x,1/10,1,1/10}]
y = Table[y,{y,1/100,1/10,1/100}]

This gives me 10 values for x and 10 for y.

Now I want to evaluate my function f(x,y) for each point f(x1,y1), f(x1,y2),...,f(x2,y1) etc. This should give me 100 evaluations if I have 10 values for x and y. I've tried to evaluate by just substituting in x and y, but all this gives is 10 values corresponding to f(x1,y1), f(x2,y2), f(x3,y3),...,f(x10,y10).

Obviously I could do this by hand, but that would become a problem if I were to use say 100 values for x and for y, because that's 10000 points.

My second problem is that f(x,y) can be complex for some pairs of (x,y) and for others it has only a real part. Once I have mathematica evaluating the function correctly how do I make it output only the real evaluations?

Apologies for not using Latex. Every time I try to put in f(x,y) it just gives me f(x,y)
 
Physics news on Phys.org
For your first question perhaps Outer[f,list1,list2] might be interesting, documented here

http://reference.wolfram.com/mathematica/ref/Outer.html

or in your help browser.

For your second question perhaps DeleteCases might be interesting, documented here

http://reference.wolfram.com/mathematica/ref/DeleteCases.html

or in your help browser. Perhaps this example can give you some ideas

In[1]:= DeleteCases[{3,Pi,x,1+I}, _Complex]
Out[1]= {3,Pi,x}

And there is no need to apologize, at least not to me, for not desktoppublishingeverything
 
Thanks for the help. Unfortunately Outer was not much use to me. Instead I made another table:
Table[f(a,b),{a,x},{b,y}] (using the tables of x and y defined in my previous post) and this appears to have solved my problem.

DeleteCases worked perfectly once I had flattened the table.

Thanks again :)
 
jimmy1066 said:
Unfortunately Outer was not much use to me. Instead I made another table:
Table[f(a,b),{a,x},{b,y}] (using the tables of x and y defined in my previous post)
That is exactly what Outer does.
 
Just do the following:
Code:
Values=Table[Re[f[x[[i]],y[[j]]],{i,10},{j,10}];
This will generate a table of 100 values, one for each pair of x and y. The for example, if you want the value of f for a particular combination, it would just be Values[[3,7]].

Re[x] will take the real part of x.
 
Back
Top