0
$\begingroup$

I have a project to animate heat equation in 3D. I have the following problem

$$u_t=u_{xx},\qquad 0\leq x\leq 1 \land t>0$$ $$u(0,t)=u(1,t)=0$$ $$u(x,0)=\begin{cases} 2x, & 0\leq x<\frac{1}{2}\\ 2-2x, & \frac{1}{2}\leq x<1 \end{cases}$$

And then animate the comparison between exact solution and my manual solution within the time interval $0 \leq t \leq 2$. I try to solve using Mathematica with the command NDSolve, here's my code

sol = NDSolve[{D[u[x, t], t] == D[u[x, t], {x, 2}],
   u[0, t] == 0, u[1, t] == 0,
   u[x, 0] == Piecewise[{{2 x, 0 <= x <= 1/2}, {2 - 2 x, 1/2 <= x <= 1}}]}, 
   u, {t, 0, 2}, {x, 0, 1}]
u[x,t]/.sol;
Manipulate[Plot3D[u[x, t] /. sol, {x, 0, 1}, {t, 0, 2}, PlotRange -> All, PlotPoints -> 250, Mesh -> None], {t, 0, 2}]

But my Wolfram Mathematica seems heavily working, it even hardly change the plot3D image, when I change the control value in Manipulate. Is there a way to fix this? Or is there any alternative better command?

$\endgroup$
5
  • $\begingroup$ I'm sorry i have a trouble editing the equation it keeps getting me the "Your post appears to contain code that is not properly formatted" please help me edit it if you can $\endgroup$
    – Aji Wibowo
    May 30 at 3:24
  • $\begingroup$ You're using {t, 0, 2} for both Plot3D's range specification and Manipulate's range specification, and I'm pretty sure that's not what you're trying to animate here? Edit: Conceptually, once Plot3D uses its t range, there should be no more t values to Manipulate. $\endgroup$
    – eyorble
    May 30 at 3:28
  • $\begingroup$ @eyorble I change it to $u$, but still can't export it to .avi, well I can and it sometimes not responding but the result video is just the plot at t=0 not moving anywhere $\endgroup$
    – Aji Wibowo
    May 30 at 3:41
  • 1
    $\begingroup$ PlotPoints -> 250 is not your friend, nor the friend of speed. Manipulate (Animate just makes a specialized Manipulate) is designed for computations that can be updated quickly. If you need an initial 250 plot point grid to make a reasonable plot, you probably cannot do it quickly. But as @eyorble seems to hint, maybe you want Plot[u[x, t] /. sol, {x, 0, 1}] instead with t controlled by Manipulate? $\endgroup$
    – Michael E2
    May 30 at 4:04
  • $\begingroup$ The problem comes from the fact that the initial condition is not compatible with your ODE. The second derivative D[u[x, 0], {x, 2}] at t==1/2 is infinite. Try to give an initial condition that gives a finite second derivative. $\endgroup$ May 30 at 14:41

1 Answer 1

1
$\begingroup$

it even hardly change the plot3D image, when I change the control value in Manipulate

I made some changes to make it run much faster now.

enter image description here

ClearAll[u, x, t];
pde = D[u[x, t], t] == .1*D[u[x, t], {x, 2}];
bc = {u[0, t] == 0, u[1, t] == 0};
ic = u[x, 0] == Piecewise[{{2 x, 0 <= x <= 1/2}, {2 - 2 x, 1/2 <= x <= 1}}];
sol = NDSolveValue[{pde, ic, bc}, u, {t, 0, 2}, {x, 0, 1}];
Manipulate[
 Plot3D[sol[x, t], {x, 0, 1}, {t, 0, t0}, PerformanceGoal -> "Speed", 
  PlotRange -> {Automatic, {0, 2}, {-.1, 1}}, 
  AxesLabel -> {"x", "time", "u(x,t)"}, ImageSize -> 400],
 {{t0, 0.01, "time"}, .01, 2, .01, Appearance -> "Labeled"},
 TrackedSymbols :> {t0}
 ]

Removed PlotPoints -> 250 and added PerformanceGoal -> "Speed" and other minor cleanup.

$\endgroup$
3
  • $\begingroup$ Thank you so much, but i think what i need is just changing to that 'PerformanceGoal->"Speed"', only 1 thing still stumble me, how do I export the file so its smooth on the video ? Seems like the 'Export' doesnt render all the value in time $\endgroup$
    – Aji Wibowo
    May 30 at 9:12
  • $\begingroup$ You might try making a list of frames, then exporting that. (Also, ListAnimate might work better, because your frames are pre-rendered). $\endgroup$
    – Chris K
    May 30 at 15:50
  • $\begingroup$ You could precompute the graphic frames and use ListAnimate. ListAnimate will also let you specify the frames per second. reference.wolfram.com/language/ref/… $\endgroup$ Jun 2 at 1:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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