1
$\begingroup$

I was trying to show the heat conduction of Laplace equation problem.But the plot does not show properly

a = 2; b = 2; imax = 20; jmax = 20; k = 1;
dx = a/imax; dy = b/jmax;

For [i = 0, i < imax + 1, i++, 
  For[j = 0, j < jmax + 1, j++, T[i, j] = 0]]

For[i = 0, i <= imax, i++, T[i, 0] = 0; T[i, jmax] = 1];
For[j = 0, j <= jmax, j++, T[0, j] = 0; T[imax, j] = 0 ];
For[k = 1, k < 10, k++, 
  For[i = 1, i < imax, i++, 
   For[j = 1, j < jmax, j++, 
    T[i, j] = 0.25*(T[i + 1, j] + T[i - 1, j] + T[i, j + 1] + T[i, j - 1]);]]]

It seems there is something wrong in following code:

nn = Flatten[Table[{i*dx, i*dy, T[i, j]}, {i, 1, imax - 1}, {j, 1, jmax - 1}] // 1];
ListContourPlot[nn, PlotLegends -> Automatic, PlotRange -> All, Contours -> 20]
$\endgroup$

closed as off-topic by m_goldberg, MarcoB, Carl Lange, José Antonio Díaz Navas, bbgodfrey Feb 18 at 21:51

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, MarcoB, Carl Lange, José Antonio Díaz Navas, bbgodfrey
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    $\begingroup$ What is the purpose of doing "//1" on nn ? I don't think it is doing whatever you were trying to do. $\endgroup$ – MinHsuan Peng Feb 13 at 17:28
  • $\begingroup$ i was trying define the dimensions,maybe its wrong $\endgroup$ – matrix Feb 13 at 18:35
1
$\begingroup$

For-loops have lots of problems including that they are harder to follow than Do-loops. I will use Do. I will also reduce the size of max and jmax and give them different values to make my example result simpler but more general.

a = 2; b = 2;
imax = 10; jmax = 6;
dx = a/imax; dy = b/jmax;
k = 5;

Clear @ T;
Do[T[i, j] = 0, {i, 0, imax}, {j, 0, jmax}];
Do[T[i, jmax] = 1, {i, 0, jmax}];
Do[
  Do[
    T[i, j] = 0.25*(T[i + 1, j] + T[i - 1, j] + T[i, j + 1] + T[i, j - 1]),
    {i, imax - 1}, {j, jmax - 1}],
  k]

For the common situation of making xy-pairs or xyz-triples, the more convenient function Catenate has been defined to replace Flatten with a 2nd argument.

nn = Catenate @ Table[{i dx, j dy, T[i, j]}, {i, imax - 1}, {j, jmax - 1}];
ListContourPlot[nn, PlotLegends -> Automatic, PlotRange -> All, Contours -> 12]

plot

Update

Adding this to address issue raised in comment below.

Catenate can be replace by Flatten[#, 1]& in the above expression for nn, or you can rewrite it as

nn = Flatten[Table[{i dx, j dy, T[i, j]}, {i, imax - 1}, {j, jmax - 1}], 1];
$\endgroup$
  • $\begingroup$ thank you, learned a lot, but could you help me to get the result by using Flatten function? $\endgroup$ – matrix Feb 14 at 0:19
  • $\begingroup$ @matrix. I have added an update that should help you. $\endgroup$ – m_goldberg Feb 14 at 1:51
  • $\begingroup$ Great, thanks a lot! $\endgroup$ – matrix Feb 14 at 2:34
  • $\begingroup$ @matrix. I'm glad you find my answer useful. Please consider accepting it. You can do that by clicking on the check mark that appears on the left of the answer below the down arrow. $\endgroup$ – m_goldberg Feb 14 at 4:20

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