Mathematica Animate function question

In summary, you are using an animate function that has data dependencies that you are not specifying correctly. You should instead define a function outside of Animate that is based on the entire dataset and use that inside your animate function.
  • #1
Munin
4
0
Hi

Im doing a 2 dimensional heat spreading simulation.
I've created a matrix with 3 indices with the first index being for time step and the two other for element coordinates.


Code:
height = 20;
width = 4;
a = 0.5;
J = Round[height/a];
L = Round[width/a];
h = 0.1;
roomT = 20;
T = 90;
t = 1000;
Dev = Normal[ConstantArray[1, {t, J, L}]];
u = T*Normal[ConstantArray[1, {J, L}]];

J*L

v = Normal[ConstantArray[0, {J*L, 1} ]];
A = Normal[ConstantArray[0, {J*L, J*L}]];


u[[All, 1]] = roomT;
u[[All, L]] = roomT;
u[[1, All]] = roomT;
u[[J, All]] = roomT;

For[j = 1, j <= J, j++,
  For[l = 1, l <= L, l++,
    index = (j - 1)*L + l;
    v[[index, 1]] = u[[j, l]];
    ];
  ];

For[i = 1, i <= J*L, i++,
 
 A[[i, i]] = -4;
 
 If[i + L <= J*L, A[[i, i + L]] = 1, A[[i, i]] = A[[i, i]] + 1];
 
 If[i - L >= 1, A[[i, i - L]] = 1, A[[i, i]] = A[[i, i]] + 1];
 
 If[i + 1 <= J*L, A[[i, i + 1]] = 1, A[[i, i]] = A[[i, i]] + 1];
 
 If[i - 1 >= 1, A[[i, i - 1]] = 1, A[[i, i]] = A[[i, i]] + 1];
 ]
A = A/a^2;


Ett = SparseArray[{i_, i_} -> 1, {J*L, J*L}];
Mtemp = Ett - h*A;
M = Inverse[Mtemp];

For[i = 1, i <= L, i++,
  M[[i, All]] = 0;
  M[[(J - 1)*L + i, All]] = 0;
  M[[i, i]] = 1;
  M[[(J - 1)*L + i, (J - 1)*L + i]] = 1;
  
  ];
For[i = 1, i <= J, i++,
  M[[1 + L*(i - 1), All]] = 0;
  M[[1 + L*(i - 1), 1 + L*(i - 1)]] = 1;
  M[[L*i, All]] = 0;
  M[[L*i, L*i]] = 1;
  
  ];
k = 1;

While[k < t + 1,
  For[j = 1, j <= J, j++,
   For[l = 1, l <= L, l++,
     index = (j - 1)*L + l;
     u[[j, l]] = v[[index, 1]];
     ];
   ];
  Dev[[k, All, All]] = u;
  If[u[[Round[J/2], Round[L/2]]] < 21,
   Break[]
   ];
  v = M.v;
  k++;
  ];
t = k;
Print[t]

The simulation works fine but when I try to run an animate function over the time steps it only plots a density plot in the first time step. Is this the correct way to do an animate funtion for my density plot?

Code:
Animate[ListDensityPlot[Dev[[m, All, All]], 
  ColorFunction -> (RGBColor[1, 1 - #, 0] &)], {m, 1, t}, 
 AnimationRunning -> False]

As it is now the color in the color function is relative to the highest value for the time step. Is there a way to get the colors relative to the highest value for all steps?
 
Physics news on Phys.org
  • #2
Ok, I fixed the animate problem with this.
Code:
Animate[ListDensityPlot[Dev[[m, All, All]], 
  ColorFunction -> (ColorData["TemperatureMap"]), 
  ColorFunctionScaling -> True], {m, 1, t, 1}, 
 AnimationRunning -> False]

But I still can't get the colors to scale correctly, any advice on how to do this would be very helpful.
 
  • #3
Define a function outside your Animate that is based on your entire dataset that would map any value to the color you desire and then use that inside your animate as an argument to your ColorFunction.

If you are defining your argument to ColorFunction the way you wrote it inside your Animate then it is only dependent on the available data for that frame of the Animate and that was exactly what you observed, the color function changes as the Animate frame changes.
 

Related to Mathematica Animate function question

1. What is the syntax for using the Animate function in Mathematica?

The Animate function in Mathematica has the following syntax:
Animate[expr,{u, umin, umax, du}]
where expr is the expression or plot to be animated, u is the variable being animated, umin and umax are the minimum and maximum values of u, and du is the step size for u.

2. Can I animate multiple expressions or plots using the Animate function?

Yes, you can use the Animate function to animate multiple expressions or plots by using the ListAnimate function. The syntax is:
ListAnimate[{expr1, expr2, ...}, {u, umin, umax, du}]
This will create a slideshow of the different expressions or plots.

3. How can I customize the appearance and animation of my plot using the Animate function?

The Animate function has several options that allow you to customize the appearance and animation of your plot. Some of these options include AnimationDirection, AnimationRate, and AnimationRepetitions. You can also use the Manipulate function to have more control over the animation and appearance of your plot.

4. Can I export an animated plot created with the Animate function?

Yes, you can export your animated plot as a GIF, video, or other file format using the Export function. You can also use the Manipulate function to create an interactive animation that can be exported as a CDF file.

5. Are there any limitations to using the Animate function in Mathematica?

The Animate function has some limitations, such as not being able to animate 3D graphics or manipulate the animation in real-time. However, there are workarounds for these limitations, such as using the Manipulate function or creating a series of 2D plots to simulate a 3D animation.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Replies
0
Views
2K
  • Linear and Abstract Algebra
Replies
7
Views
1K
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
124
  • Introductory Physics Homework Help
Replies
10
Views
951
Replies
1
Views
861
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top