2
$\begingroup$

I want Mathematica to solve enter image description here

(I used a picture since when I use mathjax code the editor here says I might be using wrongly formatted code...)

So far, I have

pde = D[f[x,t], t] - D[f[x,t], {x,2}] == 0
sol = DSolve[{pde, f[x,0] == Min[x , 1 - x], f[0,t] == 0, f[1,t] == 0}, f[x,t], {x,0,1},{t,0,Infinity}]

But I am not sure if it is taken in account that $x \in (0,1)$ and $t > 0$. Do I have to somehow use the Piecewise function as they do here?

My second question concerns plotting the solution. The output of the above code snippet is

This is a function of two variables with values in $\mathbb{R}$, so it should easily be plottable. But when I use

Plot3D[f[x, t] /. sol, {x, 0, 1}, {t, 0, 4}]

as suggested here, I just get an empty 3d box.

$\endgroup$
3
  • $\begingroup$ Also, when using DSolve, Why do you write f[x,t], {x,0,1},{t,0,Infinity}], instead of f[x,t], {x,t}] ? $\endgroup$
    – Nasser
    Commented Jul 29, 2019 at 13:16
  • $\begingroup$ @Nasser Because I want the solution only to be for $x \in (0,1)$ and $t > 0$. $\endgroup$ Commented Jul 29, 2019 at 13:17
  • 2
    $\begingroup$ You can add assumptions DSolve[..., Assumptions -> {t > 0, 0 < x < 1}] but it will not make any difference to the solution. It is analytical solution. You decide when running the solution, what range and time you can to look at. This is different from NDSolve. $\endgroup$
    – Nasser
    Commented Jul 29, 2019 at 13:54

1 Answer 1

3
$\begingroup$

You can try

ClearAll[f, x, t];
pde = D[f[x, t], t] - D[f[x, t], {x, 2}] == 0
bc  = {f[0, t] == 0, f[1, t] == 0};
ic  = {f[x, 0] == Min[x, 1 - x]};
sol = f[x, t] /. First@DSolve[{pde, ic, bc}, f[x, t], {x, t}];
sol = Activate[sol /. Infinity -> 20];

Manipulate[
 Quiet@Plot[sol /. t -> time, {x, 0, 1},PlotRange -> {Automatic, {0, .5}}],
 {{time, 0, "time"}, 0, .51, .01}
 ]

enter image description here

Update Here is 3D plot requested

Plot3D[sol, {t, 0, .5}, {x, 0, 1}, PlotRange -> All, 
 AxesLabel -> {"time", "space", "f(x,t)"}, BaseStyle -> 14]

PLotted for $0<x<1$ and up to $t=0.5$ seconds.

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ @ViktorGlombik if you mean 3D? Yes. But you can see that it decays to zero after 0.5 seconds. Will add 3D soon. I use LICEcap program to export animation from Mathematica to gif file. $\endgroup$
    – Nasser
    Commented Jul 29, 2019 at 14:04
  • $\begingroup$ What does First@DSolve do? $\endgroup$ Commented Jul 30, 2019 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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