1
$\begingroup$

By the advise of Jagra, I removed the first set of the code and leave the actual problem. I got a working script, but the plot is empty. I tried to run it in chrome and firefox, but to no difference.

What is the reason when plots are "empty" and no error is given?

(* Define parameters *)
L = 10;                 (* Length of the domain *)
T = 15;                 (* Total time *)
dx = 0.08;              (* Spatial step size *)
dt = 0.001;             (* Time step size *)
xValues = Range[-L/2, L/2, dx]; (* Spatial grid *)
mMax = Round[T/dt];     (* Maximum time index *)

(* Ma-soliton initial condition *)
A = 1; (* Amplitude *)
c = 0.5; (* Velocity *)
k = 1; (* Wave number *)
\[Psi]0[x_, t_] := A Sech[x - c t] Exp[I (k x - (c^2 + k^2) t)];

(* Initialize wave function *)
\[Psi] = Table[\[Psi]0[x, 0], {x, xValues}];

(* Time evolution function *)
timeEvolution[\[Psi]_, m_] := Module[{\[Psi]New, nonlinearTerm},
    \[Psi]New = \[Psi];  (* Start with the current wave function *)
    (* Update wave function using FDM *)
    For[i = 2, i < Length[\[Psi]] - 1, i++,
        nonlinearTerm = Abs[\[Psi][[i]]]^2 \[Psi][[i]];
        \[Psi]New[[i]] = \[Psi][[i]] - (dt/(2 I)) ((\[Psi][[i + 1]] - 2 \[Psi][[i]] + \[Psi][[i - 1]])/(dx^2)) - dt nonlinearTerm;
    ];
    \[Psi]New
];

(* Store results for animation *)
results = {};
(* Time-stepping loop *)
For[m = 1, m <= mMax, m++,
    \[Psi] = timeEvolution[\[Psi], m];
    If[Mod[m, Round[mMax/5]] == 0, AppendTo[results, \[Psi]]];  (* Store every 5th wave function *)
];

(* Create the animation *)
ListAnimate[Table[
  ListLinePlot[results[[m]], PlotRange -> All, 
               PlotLabel -> "Ma-Soliton Evolution", 
               AxesLabel -> {"x", "\[Psi](x,t)"}, 
               PlotStyle -> {Thick, Blue}, 
               ImageSize -> Large,
               PlotLegends -> {"t = " <> ToString[m dt]}],
  {m, 1, Length[results]}
]]
$\endgroup$
3
  • $\begingroup$ Consider replacing the For[] loops with a more functional construct. $\endgroup$ Commented Oct 9, 2024 at 15:20
  • $\begingroup$ The code supplied appears to have an undefined function: HyperbolicSech[] $\endgroup$ Commented Oct 9, 2024 at 15:25
  • $\begingroup$ Unless you think it adds anything, I suggest that you remove your first set of code. $\endgroup$ Commented Oct 9, 2024 at 23:02

2 Answers 2

4
$\begingroup$

The first solution below addressed an earlier state of the OP's original.

The second, provides I think a slightly clearer way to do what the OP, now wants that they didn't previously describe.

First solution

(*Define parameters*)
L = 10;                 (*Length of the domain*)
T = 15;                 (*Total time*)
dx = 0.08;              (*Spatial step size*)
dt = 0.001;             (*Time step size*)
xValues = Range[-L/2, L/2, dx]; (*Spatial grid*)
mMax = Round[T/dt];       (*Maximum time index*)

(*Ma-soliton initial condition*)
A = 1; (*Amplitude*)
c = 0.5; (*Velocity*)
k = 1; (*Wave number*)
\[Psi]0[x_, t_] := A*Sech[x - c t]*Exp[I *(k x - (c^2 + k^2)* t)];

(*Initialize wave function*)
\[Psi] = Table[\[Psi]0[x, 0], {x, xValues}];

(*Time evolution function*)
timeEvolution[\[Psi]_, m_] := Module[{\[Psi]New, nonlinearTerm},
   \[Psi]New = \[Psi];(*Start with the current wave \
function*)(*Update wave function using FDM*)
   For[i = 2, i < Length[\[Psi]] - 1, i++,
    nonlinearTerm = Abs[\[Psi][[i]]]^2*\[Psi][[i]];
    \[Psi]New[[i]] = \[Psi][[i]] - (dt/(2 I)) ((\[Psi][[i + 1]] - 
           2 \[Psi][[i]] + \[Psi][[i - 1]])/(dx^2)) - 
      dt *nonlinearTerm;];
   \[Psi]New];

(*Store results for animation*)
results = {};
(*Time-stepping loop*)
For[m = 1, m <= mMax, m++, \[Psi] = timeEvolution[\[Psi], m];
  If[Mod[m, Round[mMax/5]] == 0, 
   AppendTo[results, \[Psi]]];  (*Store every 5th wave function*)];

(*Create the animation*)
ListAnimate[
 Table[
  ComplexListPlot[results[[m]],
   PlotRange -> All,
   PlotLabel -> "Ma-Soliton Evolution",
   AxesLabel -> {"x", "\[Psi](x,t)"},
   PlotStyle -> {Thick, Blue},
   ImageSize -> Large,
   PlotLegends -> {"t = " <> ToString[m dt]}], {m, 1, 
   Length[results]}]]

enter image description here

Your results has complex numbers and would therefore need something like ComplexListPlot.

I also suggest, using Times explicitly rather than proximity.

Second solution

This solution places the calculation results in a Module which generates the real values of Psi. This will make it easier to refactor this code in the future, perhaps replacing the For loop with a functional solution.

Now having the list of reals, you can remove the Re[] from ListLinePlot. This separates calculations from plotting making it easier to refactor (improve) code going forward.

(*Define parameters*)L = 10;                 (*Length of the domain*)
T = 15;                 (*Total time*)
dx = 0.08;              (*Spatial step size*)
dt = 0.001;             (*Time step size*)
xValues = Range[-L/2, L/2, dx]; (*Spatial grid*)
mMax = Round[T/dt];     (*Maximum time index*)

(*Ma-soliton initial condition*)
A = 1; (*Amplitude*)
c = 0.5; (*Velocity*)
k = 1; (*Wave number*)
\[Psi]0[x_, t_] := A Sech[x - c t] Exp[I (k x - (c^2 + k^2) t)];

(*Initialize wave function*)
\[Psi] = Table[\[Psi]0[x, 0], {x, xValues}];

(*Time evolution function*)
timeEvolution[\[Psi]_, m_] := 
  Module[{\[Psi]New, 
    nonlinearTerm}, \[Psi]New = \[Psi];(*Start with the current wave \
function*)(*Update wave function using FDM*)
   For[i = 2, i < Length[\[Psi]] - 1, i++, 
    nonlinearTerm = Abs[\[Psi][[i]]]^2 \[Psi][[i]];
    \[Psi]New[[i]] = \[Psi][[i]] - (dt/(2 I)) ((\[Psi][[i + 1]] - 
           2 \[Psi][[i]] + \[Psi][[i - 1]])/(dx^2)) - 
      dt nonlinearTerm;];
   \[Psi]New];

(*Store results for animation*)
results = Module[{r},
   r = {};
   (*Time-stepping loop*)
   For[m = 1, m <= mMax, m++, \[Psi] = timeEvolution[\[Psi], m];
    If[Mod[m, Round[mMax/5]] == 0, 
     AppendTo[r, \[Psi]]];  (*Store every 5th wave function*)];
   Re[results]];

(*Create the animation for the real part of the wave function*)
ListAnimate[
 Table[ListLinePlot[results[[m]], PlotRange -> All, 
   PlotLabel -> "Ma-Soliton Evolution (Real Part)", 
   AxesLabel -> {"x", "Re[\[Psi](x,t)]"}, PlotStyle -> {Thick, Blue}, 
   ImageSize -> Large, PlotLegends -> {"t = " <> ToString[m dt]}], {m,
    1, Length[results]}]]

enter image description here

$\endgroup$
6
  • $\begingroup$ We shouldn't comment that out, because that is the initial condition. $\endgroup$ Commented Oct 9, 2024 at 16:52
  • $\begingroup$ Your original code hasn't provided a definition of A HyperbolicSech[]. As such it doesn't seem to make sense in your function \[Psi]0[x_] :=, which looks like it might more properly read as, \[Psi]0[x_] :=(*A HyperbolicSech[x]*) Exp[I k x] + perturbationAmplitude Sin[2 Pi x/5] $\endgroup$ Commented Oct 9, 2024 at 18:52
  • $\begingroup$ I realized you intend A to multiple your HyperbolicSech[]. $\endgroup$ Commented Oct 9, 2024 at 23:01
  • $\begingroup$ It should plot the real part of psi. $\endgroup$ Commented Oct 10, 2024 at 15:08
  • 1
    $\begingroup$ Actually, as you have now described it, you want to plot the real part of your list results. Your original post used an undefined function HyperbolicSech[]. Your revision attempted to plot complex numbers. Hard to help when what you need keeps changing. $\endgroup$ Commented Oct 10, 2024 at 15:47
0
$\begingroup$

Here is what I got fixed, now it looks correct:

(* Define parameters *)
L = 10;                 (* Length of the domain *)
T = 15;                 (* Total time *)
dx = 0.08;              (* Spatial step size *)
dt = 0.001;             (* Time step size *)
xValues = Range[-L/2, L/2, dx]; (* Spatial grid *)
mMax = Round[T/dt];     (* Maximum time index *)

(* Ma-soliton initial condition *)
A = 1; (* Amplitude *)
c = 0.5; (* Velocity *)
k = 1; (* Wave number *)
\[Psi]0[x_, t_] := A Sech[x - c t] Exp[I (k x - (c^2 + k^2) t)];

(* Initialize wave function *)
\[Psi] = Table[\[Psi]0[x, 0], {x, xValues}];

(* Time evolution function *)
timeEvolution[\[Psi]_, m_] := Module[{\[Psi]New, nonlinearTerm},
    \[Psi]New = \[Psi];  (* Start with the current wave function *)
    (* Update wave function using FDM *)
    For[i = 2, i < Length[\[Psi]] - 1, i++,
        nonlinearTerm = Abs[\[Psi][[i]]]^2 \[Psi][[i]];
        \[Psi]New[[i]] = \[Psi][[i]] - (dt/(2 I)) ((\[Psi][[i + 1]] - 2 \[Psi][[i]] + \[Psi][[i - 1]])/(dx^2)) - dt nonlinearTerm;
    ];
    \[Psi]New
];

(* Store results for animation *)
results = {};
(* Time-stepping loop *)
For[m = 1, m <= mMax, m++,
    \[Psi] = timeEvolution[\[Psi], m];
    If[Mod[m, Round[mMax/5]] == 0, AppendTo[results, \[Psi]]];  (* Store every 5th wave function *)
];

(* Create the animation for the real part of the wave function *)
ListAnimate[Table[
  ListLinePlot[Re[results[[m]]], PlotRange -> All, 
               PlotLabel -> "Ma-Soliton Evolution (Real Part)", 
               AxesLabel -> {"x", "Re[\[Psi](x,t)]"}, 
               PlotStyle -> {Thick, Blue}, 
               ImageSize -> Large,
               PlotLegends -> {"t = " <> ToString[m dt]}],
  {m, 1, Length[results]}
]]

enter image description here

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.