2

So I solved following ODE $$\frac{d^2y}{dx^2}+(a^2 (5+4\tanh( x))+b^2)y=0$$ in Mathematica. Now I need to create a list for further manipulation using the above solution. I wrote the following code:

list = {}
s = ParametricNDSolve[{y''[x] + (a*a + b*b*(5 + 1 Tanh[x]))*y[x] == 0,
y[-10] == Exp[I*10*Sqrt[a*a + 4*b*b]], y'[-10] == (-I)*Sqrt[a*a + 4*b*b]*Exp[I*10*Sqrt[a*a + 4*b*b]]}, y, {x, -10, 10}, {a, b}]
For[i = -1, i <= 1, i = i + .1,
 For[j = -1, j <= 1, j = j + .1,
     y1 = y[i, j] /. s;
     list = Append[list, {i, j, Abs[y1[0]]}]
    ]
   ]

I know the error lies in the last part of Append function i.e. Abs[y1[0]] but I cannot come with any solution to this issue. How should I resolve this issue?

aitfel
  • 177
  • 5
  • 3
    This code produces no errors when run un a fresh kernel. You should really be using Table instead of For. https://mathematica.stackexchange.com/questions/134609/why-should-i-avoid-the-for-loop-in-mathematica – Szabolcs Mar 05 '20 at 14:30

1 Answers1

1

Here is the solution which @Szabolcs proposes

Y = 
  ParametricNDSolveValue[ 
    {y''[x] + (a*a + b*b*(5 + 1 Tanh[x]))*y[x] == 0, 
     y[-10] == Exp[I*10*Sqrt[a*a + 4*b*b]], 
     y'[-10] == (-I)*Sqrt[a*a + 4*b*b]*Exp[I*10*Sqrt[a*a + 4*b*b]]}, 
    y, {x, -10, 10}, {a, b}] 

Table[{i, j, Y[i, j][0]}, {i, -1, 1}, {j, -1, 1, .1}]
(*{{{-1, -1., 0.945636 - 0.134144 I}, {-1, -0.9,0.949913 - 0.116563 I},{-1,-0.8,0.954501 - 0.0988263 I}, …}*)
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Ulrich Neumann
  • 53,729
  • 2
  • 23
  • 55