Group Abstract Group Abstract

Message Boards Message Boards

How does Mathematica's Table function iterate equations?

 n = 2;
 yv[i_] := Range[1/n, 1, 1/n];
 yc[j_] := Which[
   0 <= i <= 1, 0,
   1 < i <= n, 1/2 (yv[n][[i]] + yv[n][[i - 1]])];
 Table[yc[j], {i, 1, n}]
 test1 = Table[
N[1/(2 \[Pi]) (yv[i]/(yv[i]^2 - yc[j]^2))], {i, 1, n}, {j, 1, n}]
{1/2, 1}
{0, 3/4}
{{{0.31831, 0.159155}, {0.31831, 0.159155}}, {{-0.254648,
0.363783}, {-0.254648, 0.363783}}}{{0.31831, 0.159155}, {-0.254648, 0.363783}}

I'm an undergraduate aerospace engineer learning Mathematica and trying to program Prandtl's numerical lifting line theory. I'm having trouble trying to understand Mathematica's interation process and how it creates lists (in this case, why I get a list of lists of lists vs. a list of lists). I'm doing some iterations (observe the "test" functions) with an equation that depends on two variables: variable yv that depends on i, and variable yc that depends on j. Both of my yv and yc functions are producing the correct answers that I need (sorry if the code for them looks ugly; it's a learning process). Test2 produces the correct output that I need: a square n x n matrix that matches my hand-calculations. Bluntly speaking, why does test1 create a weird looking list? If I need to do an iteration of both i and j from 1 to n, it visually seems like that code should produce an answer that matches test2. Is there a way to make test1 match test2's answer? Also, why does my code for test2 even work when I'm only iterating i, not j, from 1 to n (how does yc get its values)?

I'd appreciate any tips, as well as any resources to better understand the iteration process/linear algebra in Mathematica (my code will require me to use a lot of iterations and linear algebra, so I better learn from my mistakes and prevent this in my future codes. As a side note, I'm also trying to also switch from Matlab to Mathematica in my CFD class so I'll need to really understand Mathematica's numerical computation process).

TL;DR: My test2 output is exactly what I need, but how did Mathematica compute yc without steps for j?

Thank you.
POSTED BY: Matt Bergman
7 months ago
"how did Mathematica compute yc without steps for j?"

yc[ j_ ] or yc[ j ] does not depend on the value of j in any way. It doesn't test the value of the argument. It doesn't use the value of the argument. It only tests and uses the value that i happens to have at the moment that yc[ j ] is evaluated.
POSTED BY: Bill Simpson
7 months ago
Hello Matt,
I can't quite make out what you are trying to do.

However, if you study this little example, if might help you understand how to do what you want and thus achieve your purpose.

My purpose here is to show you the order in which things occur, and why it is useful to work symbolically instead of numerically

Put each expression into its own cell and evaluate one cell at a time. When you have convinced yourself that you understand the result of evaluation, move to the next cell.
Clear variables:

define a function that will give you a square matrix
test[n_] := 
Table[1/(2 Pi) yv[i]/(yv[i]^2 - yc[i, j]^2), {i, 1, n}, {j, 1, n}]

check and see if it works

define yv
yv[var_] := Sin[var Pi/(1 + var)]

test it

try your square matrix

go ahead and define yc
yc[lb_, ub_] := Integrate[g[z], {z, lb Pi, ub Pi}]

test it

Try your square matrix

Make a direct assignment to g
g = Sin

test your square matrix
anExample = test[4]

Extract parts:

POSTED BY: W. Craig Carter
7 months ago
To iterate the various Nest* functions can be helpful:
POSTED BY: Peter Fleck
7 months ago