Group Abstract Group Abstract

Message Boards Message Boards

0
|
9.6K Views
|
4 Replies
|
3 Total Likes
View groups...
Share
Share this post:

How can I animate the solution to the 2D heat equation?

Hi,

I am entirely new to Mathematica and have been given the task to animate the solution to the 2D heat equation with given initial and boundary conditions. It looks like I was able to solve it using NDSolve, but when I try to create an animation of it using Animate all I get is blank frames. Could someone please help me understand what I am doing wrong? Here goes my code as it is right now:

r = 1000;
v[t_] := r*t

k = .002;
solution = NDSolve[{D[u[x, y, t], t] == k*(D[u[x, y, t], x, x] + D[u[x, y, t], y, y]), 
u[x, y, 0] == 0, 
u[0, y, t] == v[t], 
u[9, y, t] == v[t], 
u[x, 0, t] == v[t], 
u[x, 9, t] == v[t]}, 
u[x, y, t], {x, 0, 9}, {y, 0, 9}, {t, 0, 43200}];

distribution = u[x, y, t] /. solution;

Animate[Table[DensityPlot[distribution[x, y, t], {x, 0, 9}, {y, 0, 9}, Mesh -> 9, ColorFunction -> "TemperatureMap", ColorFunctionScaling -> True]], {t, 0, 43200}, AnimationRunning -> False]

Thanks a lot!

4 Replies

Hi Henrik,

I forgot to mention, but I had changed my value of "r" to 0.001. That's how I was getting that temperature range. Your first solution was exactly what I needed. Thank you so much for your reply!

I'm looking forward to keep practicing and improving on Mathematica now. I've found this language to be fascinating since the first time I saw it being used, but also very challenging to get familiar and comfortable with sometimes.

Best regards,

Felipe.

Hi Felipe,

I am entirely new to Mathematica ...

but you were already quite on the right track! Only your definition of distribution and the Table-command inside Animate caused the problem. Try this:

ClearAll["Global`*"]

r = 1000;
v[t_] := r*t
k = .002;

solution = NDSolve[{D[u[x, y, t], t] == 
     k*(D[u[x, y, t], x, x] + D[u[x, y, t], y, y]), u[x, y, 0] == 0, 
    u[0, y, t] == v[t], u[9, y, t] == v[t], u[x, 0, t] == v[t], 
    u[x, 9, t] == v[t]}, 
   u[x, y, t], {x, 0, 9}, {y, 0, 9}, {t, 0, 43200}];

distribution[x_, y_, t_] = First[u[x, y, t] /. solution];

Animate[DensityPlot[distribution[x, y, t], {x, 0, 9}, {y, 0, 9}, Mesh -> 9, ColorFunction -> "TemperatureMap", 
  ColorFunctionScaling -> True], {t, 0, 43200}, AnimationRunning -> False]

Regards -- Henrik

POSTED BY: Henrik Schachner

Hi Henrik,

Thanks a lot for your reply! Your suggestions solved my problem, but then I got another one. I wanted all my frames to be in the same color scale. I think I was able to do that, but I haven't been able to find a way to get that reflected on my color bar. Could you help me figure out how to get my color bar to reflect the color scale of my frames? I wanted it to have blue associated to 0, red associated to 50, and all colors in between associated to all numbers between 0 and 50. This is my next syntax for Animate:

Animate[DensityPlot[distribution[x, y, t], {x, 0, 9}, {y, 0, 9}, Mesh -> 9, FrameLabel -> {"x (cm)", "y (cm)"}, ColorFunction ->(ColorData["TemperatureMap"][
  Rescale[#, {0, 50}, {0, 1}]] &), ColorFunctionScaling -> False, PlotLegends -> BarLegend[{"TemperatureMap", {0, 50}}, LegendMarkerSize -> 320, 
LegendLabel -> "T (\[Degree]C)"], PerformanceGoal -> "Quality"], {t, 0, 43200}, AnimationRunning -> False]

Hi Felipe,

you seem to assume a temperature range like {0, 50}, but this appears not to be the case:

{min, max} = MinMax @ Table[distribution[5, 5, t], {t, 0, 43200, 100}]
(*  Out :  {-6.039615579447638`, 4.027849921458779`*^7}  *)

so I can offer two options (using the above definition of {min, max}): Either your color bar shows the full range of possible temperatures:

Manipulate[
 DensityPlot[distribution[x, y, t], {x, 0, 9}, {y, 0, 9}, Mesh -> 9, 
  FrameLabel -> {"x (cm)", "y (cm)"}, 
  ColorFunction -> (ColorData["TemperatureMap"][
      Rescale[#, {min, max}, {0, 1}]] &), 
  ColorFunctionScaling -> False, 
  PlotLegends -> 
   BarLegend[{Automatic, {min, max}}, LegendMarkerSize -> 320, 
    LegendLabel -> "T (\[Degree]C)"], 
  PerformanceGoal -> "Quality"], {t, 0, 43200}]

or the color bar shows only the temperature in the plot at a time:

Manipulate[
 DensityPlot[distribution[x, y, t], {x, 0, 9}, {y, 0, 9}, Mesh -> 9, 
  FrameLabel -> {"x (cm)", "y (cm)"}, 
  ColorFunction -> "TemperatureMap", ColorFunctionScaling -> True, 
  PlotLegends -> 
   BarLegend[Automatic, LegendMarkerSize -> 320, 
    LegendLabel -> "T (\[Degree]C)"], 
  PerformanceGoal -> "Quality"], {t, 0, 43200}]

It depends on what you like better. Note that instead of Animate I used Manipulate which I find more flexible, e.g. after pressing that [+] button one can see the current value of your parameter.

Regards -- Henrik

POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard