1
$\begingroup$

I have the problem of trying to get 2D plot from a 3D plot (which used NDSolve to solve the PDE), and I need the 3D plot's x-axis and y-axis to be the axes for my 2D plot while the z-axis of it to be the time for my 2D plot. I wrote the following code:

(*Step1. Define and plot the field*)
lhs = 24
rhs = 40
beta = 0
field = 1/(1 + Exp[-x + rhs]) - 1/(1 + Exp[-x + lhs]) + 1 + beta   (*our field value v.s. spatial dimension*) 
Plot[field, {x, 0, 64}, PlotRange -> {{0, 64}, {-1, 2}}]

(*Step.2 Define the field potential derivative dv/dphi*)
b = 0.1
dv = (y[t, x]^2 - 1)*(y[t, x] + b)       (*dv actually denotes dv/dphi*)

(*Step3. Define PDE from our E.O.M *)
pde = D[y[t, x], {t, 2}] - D[y[t, x], {x, 2}]+ dv == 0

(*Step4. Use NDSolve to solve PDE with the help of known initial condition - field value v.s. spatial coordinate at t0 *)
nsol = NDSolve[{pde, y[0, x] == field}, y[t, x], {t, 0, 10}, {x, 0, 64}]

(*Step5. plot the field in spatial dimension at different times*)
nsol2 = nsol[[1, 1, 2]]
Plot3D[nsol2, {t, 0.000, 0.001}, {x, 0, 64}, 
 PlotRange -> {-1.5, 1.5}]
Plot3D[nsol2, {t, 0.200, 0.201}, {x, 0, 64}, 
 PlotRange -> {-1.5, 1.5}]
Plot3D[nsol2, {t, 0.400, 0.401}, {x, 0, 64}, 
 PlotRange -> {-1.5, 1.5}]
Plot3D[nsol2, {t, 0.600, 0.601}, {x, 0, 64}, 
 PlotRange -> {-1.5, 1.5}]

what i get is enter image description here

where the horizontal axis(x-axis) denotes spatial coordinates, vertical axis(y-axis) denotes field value, inward axis(z-axis) denotes time.

What I need is to get an animation of 2D plot of y-axis versus x-axis over time, and I wrote the code to be 'Animate[Plot[nsol2, {x, 0, 64}, PlotRange -> {-1.5, 1.5}], {t, 0, 10}]' but it does not show any plot. Please let me know what could be the correct way to do this. Thank you for reading my post! my Mathematica

$\endgroup$
10
  • $\begingroup$ Mathematica v12.2 cann't solve the pde and gives message FindRoot::dfmin: The minimal damping factor of 1/10000 has been reached. and NDSolveValue::fempsf: PDESolve could not find a solution. What's your Mathematica version? $\endgroup$ Nov 11, 2021 at 10:28
  • $\begingroup$ My version is Mathematica 12.0.0.0.(under MacOS Big Sur) I got the same message of 'FindRoot::dfmin: The minimal damping factor of 1/10000 has been reached.' when i directly copy and run my code above, but I do not have 'NDSolveValue::fempsf: PDESolve could not find a solution.' If you have the same version you should be able to get 5 plots in total, and the last 4 plots after manual rotation are the images above $\endgroup$
    – jack
    Nov 11, 2021 at 11:07
  • $\begingroup$ Unfortunately not, because nsol has no value! $\endgroup$ Nov 11, 2021 at 11:14
  • $\begingroup$ May I ask how to deal with this error? I don't know why my Mathematica is able to produce graphical outcome with the codes above $\endgroup$
    – jack
    Nov 11, 2021 at 11:23
  • $\begingroup$ No idea. Can you reproduce your plots with a fresh kernel? $\endgroup$ Nov 11, 2021 at 11:35

2 Answers 2

0
$\begingroup$

Try

solution of pde:

Y = NDSolveValue[{pde, y[0, x] == field}, y, {t, 0,10}, {x, 0, 64}]

animation:


    Module[{}, Animate[Plot[Y[t, x], {x, 0,64},PlotRange -> {-1.5, 1.5}],{t, 0, 10} ]]
  

enter image description here

$\endgroup$
1
  • $\begingroup$ yes it works well! thanks! $\endgroup$
    – jack
    Nov 11, 2021 at 12:54
0
$\begingroup$

Maybe something like this

nsol3[t_, x_] = nsol2;
ListAnimate[ Table[Plot[nsol3[t, x], {x, 0, 64}, PlotLabel -> t, 
             PlotRange -> {-1.5, 1.5}], {t, 0, 10,0.1}]]

enter image description here

$\endgroup$
1
  • $\begingroup$ thanks i tested and this works well! $\endgroup$
    – jack
    Nov 11, 2021 at 12:54

Your Answer

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

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